Pages

Friday, 14 March 2014

Android: Using Toast Messages.

Android Toast is a little message used to provide feedback to the user. Today in this tutorial we will see how to make use of these Toast messages.

Output will be as:
Android Toast Message


Xml file:
res/layout/
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  > 
  <Button  
   android:id="@+id/button1" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="Android Toast" 
   /> 
  </LinearLayout> 

Java file:
src/
 package com.example.toastexample; 
 import android.os.Bundle; 
 import android.app.Activity; 
 import android.view.Menu; 
 import android.view.View; 
 import android.view.View.OnClickListener; 
 import android.widget.Button; 
 import android.widget.Toast; 
 public class MainActivity extends Activity implements OnClickListener { 
   Button b1; 
   @Override 
   protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      b1=(Button) findViewById(R.id.button1); 
      b1.setOnClickListener(this); 
   } 
   @Override 
   public void onClick(View v) { 
      // TODO Auto-generated method stub 
      switch(v.getId()){ 
      case R.id.button1: 
        Toast.makeText(getApplicationContext(), "Android Toast Message", Toast.LENGTH_SHORT).show(); 
        //Display Toast message 
        break; 
      } 
   } 
 } 
Here Toast.makeText() method is used to make a Toast message which takes Context, Message and the duration for which the toast is to be displayed. And then show() method is used to display the Toast Message.

No comments:

Post a Comment

[フレーム]

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /