Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert the android resources int to a string. eg.: android.R.string.cancel?

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

thank you

like image 845
HugoXP Avatar asked Nov 14 '12 22:11

HugoXP


People also ask

How to convert resource to string Android?

Use this: String string = getActivity(). getString(android. R.

What is string resource in Android?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string. String Array.

What is string XML file in Android?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.

In which folder can you find the string resource file strings xml?

You can find strings. xml file inside res folder under values as shown below.


1 Answers

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.

like image 95
Sam Avatar answered Oct 06 '22 01:10

Sam