Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get String Value Dynamically

I am passing a value from one activity to another through putExtra() and getExtra() and getting value like:

Passing from Activity 1:

intent = new Intent(this, blankScrollActivity.class);
intent.putExtra("alphabets", "a");

Recieving at Activity 2: blankScrollActivity.class

Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");

TextView textView = (TextView) findViewByid(R.id.blankTextView);

String Resource MyString.xml:

<string name="a">My Example 1</string>
<string name="b">My Example 2</string>

I want to get the value of string dynamically to assign textView.

like image 860
Sandhu Avatar asked Oct 28 '25 06:10

Sandhu


1 Answers

Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");

int resId = getResources().getIdentifier(varName , "string", getPackageName());
String data = getResources().getString(resId);

This way, you will get the String resource that you want based on the String value sent from the first Activity.

like image 50
Mohammed Aouf Zouag Avatar answered Oct 29 '25 21:10

Mohammed Aouf Zouag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!