Closed Sultan91 closed 5 years ago
This is an expected behaviour. When you did not assign an id to a button, ToggleButtonGroup will generate an id during initialization, which does not guarantee to be the same at each run.
If you want a "static" id for each button, please assgin the id attribute for the button, for example:
<com.nex3z.togglebuttongroup.button.CircularToggle
android:id="@+id/choice_a" <- assign android:id attribute
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="A"/>
In this way, the id will not change at each run, but still may change across different build. You may want to use id name (like choice_a
from above) instead of specific id value (like 2131296300
).
Thanks for reply!, What about if I need create CircularToggle button dynamically in java code:
CircularToggle toggle = new CircularToggle(this);
toggle.setId();
Allows to set an ID, which AFAIK should be already defined in layout. There is no other way to define button ID without pre-creating layout elmement
You can use create button with specific id and add it to the button group:
SingleSelectToggleGroup single = findViewById(R.id.group_choices);
CircularToggle toggle = new CircularToggle(this);
toggle.setId(123); // set button id
toggle.setText("test");
single.addView(toggle); // add button to SingleSelectToggleGroup
It's better to give each button a unique id if you would like to retrieve them by findViewById()
later.
Yes but toggle.setId(123)
requires R.id. element(which is usually quite big number) not a simple integer
Technically all setId()
needs is a positive number. You can also use generateViewId()
for a long and unique id, or use id resource to predefine unique id to be used later.
This is an expected behaviour. When you did not assign an id to a button, ToggleButtonGroup will generate an id during initialization, which does not guarantee to be the same at each run.
If you want a "static" id for each button, please assgin the id attribute for the button, for example:
<com.nex3z.togglebuttongroup.button.CircularToggle android:id="@+id/choice_a" <- assign android:id attribute android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:text="A"/>
In this way, the id will not change at each run, but still may change across different build. You may want to use id name (like
choice_a
from above) instead of specific id value (like2131296300
).
how can I . I mean choice_a Check pls code sample
This is an expected behaviour. When you did not assign an id to a button, ToggleButtonGroup will generate an id during initialization, which does not guarantee to be the same at each run. If you want a "static" id for each button, please assgin the id attribute for the button, for example:
<com.nex3z.togglebuttongroup.button.CircularToggle android:id="@+id/choice_a" <- assign android:id attribute android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:text="A"/>
In this way, the id will not change at each run, but still may change across different build. You may want to use id name (like
choice_a
from above) instead of specific id value (like2131296300
).how can I . I mean choice_a Check pls code sample
CircularToggle choiceA = findViewById(R.id.choice_a);
choiceA.setChecked(true);
Please create different issue for different question.
THANK YOU
I have problem with checkedID when setting setOnCheckedChangeListener() checkedID does not resets after activity has finished and reopened once again checkedID grows