Alert: It is a type of dialog box.
Toast: It is a message that holds on screen for a while<milliseconds>.
Code for Alert and Toast
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AlertandtoastActivity extends Activity {
Button b;
// it is used for create for create an alert that is by clicking on it. a dialog box i.e an alert will display on screen
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b =(Button)findViewById(R.id.button1);
}
public void show(View V)
// just right click on button in layout and on properties just select onclick and set show or any name on it.
{
AlertDialog.Builder ob = new AlertDialog.Builder(this);
//Alerdialog.builder is used to create an alert.
ob.setMessage("is ur entry is correct");
//setmessage just set your message
ob.setNeutralButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(AlertandtoastActivity.this, "ur correct", 5000).show();
//5000 means 5k milliseconds is used to hold a message on screen
}
});ob.setPositiveButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "plz re eneter", 7000).show();
//instead of getbasecontext we can use alerttoastactivity.this
}
});
ob.show();
}
}
xml file
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.11"
android:text="username" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:text="PAssword" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="166dp"
android:layout_height="34dp"
android:onClick="show"
android:text="LOGIN" />
</LinearLayout>
Toast: It is a message that holds on screen for a while<milliseconds>.
Code for Alert and Toast
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AlertandtoastActivity extends Activity {
Button b;
// it is used for create for create an alert that is by clicking on it. a dialog box i.e an alert will display on screen
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b =(Button)findViewById(R.id.button1);
}
public void show(View V)
// just right click on button in layout and on properties just select onclick and set show or any name on it.
{
AlertDialog.Builder ob = new AlertDialog.Builder(this);
//Alerdialog.builder is used to create an alert.
ob.setMessage("is ur entry is correct");
//setmessage just set your message
ob.setNeutralButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(AlertandtoastActivity.this, "ur correct", 5000).show();
//5000 means 5k milliseconds is used to hold a message on screen
}
});ob.setPositiveButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "plz re eneter", 7000).show();
//instead of getbasecontext we can use alerttoastactivity.this
}
});
ob.show();
}
}
xml file
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.11"
android:text="username" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:text="PAssword" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="166dp"
android:layout_height="34dp"
android:onClick="show"
android:text="LOGIN" />
</LinearLayout>
No comments:
Post a Comment