Android - Alert Dialog
Android - Alert Dialog
Home
Coding
HTML Ground
CSS Javascript SQL Python Java C C++
Jobs
Whiteboard
Android - Alert Dialog
Tools
Some times in your application, if you wanted to ask the user about taking a
decision between yes or no in response of any particular action taken by the
user, by remaining in the same activity and without changing the screen, you
can use Alert Dialog.
Now you have to set the positive (yes) or negative (no) button using the
object of the AlertDialogBuilder class. Its syntax is
alertDialogBuilder.setPositiveButton(CharSequence text,
DialogInterface.OnClickListener listener)
alertDialogBuilder.setNegativeButton(CharSequence text,
DialogInterface.OnClickListener listener)
Apart from this , you can use other functions provided by the builder class to
customize the alert dialog. These are listed below
setIcon(Drawable icon)
1
This method set the icon of the alert dialog box.
setMessage(CharSequence message)
3
This method sets the message to be displayed in the alert dialog
setOnCancelListener(DialogInterface.OnCancelListener
onCancelListener)
5
This method Sets the callback that will be called if the dialog is
cancelled.
setTitle(CharSequence title)
6
This method set the title to be appear in the dialog
After creating and setting the dialog builder , you will create an alert dialog
by calling the create() method of the builder class. Its syntax is
This will create the alert dialog and will show it on the screen.
Dialog fragment
Before enter into an example we should need to know dialog fragment.Dialog
fragment is a fragment which can show fragment in dialog box
List dialog
It has used to show list of items in a dialog box.For suppose, user need to
select a list of items or else need to click a item from multiple list of
items.At this situation we can use list dialog.
if (isChecked) {
// If the user checked the item, add it to the selected it
mSelectedItems.add(which);
}
else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
Example
The following example demonstrates the use of AlertDialog in android.
Steps Description
Run the application and choose a running android device and install
5
the application on it and verify the results.
package com.example.sairamkrishna.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
alertDialogBuilder.setNegativeButton("No",new DialogInterface.On
Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert Dialog"
android:id="@+id/textView"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorialspoint"
android:id="@+id/textView2"
android:textColor="#ff3eff0f"
android:textSize="35dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<ImageView
https://w w w .tutorialspoint.com/android/android_alert_dialoges.htm 7/13
25/11/2023, 10:43 Android - Alert Dialog
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert dialog"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_marginTop="42dp"
android:onClick="open"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />
</RelativeLayout>
Here is ofStrings.xml
<resources>
<string name="app_name">My Application</string>
</resources>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sairamkrishna.myapplication.MainAct
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your application. I assume you have connected your actual
Android Mobile device with your computer. To run the app from Android
studio, open one of your project's activity files and click Run
icon from the toolbar. Before starting your application, ]Android studio will
display following window to select an option where you want to run your
Android application.
Select your an option and then click on it. For suppose, if you have clicked on
yes button, then result would as follows
if you click on no button it will call finish() and it will close your application.
Get Started
Advertisements
AD
Affiliates eBooks
Contact Us
Tutorials Point India Private Limited, Incor9
Building, Kavuri Hills, Madhapur,
Hyderabad, Telangana - 500081, INDIA