0

I have a TextView created in xml e.g.

<TextView
   android:id="@+id/hello_world_text_view"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello_world" />

and my strings.xml file has the property

<string name="hello_world">Hello World!!!</string>

Is there any way to get programmatically the resource name (in my example "hello_world") from the TextView?

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Hamilton
  • 152
  • 6

4 Answers4

1

You can do programatically like this

TextView txt=(TextView)findViewById(R.id.hello_world_text_view);

getResources().getResourceEntryName(R.id.hello_world_text_view);
Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
0

Use this way

String packageName = "your package name";
int resId = getResources().getIdentifier(YourString, "string", packageName);
textView.setText(getString(resId));
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • `getIdentifier(....)` expect first parameter to be the `name` of resource instead of resource value. https://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String,%20java.lang.String,%20java.lang.String) –  Feb 06 '19 at 15:40
0

Try this:

String yourValue = t.getText().toString();
int resourceId = this.getResources().getIdentifier(yourValue, "string", this.getPackageName());
String resourceName = this.getResources().getResourceEntryName(resourceId);
Jan Stoltman
  • 394
  • 3
  • 15
-1
TextView txt_hello = (TextView) findViewById(R.id.hello_world_text_view);

get your text :

String hello = txt_hello.getText().toString();
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95