I am just starting to work with dialog fragment and there is a lot that I don't know. I have a MainActivity that by clicking on a button opens a DialogFragment, in that DialogFragment I have another button that opens a SecondDialogFragment. The first one works fine but the second one not, I click on the button in the first DialogFragment the screen lose focus with the background but shows nothing. I really don't know what is wrong? I would be grateful if someone could give me a hand.
This is the first DialogFragment
where I call the second one by onClick
.
DialogFragment.java
ImageButton iconButton = v.findViewById(R.id.user_icon);
iconButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DSelectIcon().show(getFragmentManager(), "DSelectIcon");
}
});
SecondDialogFragment.java
public class DSelectIcon extends DialogFragment{
private View v = null;
private ImageView Selection;
private static final Integer[] items = { R.drawable.image1,
R.drawable.image1, R.drawable.image1,
R.drawable.image1, R.drawable.image1,
R.drawable.image1, R.drawable.image1,
R.drawable.image1 };
public DSelectIcon() {
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (savedInstanceState != null) {
// Restore last state for checked position.
}
LayoutInflater inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.grid_icon_event, null);
return createDSelectIcon(v);
}
private AlertDialog createDSelectIcon(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Selection = v.findViewById(R.id.selection);
GridView grid = v.findViewById(R.id.grid);
// grid.setAdapter(new ArrayAdapter<Integer>(this, R.layout.cell,
// items));
grid.setAdapter(new CustomGridAdapter((MainActivity)getActivity(), items));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Clicked postion is" + i,
Toast.LENGTH_LONG).show();
//Selection.setImageResource(items[arg2]);
}
});
return builder.create();
}
public class CustomGridAdapter extends BaseAdapter {
private Activity mContext;
// Keep all Images in array
public Integer[] mThumbIds;
// Constructor
public CustomGridAdapter(MainActivity mainActivity, Integer[] items) {
this.mContext = mainActivity;
this.mThumbIds = items;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
}
Grid_icon_event.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dip"
android:gravity="center"
android:horizontalSpacing="5dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="40dip" >
</GridView>
</LinearLayout>
FIXED!!!
A stupid mistake, but I forgot to add this:
builder.setView(v);
In the SecondDialogFragment.java method createDSelectIcon
Now it looks like this:
public class DSelectIcon extends DialogFragment{
private View v = null;
private ImageView Selection;
private static final Integer[] items = { R.drawable.image1,
R.drawable.image1, R.drawable.image1,
R.drawable.image1, R.drawable.image1,
R.drawable.image1, R.drawable.image1,
R.drawable.image1 };
public DSelectIcon() {
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (savedInstanceState != null) {
// Restore last state for checked position.
}
LayoutInflater inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.grid_icon_event, null);
return createDSelectIcon(v);
}
private AlertDialog createDSelectIcon(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Selection = v.findViewById(R.id.selection);
GridView grid = v.findViewById(R.id.grid);
// grid.setAdapter(new ArrayAdapter<Integer>(this, R.layout.cell,
// items));
grid.setAdapter(new CustomGridAdapter((MainActivity)getActivity(), items));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Clicked postion is" + i,
Toast.LENGTH_LONG).show();
//Selection.setImageResource(items[arg2]);
}
});
builder.setView(v);
return builder.create();
}
public class CustomGridAdapter extends BaseAdapter {
private Activity mContext;
// Keep all Images in array
public Integer[] mThumbIds;
// Constructor
public CustomGridAdapter(MainActivity mainActivity, Integer[] items) {
this.mContext = mainActivity;
this.mThumbIds = items;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
}