Wednesday, 7 January 2015

Android Beginner (Getting Started with Android) - Even Or Odd



In this a very basic app which checks whether a number is even or odd.

This app is done using Android Studio.
This is the link to get Android Studio.


NOTE :This post is targeted to those who have not programmed android before, this a simple app for gettiing started with android app development. 





A number is called EVEN when it is divided by 2 , all other numbers are ODD numbers

The condition is when a number is divided by 2 and if its remainder is 0 it is EVEN else ODD


For developing an android app for this we  first design the layout or the user interface for it. Generally the layout is mostly designed in XML however it can also be done using Java .

Lets do it now.

1) Setting Up The Project


First open Android Studio -> create a new project
Give an application a name , company name and location of the project .

Then select Phone and  Tablet


Next Select a Blank Activity

Add caption

Click Next





Then Click Finish.

2) Design

We will have one text field  for entering the number and one button for checking whether the number is even or odd.

For the text field we can directly drag and drop a Text Field from the Palatte in the Design mode


or we can write the code for it using the text mode

<EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter any number"
        android:inputType="number" />


Similarly we can drag and drop a button from the Widgets section of the  Palatte in the design mode


or write the code using the text mode

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check" />


 Now will add some space between Edit Text and Button and make the whole design centered.

for making the whole layout centered set the parent layouts gravity to center
android:gravity="center"

for having some space between Edit Text and Button  place View between them as shown

<View
        android:layout_width="0dp"
        android:layout_height="50dp" />

Now the layout will look like




The Complete XML Code is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter any number"
        android:inputType="number" />

    <View
        android:layout_width="0dp"
        android:layout_height="50dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check" />
</LinearLayout>

4) Functionality(Java Code) 

First we will initialize  Edit Text and Button which we used in our layout for design

place this  2 lines  below setContentView line

EditText input = (EditText) findViewById(R.id.editText);
Button check = (Button) findViewById(R.id.button);

for this button we will set an onClickListener
check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                

            }
        });

now when the button is clicked we will check whether the number is EVEN or ODD
first we will get the value entered in Edit Text field and convert it to string
int value = Integer.valueOf(input.getText().toString());

then we will check for EVEN or ODD

if (value % 2 == 0)
                    Toast.makeText(MainActivity.this, "EVEN NUMBER", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(MainActivity.this, "ODD NUMBER", Toast.LENGTH_LONG).show();


It will Toasts the result whether the number is EVEN or ODD

finally we will also add an error condition  when no input is given
if (input.length() == 0) {
                    Toast.makeText(MainActivity.this, "Enter any number",
                            Toast.LENGTH_SHORT).show();
                    return;
                }

That's it .we are done.

The Complete Java Code will look like
package com.example.avinash.firstapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText input = (EditText) findViewById(R.id.editText);
        Button check = (Button) findViewById(R.id.button);


        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (input.length() == 0) {
                    Toast.makeText(MainActivity.this, "Enter any number",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                int value = Integer.valueOf(input.getText().toString());

                if (value % 2 == 0)
                    Toast.makeText(MainActivity.this, "EVEN NUMBER", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(MainActivity.this, "ODD NUMBER", Toast.LENGTH_LONG).show();
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


5) Running the App 

Connect your phone with an usb cable to the system or you can run it emulator

click on the run button present on the top in the toolbar





OUTPUT :


For EVEN number




For ODD number




Error Checking