Explain event handling with button control in Android.
Events are messages or notifications that are generated by controls such as Button, Checkbox, and Radio Button etc. System responds to these messages by executing some code. In android you can handle events in different ways. The most important methods are as follows.
Method 1
Responding to Click Events using XML layout file:- When the user clicks a button, on-click event is fired and received by button object.
- To define the click event handler for a button, add the android:onClick attribute to the Button element in your XML layout file.
- It is important to note that the value of android:onClick attribute must be the same as the method that you will define in MainActivity.java file.
Let’s take an example of button control, defined in layout file.
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Show"
android:id = "@+id/button"
android:textSize = "30dp"
android:onClick = "showMessage"
/>
The showMessage() that you will define in java file must be public, return type should be void and must have only View object as parameter.
Following is the content of the modified MainActivity.java file
public class MainActivityextends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showMessage(View v) // method name is same as define in onClick attribute
{
Toast toast = Toast.makeText(getApplicationContext(), "Welcome to CareerRide",Toast.LENGTH_LONG);
toast.show();
}
}
Method 2
Using an OnClickListener and Anonymous Inner Class
Following is the content of the modified MainActivity.java file:
public class MainActivityextends AppCompatActivity
{
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast toast = Toast.makeText(getApplicationContext(), "Welcome to CareerRide ",Toast.LENGTH_LONG);
toast.show();
}
});
}
}
Method 3
In this method I have taken three buttons. You can handle the click events of these buttons in a single onClick() method if you implement the OnClickListener interface.
Example:
Following is the content of the modified MainActivity.java file
public class MainActivityextends AppCompatActivityimplements View.OnClickListener
{
Button btn1, btn2, btn3;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button)findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.button1 :
Toast toast = Toast.makeText(getApplicationContext(), " Welcome to CareerRide ",Toast.LENGTH_LONG);
toast.show();
break;
case R.id.button2 :
// Your code….
break;
case R.id.button3 :
// Your code……..
break;
}
}
}