Android Basics – Add a Button and display a Toast on click

Time for some Action. Let’s add a Button to the Layout XML file and on click of button let’s display a message to the user.

Simple Steps

Add a Button component to the XML file.

    android:text="Click Me"

Add a method to the MainActivity class.

public void clickMe(View view){
    Toast.makeText(MainActivity.this, "Button Clicked !!", Toast.LENGTH_SHORT).show();

You are all set. Test your app now.

Source Code

Author: Ankur

I am Enthusiastic about Learning new things and sharing my Knowledge. I like programming and have a pretty good background in Computer Science.

One thought on “Android Basics – Add a Button and display a Toast on click”

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: