I have created a dialog with list of icons, i.e drawables. I have created a grid view to show these icons.
Now I want to get the selected icon and set it to an image view in my activity.
So how can I convert this into drawable so that I can set the selected icon on my activity's image view?
dialog code:
private void showAlertDialog() {
GridView gridView = new GridView(this);
gridView.setGravity(Gravity.CENTER);
gridView.setPadding(0,20,0,20);
int[] mThumbIds = {
R.drawable.roundicons05,R.drawable.roundicons08,R.drawable.roundicons02,R.drawable.roundicons03,R.drawable.roundicons04,
R.drawable.roundicons16,R.drawable.roundicons37,R.drawable.roundicons06,R.drawable.roundicons07,R.drawable.roundicons05,
R.drawable.roundicons09,R.drawable.roundicons10,R.drawable.roundicons11,R.drawable.roundicons12,R.drawable.roundicons13,
R.drawable.roundicons14,R.drawable.roundicons15,R.drawable.roundicons16,R.drawable.roundicons17,R.drawable.roundicons18,
R.drawable.roundicons19,R.drawable.roundicons20,R.drawable.roundicons22,
};
gridView.setAdapter(new ImageAdapter(CheckListActivity.this,mThumbIds));
gridView.setNumColumns(4);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do something here
// icon.setImageDrawable(id);
imageId = position;
Drawable drawable = getResources().getDrawable(imageId);
icon.setImageDrawable(drawable);
}
});
final MaterialDialog dialog = new MaterialDialog.Builder(CheckListActivity.this)
.customView(gridView, false)
.title("Select Icon")
.negativeText("CANCEL")
.canceledOnTouchOutside(true)
.build();
dialog.show();
}
I tried like this
imageId = position;
Drawable drawable = getResources().getDrawable(imageId);
icon.setImageDrawable(drawable);
but this throws no resource found exception.
Thank you..
instead of using position
as ResourceID (it is not), rather get an item of the mThumbIds
array at the positions
. So your code should look like this:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do something here
imageId = mThumbIds[position];
Drawable drawable = getResources().getDrawable(imageId);
icon.setImageDrawable(drawable);
}
I hope this helps.