Reputation: 610
In order to make my app as much low maintenance as possible I've created a class Cl_dialog, that is supposed to manage the creation of each Dialog of my app:
public class Cl_Dialog
{
private Activity activity;
private AlertDialog alertDialog;
private AlertDialog.Builder builder;
private View layout;
public Cl_Dialog( Activity act )
{
this.activity = act;
builder = new AlertDialog.Builder( act );
alertDialog = builder.create();
}
public void dialogShowDatePicker()
{
setContentView( R.layout.dialog_datepicker);
( (ImageButton) layout.findViewById( R.id.btn_confirm ) ).setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick( View view )
{
close();
}
});
}
private void setContentView( int idLayout)
{
LayoutInflater inflater = (LayoutInflater) activity.getSystemService( activity.LAYOUT_INFLATER_SERVICE );
layout = inflater.inflate( idLayout, (ViewGroup ) activity.findViewById( R.id.mainLayout ) );
builder.setView( layout );
builder.create();
}
public void show()
{
alertDialog.show();
}
public void close()
{
alertDialog.dismiss();
}
}
The class is called sometimes by Fragment
sometimes by Activity
.
This is an example of layout.xml
I use:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:layout_width="55dp"
android:layout_height="35dp"
android:src="@drawable/ic_navigation_check"
android:layout_margin="15dp"
android:id="@+id/btn_confirm"
android:background="@color/colorPrimary"/>
</LinearLayout>
And here an example of how I call it:
cl_dialog = new Cl_Dialog(activity.this);
cl_dialog.dialogShowDatePicker();
cl_dialog.show();
Any clue about where I am wrong in the code?
Anyway I am an old programmer and I am trying to arrange old code I wrote year ago, is still this the suggested way to create a custom dialog in android?
Thank you!
Upvotes: 1
Views: 60
Reputation: 440
In setContentView
you call builder.create()
without setting the result for the alertDialog
Upvotes: 2