Tuesday, August 28, 2012

Android | Toast Example

In most of the applications, we need to show messages like "Data save successfully.", "Delete successfully.". To show such type of message, android provides "Toast" feature.

Note: verified on Android 4.0.3 Emulator.


Below code will help you get above toast message.

Code of Layout file:


<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/btnok"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnok" />
</LinearLayout>

Code of Strings.xml file:


<resources>

    <string name="app_name">ToastExample</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_toast_example">Toast Example</string>

    <string name="btnok">OK</string>
</resources>



Code of Java file:


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 ToastExample extends Activity implements OnClickListener {

private Button btnOk;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast_example);
       
        btnOk = (Button) findViewById(R.id.btnok);
       
        btnOk.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_toast_example, menu);
        return true;
    }

public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hi Friends !!" , Toast.LENGTH_LONG).show();

}
}


Above, code always displays message at default location. Now, if you want to set toast at your desired location (like center of screen) then replace above "OnClick" method with below one in "Java" file.

public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
Toast oToast = Toast.makeText(getApplicationContext(), "Hi Friends!!", Toast.LENGTH_LONG);
oToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
oToast.show();
}
catch(Exception e){
Log.e("Toast App", e.getMessage());
}


Result:


Now, it is time to design own screen for Toast.

activity_toast_example.xml:

<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/btnok"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnok" />
</LinearLayout>

toast_layout.xml (Create this file in layout folder and add an image in each drawable folder)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tbllayouttoast"
    android:background="#bbff0000"
     >
    <TableRow>
        <ImageView 
            android:id="@+id/ivimage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/us"
            android:contentDescription="@string/ivimage"
            android:padding="10dp"
            />
        <TextView
            android:id="@+id/tvimage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/tvimage"
            android:padding="10dp"
            android:textColor="#FFFFFFFF"
            />
    </TableRow>
</TableLayout>

Strings.xml

<resources>

    <string name="app_name">ToastExample</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_toast_example">Toast Example</string>

    <string name="btnok">OK</string>
    <string name="ivimage"></string>
    <string name="tvimage">Hi Friends !!</string>
</resources>

ToastExample.java

package com.example.toastexample;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class ToastExample extends Activity implements OnClickListener {
private Button btnOk;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast_example);
        
        btnOk = (Button) findViewById(R.id.btnok);
        
        btnOk.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_toast_example, menu);
        return true;
    }

public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
LayoutInflater oLayoutInflater = getLayoutInflater();
View oView = oLayoutInflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.tbllayouttoast));
Toast oToast = new Toast(getApplicationContext());
oToast.setDuration(Toast.LENGTH_LONG);
oToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
oToast.setView(oView);
oToast.show();

}
catch(Exception e){
Log.e("Toast App", e.getMessage());
}
}

}


Result:






No comments:

Post a Comment