Today i want to explore how to create Toast in android. its nothing but like a notification to user some events is done.we can say it is a one type of pop up that display certain amount of time and automatically fade in and out.package android.widget.Toast; is allow to make toast.
In this android example,you learn how to create a Simple Toast in android:
CREATE NEW PROJECT
Step 1: Write code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast" />
</LinearLayout>
Step 2: Write a code into MainActivity.java
package dev.androidapplink.tosterdemoapp;
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 {
// create object
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Button is clicked", Toast.LENGTH_LONG).show();
}
});
}
}
For Custom Toast:
Step 3: Now Run Your Project:
In this android example,you learn how to create a Simple Toast in android:
CREATE NEW PROJECT
Step 1: Write code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast" />
</LinearLayout>
Step 2: Write a code into MainActivity.java
package dev.androidapplink.tosterdemoapp;
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 {
// create object
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Button is clicked", Toast.LENGTH_LONG).show();
}
});
}
}
For Custom Toast:
Step 3: Now Run Your Project:
No comments :
Post a Comment