Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ID of the referenced resource by a different resource?

I would like to get the ID of the referenced resource in runtime. For example this is my code:

<string name="d2c_entryroadblock_start_value" translatable="false">@string/get_started</string>

and I am interested in the ID of the R.string.get_started having only the reference to R.string.d2c_entryroadblock_start_value in runtime.

You can also see how it looks in the APK analyzer below - I need to get that @ref/0x7f1302fc

APK analyzer screenshot

like image 287
Mariusz Avatar asked Dec 05 '19 13:12

Mariusz


1 Answers

You can get that with the Resources#getValue() method, passing false for the resolveRefs parameter. For example:

TypedValue value = new TypedValue();
getResources().getValue(R.string.alias_name, value, false);
int aliasedId = value.data;

As shown, the numerical ID for the aliased resource will be in the TypedValue's data field. If you actually need it in hexadecimal, you can pass it to Integer.toHexString(). And, if you need the aliased resource name, then it's simply:

String aliasedName = getResources().getResourceEntryName(value.data);
like image 56
Mike M. Avatar answered Oct 22 '22 17:10

Mike M.