-1

I'd like to get string resource's name from a TextView. So for example I have a string in strings.xml:

<string name="my_text">Hello world</string>

and I set this text to a TextView via XML:

<TextView
    android:id="@+id/myAwesomeTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/my_text"/>

Now I need to get my string resource's name my_text programatically.

I can get text like so:

myAwesomeTextView.text //here I get: Hello world

And I can get TextView's name like so:

resources.getResourceEntryName(myAwesomeTextView.id); //here I get: myAwesomeTextView

Is there a way to get string resource's name - in this case my_text via kotlin/java code

Choletski
  • 7,074
  • 6
  • 43
  • 64
  • What do you want to get actually? The string resource name or the string resource value? – Sk Suraj Jun 03 '20 at 16:32
  • I need the string resource's name, in my case `my_text` from kotlin/Java – Choletski Jun 03 '20 at 16:37
  • What! I'm surprised why do you need this! Can you tell me in what case you need this? – Sk Suraj Jun 03 '20 at 16:41
  • This seems the possible way using `final int stringID = resources.getIdentifier(entryName,"string",context.getPackageName());`, check [this](https://stackoverflow.com/a/47075068/8244632) code. – Lalit Fauzdar Jun 03 '20 at 17:01
  • @LalitFauzdar this doesn't work, this will retrieve string res id if I know it's name, but I don't know it, I know textview id and name. – Choletski Jun 03 '20 at 17:33

1 Answers1

-2

Sorry to say this is not possible. TextView never stores the resource id so it's name. Hope this is helpful.

Sk Suraj
  • 500
  • 2
  • 15