"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
CodePudding user response:
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);
CodePudding user response:
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