Android Studio Tutorial: Simple Button OnClick and TextView Display

In this tutorial we will set up a Button that will display a message in a TextView.

First, we need to create a new project. For this project I will use the Blank Activity. You can name your project whatever you want it does not matter.

After the project has been created, we will start with the activity_main.xml tab. This file, is the layout of our app. all of the layout customizing will be done with in this file.

A button can be added in two ways, one is to actually code the button in the “Text” tab or by using the “Design” tab. Both can be located in the bottom left of the screen. But for this example I will do it by using the Design function.

To create the button, click and drag the button on the the canvas using the “Button” tab that is provided in the palette panel .

Then constrain the button to the Layout using the Constrain in the Attributes panel. Also give the Button an id, you can do so by using the Attributes panel. The id I used was ‘button’ to keep things simple.

That is everything that needs to be done on the Xml file. Now lets move on to the MainActivity.java file to code the function of the button.

First, we have to call our TextView and the Button. For this example I will call the TextView “OurText” and the Button “TheButton”.

Last step is to set the variables we created to reference the Ids of the TextView and Button on the activity_main.xml file and the set the OnClickListener for the button. Then add the text that will be shown when the button is clicked.

Thats It! Test it out by running the app and clicking the button.When the button is clicked the text “The Button was Clicked” should appear. Ill provide all the code for this project below.

MainActivity. java

public class MainActivity extends AppCompatActivity {

    TextView OurText;
    Button TheButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //setting the variables to the Layout file
        OurText = findViewById(R.id.textView);
        TheButton = findViewById(R.id.button);

        //Setting the OnClick Listener
        TheButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // What we want to display when button is clicked
                OurText.setText("The Button was Clicked!");
            }
        });
    }
}

activity_main.xml

<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="159dp"
        android:layout_marginEnd="164dp"
        android:layout_marginBottom="233dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

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 )

Facebook photo

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

Connecting to %s