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.
The following code will inflate a view from
R.layout.prompt
and set it to the AlertDialog. Thepositive
andnegative
buttons will not be used. You can set theonClick
behaviors forbtnAdd1
andbtnAdd2
:This is the way I did.
custom_alert_dialog.xml file created
In activity file
what you want to do is;
using the view to call
findViewById
, rather than the activity, which will look for the id in the layout that is being displayed.You could try something like this :
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..
My solution for your question.