How can can I add custom buttons into an AlertDialog's layout?
Solution 1
The following code will inflate a view from R.layout.prompt
and set it to the AlertDialog. The positive
and negative
buttons will not be used. You can set the onClick
behaviors for btnAdd1
and btnAdd2
:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
EditText userInput = (EditText) promptView.findViewById(R.id.userInput);
Button btnAdd1 = (Button) promptView.findViewById(R.id.btnAdd1);
Button btnAdd2 = (Button) promptView.findViewById(R.id.btnAdd2);
btnAdd1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btnAdd1 has been clicked
}
});
btnAdd2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btnAdd2 has been clicked
}
});
alertD.setView(promptView);
alertD.show();
Solution 2
what you want to do is;
alertD.show();
Button button = (Button)promptView.findViewById(R.id.buttonId);
button.setOnClickListener(....)
using the view to call findViewById
, rather than the activity, which will look for the id in the layout that is being displayed.
Solution 3
According to this approach i am able to create the image button but if i want to dismiss or cancel dialog on Cancel button then what i have to do..
public static void alertDialogShow(final Context context,
final String resultMobile) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompt,
null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
userInput.setText(resultMobile);
userInput.setEnabled(false);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
Related videos on Youtube
Comments
-
Pepa Zapletal over 3 years
I have AlertDialog with Positive and Negative buttons. In AlertDialog layout I have EditText and two Buttons (btnAdd1, btnAdd2). I want when user click at the Button btnAdd1 or btnAdd2 add same text to EditText in AlertDialog (but no close AlertDialog). Is this possible do in AlertDialog or I have to use only Dialog?
This is layout (R.layout.prompt) of AlertDialog:
<LinearLayout> <EditText android:id="@+id/userInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" > <requestFocus /> </EditText> <Button android:id="@+id/btnAdd1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="bla" /> <Button android:id="@+id/btnAdd2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="bla" /> </LinearLayout>
And this is source code:
LayoutInflater layoutInflater = LayoutInflater.from(this); View promptView = layoutInflater.inflate(R.layout.prompt, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setView(promptView); alertDialogBuilder .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //... } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alertD = alertDialogBuilder.create(); alertD.show();
I want get acces to the btnAdd1 and btnAdd2 from the layout. Set the OnClickListener() to these two buttons.
-
Bhoomika Brahmbhatt over 10 yearswhat u have done so far?
-
Vikram over 10 yearsIt's unclear what you are asking.
-
Piyush over 10 yearsYes...it is Possible..Paste your code here.
-
Piyush over 10 yearswhere is your edittext and buttons declaration??
-
Pepa Zapletal over 10 yearsI add code, it´s clear right now?
-
-
Pepa Zapletal over 10 yearsthis is not excatly what i need. I pasted my code. Look at it please. I don´t want set a Positive or Negative button.
-
Pepa Zapletal over 10 yearsthis is not excatly what i need. I pasted my code. Look at it please. I don´t want set a Positive or Negative button (I have this Positive and Negative buttons already sets).
-
jcw over 10 years@Pepa, I have no idea, I tried to do this myself one time, I didn't manage it, but it looks logical to me to do
Button button = (Button)promptView.findViewById(R.id.buttonId);
and then set an on click listener after you have calledd.show()
. -
Pepa Zapletal over 10 yearsyes! this is it, I was looking for some complicated solution, but this is working, thx!