You have to call
builderNegative.setView(dialoglayout)
before you build the dialog
final AlertDialog alertNegative = builderNegative.create();
After you change the order, it will work!
Question
I hope someone can help me, I'm trying to display a custom dialog but when I click on the button that calls this method, the gray screen appears, as if the custom dialog was showed but do not show anything of the xml.
The problem is that few days ago it works but i have not touch nothing and now it doesn't works. I've tried to clean the project and buld it, remove the app and install again. Test the app to see if some object is null, but shows like if works but doesnt show the xml.
JavaFile
private void NegativeAlertDialog()
{
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.my_alert_dialog, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builderNegative = new AlertDialog.Builder(this);
builderNegative.setCancelable(false);
final AlertDialog alertNegative = builderNegative.create();
Button buttonSaturation = (Button)dialoglayout.findViewById(R.id.buttonSaturation);
buttonSaturation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertNegative.dismiss();
sendRefuseServiceOptionSaturation(Global.urlServer);
}
});
Button buttonNoZone = (Button)dialoglayout.findViewById(R.id.buttonNoZone);
buttonNoZone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertNegative.dismiss();
sendRefuseServiceOptionNotMyArea(Global.urlServer);
}
});
Button buttonLiquidity = (Button)dialoglayout.findViewById(R.id.buttonLiquidity);
buttonLiquidity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertNegative.dismiss();
sendRefuseServiceOptionLiquidityProblems(Global.urlServer);
}
});
Button buttonMyAlertDialogCancel = (Button)dialoglayout.findViewById(R.id.buttonMyAlertDialogCancel);
buttonMyAlertDialogCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertNegative.cancel();
rowSeleceted = -1;
}
});
builderNegative.setView(dialoglayout);
alertNegative.show();
}
xml.file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonSaturation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/RefuseAssigned1" />
<Button
android:id="@+id/buttonNoZone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/RefuseAssigned2" />
<Button
android:id="@+id/buttonLiquidity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/RefuseAssigned3" />
<Button
android:id="@+id/buttonMyAlertDialogCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/RefuseAssigned4" />
</LinearLayout>
Solution
You have to call
builderNegative.setView(dialoglayout)
before you build the dialog
final AlertDialog alertNegative = builderNegative.create();
After you change the order, it will work!
OTHER TIPS
You have not added the View
to your dialog. you can do it by calling the following method.
builderNegative.setView(dialoglayout);
note that you can inflate the layout with null.