Marcin S.
Marcin S.

Reputation: 11191

How can I get a name of the image having its id?

I have an array of int. Those integers are the resource id's for the pictures:

final int flags[] = new int[] { R.drawable.argentina, R.drawable.austria ... }

and a String that holds the name of the image resource:

String flag = "R.drawable.argentina";

How can I easy check whether flag String is in flags array or not?

When I retrieve a value from the array and assign it the variable for example:

int flag = flag[0];

This flag variable will hold some integer like 2130837665 which is obvious. How can I retrieve exact name from this array as a String that way I can compare it to the flag String?

Upvotes: 0

Views: 248

Answers (2)

Vijay Rajput
Vijay Rajput

Reputation: 1091

use this code

public class MainActivity extends Activity {

Button MyTranslateButton;   
private int  image[];   
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        
  image =new int[]{R.drawable.adv_book,R.drawable.background,R.drawable.bid_active,R.drawable.break_finish};     


    MyTranslateButton = (Button)findViewById(R.id.TranslateButton);

    MyTranslateButton.setOnClickListener(MyTranslateButtonOnClickListener);
}

private Button.OnClickListener MyTranslateButtonOnClickListener
  = new Button.OnClickListener(){
@SuppressWarnings("null")
public void onClick(View v) {
     for(int i=0 ; i < image.length ; i++){     
         String names[]=new String[image.length];
         String name=getResources().getResourceEntryName(image[i]);         
        Log.i("Image Names is ",name);
        names[i]=getResources().getResourceEntryName(image[i]);

      }

}

};

}

Upvotes: 1

nandeesh
nandeesh

Reputation: 24820

You can get the name of the resource by using

   String name = getResources().getResourceEntryName(flag]);

This gives name only, argentina in your case

You can get the type by

String type = getResources().getResourceTypeName(flag);

This will return drawable in your case. So you can find whether the id corresponds to the string

Upvotes: 2

Related Questions