Sunday, 11 June 2017

How to use Buttons in Android App



Download Source code


In this post you can learn the fundamental working concept of Buttons in Android .

I am using Eclipse IDE for this example but it does not differ too much with Android studio. So users who are using Android studio also can refer this example.

First create a New Project in Eclipse IDE.

It is same as that of creating Hello World app which I have already posted before.

Read How to develop Hello World app or Creating New Project in Eclipse IDE 

In that post after creating a New Project and before going to run the Helloworld app we are going to create a Button.

Creating a button is very easy .

Look for the Pallet option near the work space area.



In that you can see the Button option in it.


Simply click and drag that Button and place anywhere in the work space.


You can remove that warning symbol by reading this post resolve warning.

Although it does not causes error it is better to avoid those warning, you can continue coding with that warning also.

Next we need to give action to that button  when it has been clicked.

Now its time to java code.

You need to register your button with the MainActivity.java and then can write action code of button.

First go to MainActivity.java which can be found under the res folder.


Then open MainActivity.java file, it will like in the below picture

in the above you can delete the selected code which not going to useful for us in this example.


after deleting we need start writing the button code below setcontentview line



First we need to register the Button.

it can be done by:

Button b=(Button)findViewById(R.id.button1);

I will explain the code in detail later in this post.

After typing the code you may find some errors.


It is nothing but we need to import the  Button widget, just move the mouse over that error you will see



Click Import 'Button' (android.widget)

Now the error is gone.


After this we have to write the code to detect when the button is clicked, this is similar to listening to the button click , we need to set the onClickListener to the button like:

b.setOnclickListener(this)

If you type the following code you will get error, just move over the error and you will see the options


In that scroll down and you will find Let MainActivity implements OnClickListener and click it.

Then after implenting the OnClickListener class again there will be an error to implement the overriden classes from the implemented interface


Move the mouse over the error and click add unimplemented methods


After clicking that you can see onClick() will be overrided

Inside the onClick() method only we are going write the code for the button action.

In this example i am going to print a message 'Hai' with the help of Toast.

Toast is a short message appears at the bottom of the screen for short time of period ex:3 sec or  5 sec .

This is the code for the Toast message.


That's it , after done the above run the program.

To Run the Program read

How to use Android Phone as an Emulator

How to Create an Emulator in Android Eclipse

Emulator is nothing but it is place to run the Android apps that we have developed.

The output will be





After clicking the button


 Code Explanation:

Button b=(Button)findViewById(R.id.button1);

 In the above code Button is an UI element in Android it is like a Data type.

b is a object or name of the button which used through out the program to use a particular button.

(Button) is a Type Casting the button we have created is of View type we have to convert into Button type.

Every button has its own id , in our example the id of our button is button1.

we can find it by findViewById method.

This the first step.

The next one is setting the OnClickListener method to the created button.

then we need to implement OnClickListener interface and add the unimplemented methods.

then the onClick method will be overridden, inside that we need write the code.

  
Toast is to display the short message , it is like a notification.

makeText is a method to describe where the text message should be appear , what is the message to be appear and the duration of the Toast message.

MainActivity.java

public class MainActivity extends Activity implements OnClickListener
{
   

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0)
    {
        // TODO Auto-generated method stub
      
        Toast.makeText(getApplicationContext(), "Hai", Toast.LENGTH_LONG).show();
      
    }

}

We will discuss the above concepts in detail in the upcoming posts.