Download Code

Android Studio Toast Example Android

Android Studio Toast Example Android teach you how to display toast in android application.First let me give some introduction of what Toast is.

Toast

Toast is a small popup feedback message which display to the user when interacting to UI component like when click on button it will display “Android Codec” Toast.You can also set Toast position to left,top,right etc. More Detailed

Now let’s start go ahead create new Android Studio toast example android Application Project.

How to create new android application project.Follow the steps.

  • Go to File > New Project then select.
  • New window will appear give Project Name,Company Domain(anything).
  • Select Project path at bottom of screen and hit Next.
  • Next select API Level and deployment type(Mobile or anything else) and hit Next.
  • Select default activity type(Blank/Empty Activity).It has a lot more types but when you develop any application from scratch then select Empty Activity type.
  • Give your Class,Layout Name and hit Finish.

After clicking Finish button it will took some time to configure your android application project.Here is the by default view of your newly created android application project.

Android Studio Toast,Android Studio Toast Example,Android Studio Toast Example android,toast example android,

Now i’ll tell you about layouts/activity.

activity_main.xml is a Layout file which have two views called Design/Text.Above Screen show you Design View but when you switch to Text View it will show Design in parallel.

Android Studio Toast,Android Studio Toast Example,Android Studio Toast Example android,toast example android,

MainActivity.java is your class/activity which runs your UI means your xml code with some funcationality.

Android Studio Toast,Android Studio Toast Example,Android Studio Toast Example android,toast example android,

Alright your basic information about your android application project.So let’s start building an application of toast.

You will basically use a Button UI component to display Toast in android application.

Now you will add Button in your activity_main.xml file using two options.

  • Drag and Drop From Plattete Section Under Widgets in Design View.
  • Write XML code with in Text View.

Android Studio Toast,Android Studio Toast Example,Android Studio Toast Example android,toast example android,

You have set up button for click now it’s time to refer that button in an activity which is MainActivity.java class.

Follow the steps.

  • Create Button variable
  • Refer id of that button from xml.
  • Apply Click event on button and display Toast.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        display = (Button) findViewById(R.id.button);
        display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Android Codec",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Now run android application project.

Android Studio Toast,Android Studio Toast Example,Android Studio Toast Example android,toast example android,

I hope you are familiar with environment of Android Studio.That’s it for this tutorial.

Thanks.