Thursday, 3 May 2018

Android custom alert dialogue examples

Custom alert dialog is small dialog which prompt to the user screen for the take a decision or show some information to the users.

Using custom alert dialog use when you ask a user to in yes or no that time you can use.

Example : Exit confiscation , Rename username , for review application.

Alert Dialogue methods and descriptions :


setIcon(Drawable icon) : This method set the icon of the alert dialog box.
setCancelable(boolean cancel able) :This method sets the property that the dialog can be cancelled or not
setMessage(CharSequence message) :This method sets the message to be displayed in the alert dialog
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener) :This method sets list of items to be displayed in the dialog as the content. The selected option will be notified by the listener
setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) : This method Sets the callback that will be called if the dialog is cancelled.
setTitle(CharSequence title) :This method set the title to be appear in the dialog
Implementation of Custom alert dialog in android

dialog_exit.xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"    >
    <TextView        android:id="@+id/tv_title"        android:layout_width="300dp"        android:layout_height="50dp"        android:text="Exit Confirmation"        android:textSize="20dp"        android:gravity="center"        android:textColor="#ffffff"        android:background="@android:color/holo_red_dark"        />
    <TextView        android:layout_width="300dp"        android:layout_height="140dp"        android:text="Are you sure to Exit this application"        android:textSize="20dp"        android:gravity="center"        android:padding="5dp"        />
    <LinearLayout        android:layout_height="wrap_content"        android:layout_width="match_parent"        android:orientation="horizontal"        android:gravity="center"        >
        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            style="?attr/borderlessButtonStyle"            android:id="@+id/no"            android:text="No"            android:textSize="18dp"
            />
        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            style="?attr/borderlessButtonStyle"            android:id="@+id/yes"            android:text="YEs"            android:textSize="18dp"            />
    </LinearLayout>
</LinearLayout>




MainActivity.java
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_exit);
dialog.setTitle("Pick Background Image");

Button yes=(Button)dialog.findViewById(R.id.yes);
Button no=(Button)dialog.findViewById(R.id.no);

yes.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
        finish();
    }
});
no.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
        dialog.dismiss();
    }
});

dialog.show();
Output :
dialogue examples




Share This

0 comments: