Toast is nothing but a small pop up or a feedback after an operation has performed in the android device. Say for example, if you perform a navigate from an email before you send it, it will trigger a "Draft Saved" toast message to know you that you can continue to edit later. Toast message will disappear after a few timeout seconds. This is a toast. It fills only the space which is required for that particular message and the current operation in the screen remains interactive and visible.
1. First create a Layout File named toast.xml
toast.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<TextView android:id="@+id/text"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#ffffff" >
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" />
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" />
</LinearLayout>
2. Add the below code to the main activity file of your project.
MainActivity.java
package com.udhaya.android;
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 MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
And that's it. Now play with toast messages in your android development projects.
No comments:
Post a Comment