Hello friends,
Today we are going to learn about alert box in android. So into this application we can consist alert dialog box with set alert title, alert icons and alert message.so let's take an example.
alert_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:onClick="alertDialog"
android:text="Show Alert" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void alertDialog(View view) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setTitle("TestApplication");
alertBuilder.setIcon(R.mipmap.ic_launcher);
alertBuilder.setCancelable(false);
alertBuilder.setMessage("Take a decision");
alertBuilder.setPositiveButton("Okay",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "You clicked okay button", Toast.LENGTH_LONG).show();
}
});
alertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertBuilder.create();
alertDialog.show();
}
}
Output:
Today we are going to learn about alert box in android. So into this application we can consist alert dialog box with set alert title, alert icons and alert message.so let's take an example.
alert_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:onClick="alertDialog"
android:text="Show Alert" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void alertDialog(View view) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setTitle("TestApplication");
alertBuilder.setIcon(R.mipmap.ic_launcher);
alertBuilder.setCancelable(false);
alertBuilder.setMessage("Take a decision");
alertBuilder.setPositiveButton("Okay",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "You clicked okay button", Toast.LENGTH_LONG).show();
}
});
alertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertBuilder.create();
alertDialog.show();
}
}
Output:
Comments
Post a Comment