0

"If the value in textView1 is equal string name I want string to show textView2" // String Example

<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>

//Example if textView1 = a textView2 Will Show Apple

2 Answers2

0

Here you have stringName as a text in your textView1. You can fetch String value programatically from resourses by stringName.

Step1 : first fetch this both values, and store it in a variable;

String mTvTextStrName = textView1.getText().toString().trim();
String strValue = getStringResourceByName(mTvTextStrName);

use this method to fetch String value.

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

You can also check this values via Log.e.

Step2 : Now set your strValue in your textView2.

// Do your code.. Show Apple
   textView2.setText(strValue);
Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30
  • Thank you for your comment and teaching. But error show like that Cannot resolve method 'getString' in 'TextView'. I do not know much yet because I am beginner. – aungban service Jun 14 '22 at 07:05
  • i edited my answer, check it again. need to fetch text strings like this : textView1.getText().toString().trim() – Bhoomika Patel Jun 14 '22 at 07:06
  • Thank you so much. The first code you provided is fine. I'm sorry for the inconvenience. Today is the happiest day to learn android code. Thank you. Forgive me for saying the wrong thing. Because I use Google Translate. – aungban service Jun 14 '22 at 07:28
  • glad that this helps, please accept answer so it can also helpful to others. – Bhoomika Patel Jun 14 '22 at 07:29
0

Try using getIdentifier() method of getResources() like below.

    TextView textView1= findViewById(R.id.textView1);
    TextView textView2= findViewById(R.id.textView2);
    String textViewText=getResources().getString(getResources().getIdentifier(textView1.getText().toString(), "string", getPackageName()));
    textView2.setText(textViewText);

Referenced from https://stackoverflow.com/a/17086690/9502601

Shayan Samad
  • 294
  • 1
  • 10