android how to use string resource in a java class

后端 未结 4 1097
说谎
说谎 2020-12-29 20:04

In my java class I want to use a string resource from strings.xml.

for that I have to use like below,

getString(R.string.address)

i

4条回答
  •  忘掉有多难
    2020-12-29 20:27

    A class does not have a context and to use a string resource a context is needed. So just call the class from an activity and give a parameter context and within your class constructor just use that context to get the string resource.

    In your custom class you need to import the R namespace for the project to get the resource Id.

    import com.myrandomapp.R;
    

    Then to get the actual string

    context.getString(R.string.COOL_STRING)
    

提交回复
热议问题