-1

I want to set ID for a dynamically added textview in a loop. I found out that the only function they have is setId with int parameter. How can we set the ID with string, perhaps like this:

LinearLayout linearLayout = findViewById(R.id.linearLayout);
for(int i=0; i<10; i++) {
    TextView textView = new TextView(this);
    textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    textView.setText("Text Number "+i);
    textView.setId("@+id/myTextView"+i); // => Is this possible? This will cause error because setId needs INT type parameter.

    linearLayout.addView(textView);
}

I don't want to use resource file because it's not dynamic. Is it possible?

Henry Gunawan
  • 922
  • 3
  • 18
  • 38

2 Answers2

1

You cannot set id with char or string. Only int will work. However you can use the setTag() API. You can later do getTag() or use findViewWithTag

Kevin Desai
  • 317
  • 3
  • 22
0

No, You can't. Only int will work.

This link may help you for details understanding. https://stackoverflow.com/a/30253770/4365240

Saidur Rahman
  • 546
  • 2
  • 4
  • 20