Android Studio Tutorial for Beginner #6: Button Click Listener and Text Change

A button is a widget which you can use to listen to events occurring in an activity. If a user click on button we can perform number of different task like text change, going to another activity, passing data etc.

To access your layout elements in your activity or java file. You need to provide id/name to your element like this:

 <TextView
        android:id="@+id/txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

@+id will create a new id with the name txt_name, through which we can access this TextView in our java file. Similarly, you will always have to provide a new unique id to every element you want to manipulate through java code.

Now let’s complete the task. We need

  1. Button and TextView.
  2. Give them a unique id.
  3. Add click listener on the button.
  4. Change TextView text on click of Button.

We can Apply click listener on Button in 3 different methods. But i am going to show you the most reliable way to code your button click listener.

In your activity_main.xml write

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="Button Example"
        android:textStyle="bold"
        android:textSize="26dp"
        tools:ignore="MissingConstraints" />
    <TextView
        android:id="@+id/txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="Hello World!"
        android:textSize="26dp"
        tools:ignore="MissingConstraints" />
    <Button
        android:id="@+id/btn_click"
        android:layout_margin="10dp"
        android:text="Click to chage Text"
        android:background="#6A2BD6"
        android:textColor="#fff"
        android:textSize="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

I have given Id to both Button and TextView as I want to access both elements in my java code, but not in my First TextView as I don’t need to change anything in my Heading TextView.

Now, in your MainActivity.java write:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
    TextView txt_name;
    Button btn_click;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt_name = findViewById(R.id.txt_name);
        btn_click = findViewById(R.id.btn_click);
        btn_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txt_name.setText("Ok! I understand How this Works");
            }
        });
    }
}
TextView txt_name;
Button btn_click;

This is how you declare your elements in java file.

findViewById(): find your element by its Id.

btn_click.setOnClickListener(): In this method you have to write the code about what you need to do on clicking of a button. We need to change text in our TextView so we will use setText() method to change the text.

You can watch my video if you want to know more about this code:

Thank you! please comment if you have any query and share and learn for free!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
error

Enjoy this blog? Please spread the word :)