I have alert dialog and both PositiveButton and NegativeButton sited programmatically , i want to retrieve them from xml layout ,i try to do it but im new to android develop
You can use your dialog_layout
to retrieve your Buttons
as you did with your EditText
Button okBtn= (Button) dialog_layout.findViewById(R.id.btn_ok);
Button cancelBtn = (Button) dialog_layout.findViewById(R.id.btn_cancel);
then set you onClickListener
s
okBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// some code
}
});
cancelBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// some code
}
});
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/dialogButtonCancell"
android:layout_width="75dp"
android:layout_height="75dp">
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="80dp"
android:layout_height="70dp"/>
</RelativeLayout>
Java Code:
// custom dialog
final Dialog dialog = new Dialog(context);
//name of xml - custom
dialog.setContentView(R.layout.custom);
// set the custom dialog components - text, image and button
Button dialogButtonOK = (Button) dialog.findViewById(R.id.dialogButtonOK);
Button dialogButtonCancell = (Button) dialog.findViewById(R.id.dialogButtonCancell);
dialog.setTitle("Warning!!!!!");
// if button is clicked, call some method..
dialogButtonOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
someMethod();
}
});
dialogButtonCancell.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();