0

I have a Main Activity that has a textbox. As you can see in the code. I want to change the id of a textbox on click of a button. can this happen ?

      <TextView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="10dp"
    android:textAlignment="center"
    android:textSize="18sp"
    android:text="0"
    android:id="@+id/test">

</TextView>

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
      //How can i chane a Textbox ID?

        }
    });
jani
  • 9
  • 4
  • IDs are supposed to be immutable, what do you want to achieve by altering it? – Pawel Aug 30 '23 at 09:17
  • Actually I have 4 check boxes. I want the ID of the textbox to be set test1 when the first checkbox is clicked. When the second check box is clicked or selected, then the text box id becomes test2 and so on. How can i do this? – jani Aug 30 '23 at 09:22
  • OK let me rephrase, why do you need IDs to change? – Pawel Aug 30 '23 at 09:31
  • also similar like this https://stackoverflow.com/a/13241629/12709358 – Elango Aug 30 '23 at 10:08

1 Answers1

0

You cannot change id of the view programmatically.

In Android, the id of a View (such as a TextView) is set at the time of layout inflation and is typically defined in XML layout files. The id is used to uniquely identify the view within the view hierarchy. Once a view has been created and added to the view hierarchy, you cannot change its id programmatically.

If you want to "change" the id programmatically in terms of assigning a new unique identifier to a view, you would need to remove the view from its parent, create a new view with the desired id, and add it back to the parent view. However, this approach is not recommended and can lead to complexities and issues within the view hierarchy.