Skip to content Skip to sidebar Skip to footer

How To Create Custom Alert Dialog In Android?

I am developing a sample app.I am able to show alert on button click having some tittle and button .But now I want to show a pop up window having username (Label) and text field

Solution 1:

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><EditTextandroid:id="@+id/editText"android:hint="Enter some thing"android:layout_width="wrap_content"android:layout_height="wrap_content" ><requestFocus /></EditText><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/save"android:layout_marginTop="15dp"android:layout_weight="1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="SAVE" /><Buttonandroid:id="@+id/cancel"android:layout_marginTop="15dp"android:layout_weight="1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Cancel" /></LinearLayout></LinearLayout>

Need to inflate custom_dialog.xml

finalDialogdialog=newDialog(this);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Alert Dialog");

finalEditTexteditText= (EditText) dialog.findViewById(R.id.editText);
ButtonbtnSave= (Button) dialog.findViewById(R.id.save);
ButtonbtnCancel= (Button) dialog.findViewById(R.id.cancel);
dialog.show();

Solution 2:

You can make a normal activity and set the android:theme to dialog:

<activityandroid:name="com.example.carsharingkitdemo.YourPopUpActivity"android:theme="@android:style/Theme.Holo.Dialog" ></activity>

and in your xml file for this activity you can decide the height and width of this dialog field. For example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="300dp"android:layout_height="100dp">

The important thing is the manifest part.

Solution 3:

Solution 4:

you can use Dialog , like this code :

finalDialogdialog=newDialog(context);
// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");

// set the custom dialog components - text, image and buttonTextViewtext= (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ButtondialogButton= (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
dialog.dismiss();
}
});
dialog.show();

if you want to remove title bar just use this code after define :

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

Solution 5:

Try this...

AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setTitle("Reset...").setView(editText)
   .setMessage("R u sure?").setCancelable(true)
   .setPositiveButton("Yes", newDialogInterface.OnClickListener() {
    @OverridepublicvoidonClick(DialogInterface dialog, int arg1) {
        //Do here whatever.....
    }
});
AlertDialogalert= builder.create();
alert.show();
}

Post a Comment for "How To Create Custom Alert Dialog In Android?"