Wednesday, 19 May 2010

Toast method

Toast is one of the notification method in android which is used to display text on surface for the specified time, once the time limit exceeds automatically it will disappear.
Example program for Toast message:







  • Go to res folder, click on layout folder, and double click on activity_main.xml file

  • click on Text Fields icon, select plain text field and drop and drag 

  • double click to go to activity_main.xml file

  • we will get the following screen shot:

  • Now write the name of the text file: i.e: "Message":
  • to write this code, write after <EditText tag, and see the screen shot also
android:hint="Message"


  • Now click on GraphicalLayout and do the following
  • click on Text filed icon (ie PersonName ), and drop and drag:

  • once you draged, create a button icon (display)
  • To create the Button icon on the surface, click on FormWidgets, choose Button, drag on the surface

  • double click on button icon to go to activity_main.xml file



  • Now change the name of Button, and replace with "Display"
Example code:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:hint="Message"
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginRight="26dp"
        android:layout_marginTop="34dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/textView1"
        android:text="Display" />

</RelativeLayout>
  • see the following screen shot:

  • Now we need to access the msg, and if we click display button, it has to display whatever we type in the text field.
  • go to MainActivity.java of src folder

  • see the code below:
package com.example.toastdemo1;

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.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
//step 1: create the reference variables for Button, EditText
Button b;
EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//step 2: create the objects for Button, 
b=(Button)findViewById(R.id.button1);
//step 3: configur the button with xml, add the Listeners events to that button object, and //add packages
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//step 5: write the logic for access the text from the text filed
et=(EditText)findViewById(R.id.editText1);
//step 6: read the data from the text field and store into local object String 
String msg=et.getText().toString();
//step 7: display in a toast message
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
});//step 4: write the comment here
}

@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;
}

}



  • see the screen shot:

  • now run the application








No comments:

Post a Comment