1

I wanna know how to access strings & colors & dimens of xml files programmatically.

Is there any easy way to access it ???

5
  • the reason is I made TextView programmatically. Commented Aug 13, 2014 at 2:26
  • So you want to access the string from strings.xml rather than set it? Commented Aug 13, 2014 at 2:27
  • 1
    That's what I'm sayin ~ I need to change my question little bit. sorry Commented Aug 13, 2014 at 2:28
  • 1
    Provided the answer. Thanks for clarifying the question. Commented Aug 13, 2014 at 2:31
  • possible duplicate of Android: How do I get string from resources using its name? Commented Aug 13, 2014 at 4:09

2 Answers 2

2

You'll want Resources.

In an activity, you can use getResources() to retrieve that object, and then simply use

Resources res = getResources();
String string = res.getString( R.string.string_id );
int color = res.getColor( R.color.color_id )

etc.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use getResource of your activity to get the string, color ,etc from your values folder of your project.

sample:

String m_string = getResources().getString(R.string.app_name); //string xml
float m_dimension = getResources().getDimension(R.dimen.dimension); //dimens xml
int m_color = getResources().getColor(R.color.blue); //color xml

Comments

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.