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.
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
A simple dialog containing an DatePicker . This class was deprecated in API level 26.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With