• Register
  • Log in
  • Home
  • Contact

Android button event handler



Android Button Event Listeners and handlers

OnClickListner interface

public class MainActivity extends Activity implements OnClickListener {
  
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
   Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(this);
        Button btn2 = (Button)findViewById(R.id.button2);
        btn2.setOnClickListener(this);
        Button btn3 = (Button)findViewById(R.id.button3);
        btn3.setOnClickListener(this);
     }
     @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId() == R.id.button1){
            Toast.makeText(context, "Button 1 Click", Toast.LENGTH_LONG).show();
        }
        else if(v.getId() == R.id.button2){
            Toast.makeText(context, "Button 2 Click", Toast.LENGTH_LONG).show();
        }
        else if(v.getId() == R.id.button3){
            Toast.makeText(context, "Button 3 Click", Toast.LENGTH_LONG).show();
        }
    }
}



 1- OnClick event for each View

OnClickListener()    //Event Listeners

When the User  click or touch upon any widget like Button, ImageButton, EditText, Image.

<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:orientation="vertical" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button Click event handler" />
     
    <Button
        android:id="@+id/button2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button long click event handler" />
     
    <Button
        android:id="@+id/button3"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="buttonClick"
        android:text="Button declaratively event handler" />
 
</LinearLayout>

Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
             
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "Button click event handler", Toast.LENGTH_LONG).show();
            }
        });

OnClick Declaratively Event Handler
Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout.
For example: XML file: Line # 6  using android:onClick (Button click event declaratively). For example

1.<Button
2.        android:id="@+id/button3"
3.        android:layout_width="300dp"
4.        android:layout_height="wrap_content"
5.        android:layout_gravity="center_horizontal"
6.        android:onClick="buttonClick"                             //Click Event Declaratively
7.        android:text="Button declaratively event handler" />
 
MainActivity.java
1.      public void buttonClick(View v) {
2.        Toast.makeText(this, "Button declaratively event handler", Toast.LENGTH_LONG).show();
3.    }

 

2- OnLongClick

OnLongClickListener() // Event Listeners

The user either click or touch upon any widget like Button, ImageButton, EditText, Image for one or more seconds to handle such event.

01.     btn2.setOnLongClickListener(new View.OnLongClickListener() {
02.             
03.            @Override
04.            public boolean onLongClick(View v) {
05.                // TODO Auto-generated method stub
06.                Toast.makeText(context, "Button long click event handler", Toast.LENGTH_LONG).show();;
07.                return false;
08.            }
09.        });

 


© 2021 - KodeCenter beta