michaelsmith
michaelsmith

Reputation: 1051

Resource from int

When I have a source with this:

TextView localTV = (TextView)findViewById(2312345);

is there a way to kindly ask the compiler to transform it into the resource with its real name, like:

TextView localTV = (TextView)findViewById(R.id.mytext);

Upvotes: 1

Views: 46

Answers (2)

Wajdi Chamakhi
Wajdi Chamakhi

Reputation: 426

You should open the layer.xml of the activity or the fragment you are working on and then find the textView and add it an id like this.

   <TextView
    android:id="@+id/mytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My local TV" />

this will create a variable in the R.java with an int that you will not have to handle by your self. you only call

TextView localTV = (TextView)findViewById(R.id.mytext);

But if you do not have Xml layout and it is created programaticaly, you should go to res/values/ids.xml (if you do not find ids.xml you can create it ) and add an ID like this

<?xml version="1.0" encoding="utf-8"?>
<resources>

<item name="mytext" type="id" />
<item name="navBarText" type="id" />
<item name="navBarImg" type="id" />


</resources> 

and when you create the textView programaticaly you set an Id for the TextView like this

menuLayout.setId(R.id.mytext);

and then you can call the TextView by using

TextView localTV = (TextView)findViewById(R.id.mytext);

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006604

When I have a source with this

The most likely reason for having that code would be that the code was decompiled from an APK.

is there a way to kindly ask the compiler to transform it into the resource with its real name

No, for the simple reason that there may not be a resource corresponding with that number. As soon as you loaded this decompiled app into an IDE, all the resources would be assigned numbers that may or may not match the hardcoded values from the decompiled code.

Upvotes: 1

Related Questions