Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: pass a resource id as a parameter

I have this code:

translateRight("iv1"); //iv1 is an id of an imageView

    private void translateRight(String st){

    ImageView img = (ImageView)findViewById(R.id.st);
        Animation a = AnimationUtils.loadAnimation(this, R.anim.translate_right);
        img.startAnimation(a);
    }

I have "translateRight" that is a method that show an animation, but at this method I should pass a resource id; I tried with a string but it don't work, what can I do?

like image 394
cyclingIsBetter Avatar asked Sep 03 '12 13:09

cyclingIsBetter


People also ask

What is the use of resources ID in android?

drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.

What is getDrawable?

A drawable resource is a common concept for a graphic that can be drawn to the screen and which one can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon.06-Mar-2022.

How does android studio define ID?

For example, when you specify an ID to an XML resource using the plus sign — in the format android:id="@+id/myView" —it means that a new ID resource with the name “myView” needs to be created, and if it does not exist, create a unique integer for it and add to R. java .


2 Answers

Resource id - is an integer value. Just pass an int:

private void translateRight(int resource){
    ImageView img = (ImageView) findViewById(resource);
    //stuff
}
like image 132
Dmitry Zaytsev Avatar answered Oct 21 '22 03:10

Dmitry Zaytsev


Resource ID is an integer, not a String.

like image 22
easycheese Avatar answered Oct 21 '22 03:10

easycheese