0

I have a string variable which is the same as the identifier for a view.

I would like to use the R.id.xxx, but obviously I can't just replace the xxx with my string as the xxx is actually an int. What can I do to get around this?

Thanks!

2 Answers 2

1

Nonsense. The framework obviously discourages that approach but it is possible: just use Java reflection, e.g.,

 java.lang.reflect.Field f = R.id.class.getField(name);
 int id = f == null ? -1 : (Integer)f.get(null);
 // now just use findViewById(id);
Sign up to request clarification or add additional context in comments.

Comments

0
String s=getString(R.string.xxx);

5 Comments

Sorry this isn't quite what I meant. I am trying to retrieve a handle to a LinearLayout. Usually I would use findViewById(R.id.idname). I have idname as a string - how can I use the findViewById method as this takes an int?
can you post your xml?what is there and what you want from there
In my xml there is <LinearLayout android:id="@+id/t112" android:padding="3dip"></LinearLayout> and I have a string variable: String str = "t112" I want to use this variable to retrieve a handle to the view it is the id of
Sounds like you want to replace a variable name.This is not possible
findViewById() takes integer as parameter and the integer parameter is determined by android system.How you can refer with other variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.