Android Toast Example

Android-toast-example-featured
Android-toast-example-blog

Android Toast Example:

Android toast can be used to display the internet connectivity status and application exit status. It can be used to make the more interaction to the application. Android toast example given below to help you in android programming!.

Android Toast
download

Toast.makeText():
Toast.makeText method used to display the toast. we need to pass the three parameters,
1. Context
2. String to display in toast (EditText value should be used with getText method)
3. Toast display time (can be Toast.LENGTH_LONG or Toast.LENGTH_SHORT)

Example:
Toast.makeText(getApplicationContext(),toastValueTxt.getText(),Toast.LENGTH_LONG);

Once the toast initialized then we need to call show() method to display it.
Toast.makeText(getApplicationContext(),toastValueTxt.getText(),Toast.LENGTH_LONG).show();

MainActivity.Java:

[java]
package in.javadomain.androidtoast;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
Button displayToastButton;
EditText toastValueTxt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toastValueTxt = (EditText) findViewById(R.id.txtToastValue);
displayToastButton = (Button) findViewById(R.id.butDisplayToast);
displayToastButton.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view){
System.out.println(“Entered value is :::”+toastValueTxt);
Toast.makeText(getApplicationContext(),toastValueTxt.getText(),Toast.LENGTH_LONG).show();
}
});
}
}
[/java]

activity_main.xml:

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

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”www.Javadomain.in”
android:id=”@+id/textView”
android:layout_alignParentBottom=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”Android Toast Example”
android:id=”@+id/textView2″
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”71dp” />

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/txtToastValue”
android:layout_marginTop=”65dp”
android:width=”200dp”
android:layout_below=”@+id/textView2″
android:layout_centerHorizontal=”true” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Click me to display Toast”
android:id=”@+id/butDisplayToast”
android:layout_centerVertical=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>
[/xml]

AndroidManifest.xml:

[xml]
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”in.javadomain.androidtoast” >

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”in.javadomain.androidtoast.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>
[/xml]

Android Toast Example Output:

Android Toast Output
Android Toast Example Output

Leave a Reply