Issue
I have a problem with ids on Java for Android. I do some TextView by code, and give it an id. But I can't get the TextView with the id I gave it.
I tried to do this, but it's wont work.
LinearLayout ll;
int id = 2000000;
private void init() {
ll = findViewById(R.id.layout);
}
private TextView addTV(int x, int y, int width, int height) {
TextView tv = new TextView(this);
tv.setId(id);
id++;
tv.setText("Hello");
return tv;
}
private void changeTextTV(int id, String text) {
TextView tv = findViewById(id); //<- this is wrong, but I don't know what is right
tv.setText(text);
}
init();
ll.addView(addTV(0,0,100,100));
changeTextTV(2000000, "It's me");
Could you help me ?
PS : I'm French, so sorry for my spelling mistakes
Solution
after long time search and do more test. I find some answer, you have 2 choise to get the component by a setted ID :
if you want by a hard ID :
@SuppressLint("ResourceType")
private void changeTextTV(String text) {
TextView tv = findViewById(2000000);
tv.setText(text);
}
or by a variable
private void changeTextTV(int id, String text) {
TextView tv = findViewById(id);
tv.setText(text);
}
Thanks for the time you spend for me.
Answered By - Tim Fav
Answer Checked By - Gilberto Lyons (JavaFixing Admin)