android-tost-example

Toast or popup message example in android

Toast or popup message example in android

Toast is a popup message that come for a duration and automatically fade out.User can't control the toast message.

Here the Toast function has written under  a button click . Short code for Toast

Toast.makeText(ToastActivity.this, " Your Toast message.", Toast.LENGTH_LONG).show();

 

Now Look at the source code.
       1. ToastActivity.java


 

package com.androidfromhome.toast;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ToastActivity extends Activity implements OnClickListener {

    private Button btn_toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
        
        
        btn_toast=(Button)findViewById(R.id.btn_toast);
        btn_toast.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_toast:
            Toast.makeText(ToastActivity.this, "Hi, Your Toast is ready to use.", Toast.LENGTH_LONG).show();
            break;

        default:
            break;
        }
        
    }
   
}

 


 

xml file "activity_toast.xml"


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.androidfromhome.toast.ToastActivity" >

<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#A758AB"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="Android From Home"
android:textColor="#ffffff"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >

<Button
android:id="@+id/btn_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Toast Me" />
</LinearLayout>

</LinearLayout>

 


 For custom toast go to next tutorial.

Download Source Code

 

Related Post:-

Internet connection checking in android device

Custom Alert Dialog in android with custom xml layout

ListView in android example

email send from android application example

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget