In Android, Toast is a notification message that pop up, display a certain amount of time.
The toast message automatically fades in and out on the surface of the current screen and only fills the amount of space required for the message. A toast message does not accept user input. The user’s current activity remains visible and interactive when a toast message pops up. The toast message can appear even if the application isn’t visible.
In this tutorial we will show how to create a simple Toast message :
File : res/layout/activity_main.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=".MainActivity" >
<TextView
android:id="@+id/txt_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="22dp"
android:text="@string/Value" />
<EditText
android:id="@+id/edt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt_value"
android:layout_below="@+id/txt_value"
android:layout_marginTop="18dp"
android:ems="10" />
<Button
android:id="@+id/btn_click"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/edt_name"
android:layout_toRightOf="@+id/edt_name"
android:text="@string/btn_value" />
</RelativeLayout>
File : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyFirstApp</string>
<string name="hello_world">My First Android App</string>
<string name="menu_settings">Settings</string>
<string name="Value">Enter your name</string>
<string name="btn_value">Click</string>
</resources>
File : MainActivity.java
package com.example.toastexample;
import com.example.myfirstapp.R;
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 {
private EditText edtName;
private Button btnCick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
this.btnCick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String edtValue = edtName.getText().toString();
showToast(edtValue);
}
});
}
private void initialize() {
this.edtName = (EditText) findViewById(R.id.edt_name);
this.btnCick = (Button) findViewById(R.id.btn_click);
}
private void showToast(String name) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hai " + name,
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.activity_main, menu);
return true;
}
}
See demo, when a button is clicked, display a Toast message.
The toast message automatically fades in and out on the surface of the current screen and only fills the amount of space required for the message. A toast message does not accept user input. The user’s current activity remains visible and interactive when a toast message pops up. The toast message can appear even if the application isn’t visible.
In this tutorial we will show how to create a simple Toast message :
File : res/layout/activity_main.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=".MainActivity" >
<TextView
android:id="@+id/txt_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="22dp"
android:text="@string/Value" />
<EditText
android:id="@+id/edt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt_value"
android:layout_below="@+id/txt_value"
android:layout_marginTop="18dp"
android:ems="10" />
<Button
android:id="@+id/btn_click"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/edt_name"
android:layout_toRightOf="@+id/edt_name"
android:text="@string/btn_value" />
</RelativeLayout>
File : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyFirstApp</string>
<string name="hello_world">My First Android App</string>
<string name="menu_settings">Settings</string>
<string name="Value">Enter your name</string>
<string name="btn_value">Click</string>
</resources>
File : MainActivity.java
package com.example.toastexample;
import com.example.myfirstapp.R;
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 {
private EditText edtName;
private Button btnCick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
this.btnCick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String edtValue = edtName.getText().toString();
showToast(edtValue);
}
});
}
private void initialize() {
this.edtName = (EditText) findViewById(R.id.edt_name);
this.btnCick = (Button) findViewById(R.id.btn_click);
}
private void showToast(String name) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hai " + name,
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.activity_main, menu);
return true;
}
}
See demo, when a button is clicked, display a Toast message.
0 comments:
Post a Comment