custom dialog box in Android

To customize the dialog box, you can follow the below steps.

call the below method in any listener:

code to keep in class:

protected AlertDialog onClickLogin() {
// This example shows how to add a custom layout to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(CustomDialogOneActivity.this)
// .setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(“Snapshot”)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked cancel so do some stuff */
}
})
.show();
}

/********************************************/

layout code :

alert_dialog_text_entry.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent” android:layout_height=”wrap_content”
android:orientation=”vertical”>
<LinearLayout android:layout_width=”fill_parent”
android:layout_height=”wrap_content” android:layout_gravity=”top|center”
android:paddingTop=”10dip” android:paddingRight=”40dip”>

<TableLayout android:layout_width=”fill_parent”
android:layout_height=”wrap_content” android:stretchColumns=”1″
android:layout_gravity=”center”>
<TableRow>
<TextView android:layout_width=”wrap_content”
android:layout_height=”wrap_content” android:text=”TextView”
android:gravity=”center” android:paddingLeft=”25dip” />

<EditText android:id=”@+id/exception_reason_button”
android:layout_width=”wrap_content” android:layout_height=”wrap_content”
android:text=”” android:onClick=”onClickReason” />
</TableRow>
<TableRow>
<TextView android:layout_width=”wrap_content”
android:layout_height=”wrap_content” android:text=”Button”
android:gravity=”center” android:paddingLeft=”25dip” />

<Button android:id=”@+id/exception_reason_button”
android:layout_width=”wrap_content” android:layout_height=”wrap_content”
android:text=”Button” android:onClick=”onClickReason” />
</TableRow>
</TableLayout>

</LinearLayout>
<LinearLayout android:layout_width=”wrap_content”
android:orientation=”horizontal”
android:layout_height=”wrap_content” android:layout_gravity=”center|bottom” android:gravity=”center”>

<ImageView android:id=”@+id/camera_button” android:src=”@drawable/android”
android:layout_width=”wrap_content” android:layout_weight=”1″
android:layout_height=”wrap_content” android:onClick=”onClickImageOne”
android:layout_gravity=”center” android:gravity=”center”
android:focusable=”true” android:padding=”30dip” />

<ImageView android:id=”@+id/camera_button” android:src=”@drawable/android”
android:layout_width=”wrap_content” android:layout_weight=”1″
android:layout_height=”wrap_content” android:onClick=”onClickImageTwo”
android:layout_gravity=”center” android:gravity=”center”
android:focusable=”true” android:padding=”30dip” />

</LinearLayout>
</LinearLayout>

Advertisements

1 Comment

Filed under Uncategorized

One response to “custom dialog box in Android

  1. juro

    hi, how can i make button “@+id/exception_reason_button” working? with alertdialog i can’t do:

    button= (Button) dialog.findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {} });

    but without dialog.findViewById it throws me nullpointer on setOnClickListener. =/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s