37

How can I obtain the string value "cancel" from this resource int: android.R.string.cancel ?

thank you

1
  • It is late but I think what you are seeking is that you want to convert resource id to string so that you can use it for mapping or any other use cases. I suggest to give your id name to view tag for example android:tag="cancel" and then use it in code like myViewOrResource.getTag() in Java or myViewOrResource.tag in kotlin. tags are strings and can be used easily. Commented Oct 4, 2020 at 18:27

4 Answers 4

66

Simply use Context#getString():

String string = getString(android.R.string.cancel); 

I've already tried this approach but with no success... I've a class: public class MyDialogFragment extends DialogFragment {

A DialogFragment is not a subclass of Context, so you need to get access to a valid one (like your Activity's). Use this:

String string = getActivity().getString(android.R.string.cancel); 

Or as your discovered you can use the Activity passed in onAttach(), but understand you can do this anywhere inside a Fragment as long as you have a valid Context to work with.

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

2 Comments

I've already tried this approach but with no success... but now I know what I was doing wrong: I've a class: 'public class MyDialogFragment extends DialogFragment {' and inside this class I was doing: private String xbtAffirmativeLabel = getString(android.R.string.cancel) ; and this is the problem, I can only do that inside a method like: public void onAttach(Activity activity) { thank you for your support it helped me to realize that.
Why did you use android before R.string.cancel? Just getString(R.string.cancel) worked for me inside onCreateDialog in a dialog fragment in a class that extends android.support.v4.app.DialogFragment
8

As indicated here: http://developer.android.com/reference/android/content/Context.html#getString(int)

String s = context.getString(android.R.string.cancel); 

context can be the current activity, or any object inheriting the Context abstract class.

Comments

0

This will convert any Android resource into a string. In this example I’ve used an ‘R.color.myColor’ but it will work with any Android resource type.

colors.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="btnDialBgColor">#00BFA5</color> <color name="btnDialBgColorActive">#C51162</color> </resources> TypedValue typedValueActive = new TypedValue(); TypedValue typedValue = new TypedValue(); getResources().getValue(R.color.btnDialBgColorActive, typedValueActive, true); getResources().getValue(R.color.btnDialBgColor, typedValue, true); 

Hope this helps.

Comments

0

I know that's an old question, but it might help more people. What you can do is to call getIdentifier(). For that, you'll need to call it inserting after what value you want for a this variable, to make the string that you want for your resource ID. For exemple:

Your resources file:

R.string.cancel 

In java:

int resourceId = getResources().getIdentifier("cancel", "string", this.getPackageName()) println(getResources().getString(resourceId)); 

Then, in your rescourceId variable, you'll have an equivalent to: R.string.cancel. And, in println, you'll have the value correspondent of your resources string.

In Kotlin:

val resourceId = this.resources.getIdentifier("cancel", "string", this.packageName) println(resources.getString(resourceId)) 

With the same explanation that I said before.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.