Toast Message OR Error Message

Guest Posts coolbhushans

In Android, Toast is a notification message that pop up,

Display a certain amount of time, and automatically fades in and out,

Most people just use it for debugging purpose or show the error message

 

 // short period of time 2 second
Toast.makeText(getApplicationContext(), "Please Enter Name ..!", Toast.LENGTH_SHORT).show();

// long period of time 3.5 second
Toast.makeText(getApplicationContext(), "Your Name is"+getText, Toast.LENGTH_LONG).show(); 

res/layout/activitymain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout
width="matchparent"

android:layout
height="matchparent"   android:paddingBottom="@dimen/activityverticalmargin"

android:paddingLeft="@dimen/activity
horizontalmargin"

android:paddingRight="@dimen/activity
horizontalmargin"

android:paddingTop="@dimen/activity
verticalmargin"

tools:context=".MainActivity" >

<EditText

android:id="@+id/editText1"

android:layout
width="wrapcontent"

android:layout
height="wrapcontent"

android:layout
alignParentLeft="true"

android:layoutalignParentTop="true"

android:layout
marginLeft="80dp"

android:layoutmarginTop="19dp"

android:ems="10" >

<requestFocus />

</EditText>

<TextView

android:id="@+id/textView1"

android:layout
width="wrapcontent"

android:layout
height="wrapcontent"

android:layout
alignParentLeft="true"

android:layoutalignTop="@+id/editText1"

android:layout
marginLeft="14dp"

android:textStyle="bold"

android:textSize="20dp"

android:text=" Name " />

<Button

android:id="@+id/button1"

android:layoutwidth="wrapcontent"

android:layoutheight="wrapcontent"

android:layoutalignLeft="@+id/textView1"

android:layout
below="@+id/editText1"

android:layoutmarginTop="30dp"

android:text=" Show " />

<Button

android:id="@+id/button2"

android:layout
width="wrapcontent"

android:layout
height="wrapcontent"

android:layout
alignBaseline="@+id/button1"

android:layoutalignBottom="@+id/button1"

android:layout
centerHorizontal="true"

android:text=" Clear " />

</RelativeLayout>

   
src/MainActivity.java

 

 package com.gnetspace.toastmessage;
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 {
    Button showBtn,clrBtn;
    EditText edtTxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtTxt=(EditText) findViewById(R.id.editText1);
        showBtn=(Button) findViewById(R.id.button1);
        clrBtn=(Button) findViewById(R.id.button2);
        showBtn.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String getText=edtTxt.getText().toString().trim();
                if(getText.equals(""))
                {
                    Toast.makeText(MainActivity.this, "Please Enter Name ..!", Toast.LENGTH_LONG).show();
                }else
                {
                    Toast.makeText(MainActivity.this, "Your Name is "+getText, Toast.LENGTH_LONG).show();
                }
            }
        });
        clrBtn.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                edtTxt.setText("");
            }
        });         
    }
    @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;
    }   
} 

output after running app
ART3_image1

ART3_image2