Ways you can handle button click in Android Studio Project


In a Android project you need to handle button click for completion of data processing, a double click maiy not enough create automatic Event handler as in Microsoft visual Studio projects. You have to do it in Java style.

You have two options, one is to handle click in Oncreate method or simply add new Method and attach it to the Onclick property [Layout]

 The first Method

Most of the coders love this method and it can save time as well. The code reside in OnCreate method

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);    
    textView = (TextView) findViewById(R.id.text1);
    Button button2=findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText("I clciked button2 - Oncreate");
        }
    });
     
}
The Second Method

This will require Create a Public Method with View as argument. Keep in mind that all controls should be declared just below the beggining of the class and assing controls at OnCreate method

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);         
        textView = (TextView) findViewById(R.id.text1);
   
    }
The hadler method

Place the code after the OnCreate

public void ButtonClick1(View view) {
    textView.setText("I Clicked Button1");
}

Go to the layout and  find the onclick property of the button ther you will see the ButtonClick1 choose the right one

or you can configure it in the XML file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="ButtonClick1"
            android:text="OnClick 1"
            tools:layout_editor_absoluteX="30dp"
            tools:layout_editor_absoluteY="58dp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Onclick 2"
            tools:layout_editor_absoluteX="128dp"
            tools:layout_editor_absoluteY="58dp" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Watch the tutorial video on Youtube

I think this was enough for begiiners, leave comments and questions below

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.