-3

I had try somethimg like this I had seen example have its drawable name and find resource id ..Now I don't know drawable name I just have to find it from imgresourceId

This is my Image Resource Id

int id = Integer.parseInt(data.getStringExtra("imgId"));

05-30 12:09:33.648: D/imgid(5461): 2130837579

I want to set this to drawable right of a edittext

So I had done something like this

ed_operator.setCompoundDrawablesRelativeWithIntrinsicBounds( null, null, id, null );

3rd parameter is expecting drawable so how to convert this image resource id to drawable

is there any alternative solution which I can apply....

Divya Jain
  • 393
  • 1
  • 6
  • 22
Tufan
  • 2,789
  • 4
  • 34
  • 52

2 Answers2

0

To get an image from resources, and you know its id , you do the following in your Activity:

// Get the id.
int id = Integer.parseInt(data.getStringExtra("imgId"));
// Get the drawable from the id.
Drawable d = getResources().getDrawable(id);
Bill Tsagkas
  • 530
  • 1
  • 4
  • 15
  • check my question i know its resource id ..how to convert this to drawables – Tufan May 30 '15 at 06:27
  • If you know the resource id and you want to get the drawable from the resource id, this is the way. You have to clarify a bit. – Bill Tsagkas May 30 '15 at 06:31
  • The correct usage of setCompoundDrawablesRelative is setCompoundDrawablesRelative(Drawable, Drawable, Drawable, Drawable) and not setCompoundDrawablesRelative(int, int, int, int). So you have to get the drawable first from the id and then set it to Edit Text – Bill Tsagkas May 30 '15 at 06:39
  • this is my resource id 05-30 12:09:33.648: D/imgid(5461): 2130837579 – Tufan May 30 '15 at 06:40
  • this is my img resource id 05-30 12:09:33.648: D/imgid(5461): 2130837579 how to convert this to drawable – Tufan May 30 '15 at 06:41
  • now its fine it convert imgid to drawable but can you tell me why this drawable is not displayed in ed_operator.setCompoundDrawablesRelativeWithIntrinsicBounds( null, null, id, null ); as right drawable – Tufan May 30 '15 at 07:02
  • Maybe use the "wrap_content" attribute in EditText. – Bill Tsagkas May 30 '15 at 07:10
  • thats nt psble hey i got its name something like this String name = getApplicationContext().getResources().getResourceEntryName(id); can you tell me hot to convert this to drawable image – Tufan May 30 '15 at 07:11
  • i already have a right drawable for edittext ..problem is this how can i replace this with my drawable..it is not replacing – Tufan May 30 '15 at 07:23
0

hey there you are putting resource ID (intenger) as extra. and getting with string extra. make it as

        int resID =  data.getIntExtra("imgId",-1);
        if(resID != -1 ){
          Drawable img = getResources().getDrawable( resID );
          img.setBounds( 0, 0, 60, 60 );

          ed_operator.setCompoundDrawablesRelativeWithIntrinsicBounds( null, null, img, null );//if you dont have images already in drawable 
          //OR
          ed_operator.setCompoundDrawables( null, null,img,null);//if you had set images in your drawable
        }
shobhan
  • 1,460
  • 2
  • 14
  • 28
  • nope dude ..i had tried everything is fine but drawable is not set to drawable right ...although it shows get drawable is depricated – Tufan May 30 '15 at 07:19
  • i already have a right drawable for edittext ..problem is this how can i replace this with my drawable..it is not replacing – Tufan May 30 '15 at 07:23
  • i had check it but nthng hpnd is there anyway to replace exisiting image or remove image from right drawable ..so 1st i remove existing than i can set – Tufan May 30 '15 at 07:40
  • i had edited your code review it..so i can accept it as answer – Tufan May 30 '15 at 08:09
  • 1
    ed_operator.setCompoundDrawables( null, null,img,null); you can apply this if you have an already drawable in your edittext – Tufan May 30 '15 at 08:26