0

I code some TextViews programatically and I want to provide them with ids. In the xml file I would have done something like:

android:id="@+id/btn0"

but in the .java file I'm allowed to set the id only as a number:

t1.setId(3);

Is there any possibility to convert "btn0" in a number and then to use findbyid() functions using as a parameter the string "btn0"?

P.S. I experimented logging the value of R.id.btn0 and I get a number : 2131361887

Log.d("aaa", Integer.toString(R.id.btn0));

Is there any possibility to convert this number back in string of ASCII characters?

Thank you a lot!

dbc
  • 104,963
  • 20
  • 228
  • 340
a a
  • 49
  • 1
  • 6
  • Does this answer your question? [Android create ID programmatically](https://stackoverflow.com/questions/15139310/android-create-id-programmatically) – Bringoff Dec 31 '19 at 14:37

1 Answers1

2

I believe what you really need is ids predeclared in resources but not assigned to any view in a layout. If you want to do that, you may create id resource, i.e. ids.xml inside values resource directory. The ids in that file should be declared like this:

<resources>
    <item name="btn0" type="id"/>
    <item name="open_url" type="id"/>
    ...
</resources>

After that, you will be able to use those ids as normal, from the generated R class (R.id.btn0). The only thing I don't understand is why you need to search for a programmatically created view if you already have a reference to it.

Bringoff
  • 1,093
  • 1
  • 10
  • 24
  • Thank you a lot! Well, I don't have yet the refference yet, I want to create lots of textviews in a for loop and assign them the id btn and then the value of i like btn0. – a a Dec 31 '19 at 14:33