• 5

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

android how to use string resource in a java class

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

for that I have to use like below,


if my class is an activity then its taking. But my class is a simple java class , how can I use there?

Is it possible? Thank you

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

  • 46
Reply Report
      • 1
    • This answer is incomplete - To complete for those who still have questions, you need to import the R namespace for the project to get the resource Id. example) import com.myappdomain.R (USE YOUR NAMESPACE HERE) Then to get the actual string context.getString(R.string.SOME_STRING)

You can pass the context of the Activity class to the java class and access the resources.

From your Activity Class

 Helper helper = new Helper(this);

Your Java class

public class Helper {

    Helper(Context c){
  • 17
Reply Report

You can create a static Application Context in your custom Application class

public class App extends Application{

    private static Context mContext;

    public void onCreate() {
        mContext = getApplicationContext();

    public static Context getContext(){
        return mContext;

Then you just need to call App.getContext().getResources() to get any resource values.

Just remember that this Context is Application type, so there are things that this Context is not good to use. Read this for further info.

  • 13
Reply Report