Showing posts with label how to display toast message in android. Show all posts
Showing posts with label how to display toast message in android. Show all posts

Friday, 4 May 2012

Toast in Android

A toast provides simple feedback about an operation in a small popup.

First, instantiate a Toast object with one of the makeText() methods.

This method takes three parameters:

 1)application Context,
 2)text message
 3)duration for the toast.

It returns a properly initialized Toast object.

You can display the toast notification with show(), as shown in the following example:

Context context = getApplicationContext();
CharSequence text = "Hello toast message!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();


(Or)

Chain process

Toast.makeText(context, text, duration).show();

Positioning your Toast:

A standard toast notification appears near the bottom of the screen, centered horizontally.
You can change this position with the setGravity(int, int, int) method.
This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the top-left corner.

you can set the gravity like this:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

Example Project:

UI Layout(toast_display.xml)

<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"
    tools:context=".ToastDisplayActivity" >

    <Button
        android:id="@+id/showToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Show Toast" />

</RelativeLayout>

Activity class(ToastDisplayActivity.java)

package com.androidsurya.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 ToastDisplayActivity extends Activity {
    Button showToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toast_display);
        showToast = (Button) findViewById(R.id.showToast);
        showToast.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Toast.makeText(getApplicationContext(), "Toast message Display",
                        Toast.LENGTH_LONG).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.toast_display, menu);
        return true;
    }

}

Register Activity in AndroidManifest file


 <activity
            android:name="com.androidsurya.toastexample.ToastDisplayActivity"
            android:label="@string/app_name" >

Output Screenshot:



Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...