Download Code

Android Toast Example

In android toast example you will learn by way of example that how to display toast in android application.First let me give some introduction for example 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

For further illustration you have to create new Android Toast Example Application Project.

toast,android toast,android toast example,toast example,

Now click next until finish is enables then click finish.When you click finish you will see two files an xml graphical layout and an Activity which you will call as .java class. just like this.

As a result of finish click you will see Graphical Layout(white screen is a layout with “Palette” this panel contains all of the UI components like button,scrollView,textfields etc).

toast,android toast,android toast example,toast example,

Xml Layout(always remember one point don’t leave blank space at the start of the xml.If you do that thing it will give you error so make sure there is no space there).

toast,android toast,android toast example,toast example,

Your java class looks like this image by default when you create project.

toast,android toast,android toast example,toast example,

You know about your defaults files and how they look like.So,now first create button in  layout.You have two choices using XML or drag and drop from UI components Panel known as Plaette.

toast,android toast,android toast example,toast example,In that case you set up button for click now it’s time to refer that button in java class.

Create variable of Button within a parent class which is “MainActivity”  for the purpose of refering id of that button used in xml.

public class MainActivity extends Activity{
           Button myBtn; //variable of button
           protected void onCreate(Bundle saveInstanceState){
                super.onCreate(saveInstanceState);
                setContentView(R.layout.activity_main);
                myBtn = (Button) findViewById(R.id.button1); // getting the id and store in myBtn variable   
           }
}

Now after that you are going to implement clickListener Event on Button.So you can apply that Event like this.

public class MainActivity extends Activity{
           Button myBtn; //variable of button
           protected void onCreate(Bundle saveInstanceState){
                super.onCreate(saveInstanceState);
                setContentView(R.layout.activity_main);
                myBtn = (Button) findViewById(R.id.button1); // getting the id and store in myBtn variable 
                myBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});  
           }
}

When see you red line on OnClickListener take your cursor on to that and a minute window will display from that window import OnClickListener View(it’s View don’t forget).

Then you will write Toast code in that “onClick” method.

public class MainActivity extends Activity{
           Button myBtn; //variable of button
           protected void onCreate(Bundle saveInstanceState){
                super.onCreate(saveInstanceState);
                setContentView(R.layout.activity_main);
                myBtn = (Button) findViewById(R.id.button1); // getting the id and store in myBtn variable 
                myBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "Android Codec", Toast.LENGTH_SHORT).show();
			}
		});  
           }
}

In conclusion you will get a Toast on Button click.

Activity (MainActivity.java)

public class MainActivity extends Activity {

	Button myBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myBtn = (Button) findViewById(R.id.button1);
        myBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "Android Codec", Toast.LENGTH_SHORT).show();
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Run your android toast example app.Final app display when you click on button or final output.

toast,android toast,android toast example,toast example,