How to convert int to drawable?
Asked Answered
S

4

11

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..

Stinnett answered 25/5, 2016 at 14:4 Comment(2)
imageId = position should be imageId = mThumbIds [position]Rocket
you can use parent.getItem(posiiton) and then set that as a drawableGrating
L
22

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.

Lazaro answered 25/5, 2016 at 14:8 Comment(1)
got it.. Thank you.. :-) @LazaroStinnett
B
5

The method getDrawable (int) is deprecated. Now you can easily use

    icon.setImageResource(imageId);
Broker answered 13/2, 2018 at 9:10 Comment(0)
D
4

In kotlin:

ContextCompat.getDrawable(context, resourceId)
Drisko answered 23/3, 2021 at 8:6 Comment(0)
E
0

this line

imageId = position;

should be like this

imageId = mThumbIds[position];
Emerson answered 25/5, 2016 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.