-5

I have one edittext and button which passes the editText text to the other activity onClick when it comes to second activity it is receiving the text from main activity.

My question is, I have more than 20 String names in String resource folder, I have to open the particular string which matches the edittext value of main activity. In short, I have to change the textview text which matches string resource String

<resources>
        <string name="app_name">sylb</string>
        <string name="title_activity_display">display</string>
        <string name="large_text">some_large_text</string>
        <string name="ds15mca21">second mca</string>
        <string name="ds15mca11">first mca</string>
        <string name="ds15mba21">second <b>mba</b>></string>
        <string name="ds15aut21">second <i>automobile</i></string>
        <string name="action_settings">Settings</string>
</resources>

display code

public class display extends AppCompatActivity {
        String code;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_display);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();
            code= extras.getString("keyName");
            TextView textView= (TextView) findViewById(R.id.textapp);

            textView.setText("R.string." + "ds" + code);
        }
    }
Charuක
  • 12,953
  • 5
  • 50
  • 88
Ravi Kiran
  • 1
  • 1
  • 2
  • textView.setText("R.string."+"ds"+code); this line is showing the normal text as r.string.ds – Ravi Kiran Feb 23 '17 at 03:36
  • 1
    if you want to set String value from `Strings` file you should use my answer! So, What do want to do exactly? please explain more! – Atef Hares Feb 23 '17 at 03:49
  • instead of passing `code` as extra string parameter. pass the value of `R.string.dsXYZ` as extra int resource. Then get that int in your other activity and use `textView.setText(resInt);` – Mohammed Atif Feb 23 '17 at 04:12

6 Answers6

0

Use the below code!

getResources().getString(resId)//resId is R.string.STRING_NAME
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
0

you can try this

textView.setText(getResources().getIdentifier("R.string." + "ds" + code,"string",getPackageName()));
Xesygao
  • 1
  • 3
  • 4
    On stack overflow one usually adds a few words of explanation, not a mere line of code... – mkl Feb 23 '17 at 08:53
0

This will do your job! Just pass correct String that you fixed instead of "app_name" (don't use R.string just the resource name)

Example: If I want to get the value of

 <string name="app_name">ItsCharuHereHiFolks</string>

you need to do this

 final TextView textView = (TextView) findViewById(R.id.your_text_view);
 String string = getString(getResId("app_name", R.string.class));
 textView.setText(string);

and

   public static int getResId(String resName, Class<?> c) {

        try {
            Field idField = c.getDeclaredField(resName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • So you suggest him to pass the name of string resource, then calculate the string id from it, then again get the value of actual string, then assign it to textView? seriously? Either pass the actual string which belongs to that string resource or directly pass the resId as extra parameter corresponding to that string resource. – Mohammed Atif Feb 23 '17 at 04:15
  • @Mohammed Atif his requirement is to get the string value using the name of of string resource.Read the question and thats what he trying to do using "R.string." + "ds" – Charuක Feb 23 '17 at 04:19
  • he is a beginner, he might not know better ways of doing a simple task. that doesn't mean you answer his question as is instead of helping him find better solution. – Mohammed Atif Feb 23 '17 at 04:21
  • 1
    @Mohammed Atif may getIdentifier() is a general tool and as a result, its slow. Running through n number of iterations comparing to that own R.java is better in performance and you need some learning , dont compare method with the number of lines they have! – Charuක Feb 23 '17 at 04:28
  • 1
    Here is something for you to read http://daniel-codes.blogspot.com/2009/12/dynamically-retrieving-resources-in.html – Charuක Feb 23 '17 at 04:34
  • I am pretty sure you did not understand what i actually meant. I too know that getting identifier id via identifier name is costly task. But getting identifier id using `R.string.stringName` is much faster for string specific task when compared to your generic task. – Mohammed Atif Feb 23 '17 at 06:18
  • @Mohammed Atif I got it,but for that OP needs to know that ID like R.string.dsAbcXyz, if not this way will be better, I suggested it because OP seems to struggle with passing res id using some Strings :) – Charuක Feb 23 '17 at 07:03
0

Its really easy. use this in your class

private String getStringResourceByName(String aString) {
      String packageName = getPackageName();
      int resId = getResources().getIdentifier(aString, "string", packageName);
      return getString(resId);
    }

and simply use this

textView.setText(getStringResourceByName("ds" + code));

Or you can simply use the above class and extract any strings.

Example

  • If you want questions_en from below resource

    <string name="questions_en">Questions</string>

Use this

String question=getStringResourceByName("questions_en");

Moreover use integer instead of string in getStringResourceByName method to get interger resourse.

getIdentifier (String name, String defType, String defPackage) do the job!!

Credits.

Community
  • 1
  • 1
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
0

While @Charu's answer is good and efficient enough to perform generic tasks where id's belong to different resource types, you can still have a better solution if you know the string resource name and your task is only related to Strings.

Step-1

in your calling Activity, use the following code.

Intent intent = new Intent(this, newActivity.class);
intent.putExtra("StringResourceId", R.string.dsAbcXyz); //this step will directly pass the int value of your string resource without any iterations
startActivity(intent);

Step-2

Then in your newActivity, use the following code.

Intent prevIntent = getIntent();
int resId = prevIntent.getIntExtra("StringResourceId");
textView.setText(resId); //you can directly pass the int id to textview to display the relevant text

This approach can work when you are pretty sure about the resourxe name to pass as parameter.

But if you are getting your res name through random sources like Server or Databases, then I suggest you to follow @Charu's solution.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
-1

use this

getResources().getIdentifier("ds" + code, "String", getPackageName());
Ali Bagheri
  • 3,068
  • 27
  • 28