How To Display Toast in Android (Android Studio)

Toast or toast message is a great functionality of android OS. This is the most useful and easy method to show errors, messages, activity transitions, and many other things. Here in this article, I will show you that how you can show it using java or kotlin. I am also going to cover that how when and where you should show it.

What is a toast message?

A toast message is a message box we can see at the bottom of the screen of an Android device. This a functionality to provide different information to the user like activity name, error code, activity lifecycle, etc. For testing purposes, this functionality is very helpful. You can check different components like buttons, text views, image views, etc.

Download the PDF of all keyboard shortcuts of Android Studio!

Toast message android

Basic code for toast

To show the toast message we need context, text, duration, and method. This is almost the same for kotlin and java. You just need to set the activity name where you want to show the message and then the text you want to show. Set the time or duration of your toast message and then use the method to show that. Here is the basic structure:

Java

Toast.makeText(context, text, duration).show();

Kotlin

Toast.makeText(context, text, duration).show()

Code Example (Java)

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

Code Example (Kotlin)

val text = "Hello toast!"
val duration = Toast.LENGTH_SHORT

val toast = Toast.makeText(applicationContext, text, duration)
toast.show()

Show toast on button click (Java)

Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() { 
public void onClick(View v)  {
Toast.makeText(getBaseContext(), "You clicked on this button!!" , Toast.LENGTH_SHORT ).show();
}
});

Show toast on button click (Kotlin)

package com.techkib.pathgriho.helloworld

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
 
class ToastActivity : AppCompatActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_toast)
 
        var btn_click_me = findViewById(R.id.button1) as Button
 
        btn_click_me.setOnClickListener {
            // make a toast on button click event
            Toast.makeText(this, "You clicked on the button!", Toast.LENGTH_LONG).show()
        }
    }
}

Change duration of toast message

There are 2 constant you can use to control the duration of the toast message. Here is the list for you:

ConstantDescription
public static final int LENGTH_LONGdisplays view for a long duration of time.
public static final int LENGTH_SHORTdisplays view for a short duration of time.

For example, if you use "Toast.LENGTH_LONG" then the toast message will stay on the screen for a long duration. If you use "Toast.LENGTH_SHORT" then the toast message will stay on the screen for a short duration.

Conclusion

Toast message is easy and lite functionality of android OS. Use it when you don't need any fancy-looking functionality. It doesn't use so much power which is another plus point of this thing. Again it is easy to implement in any Java or Kotlin project.

We have many other articles on android studio. Check out those to gain more knowledge about android development. Subscribe to our email subscription to get free ebooks every week.

Post a Comment (0)
Previous Post Next Post