nex3z / ToggleButtonGroup

A group of flowable toggle buttons, with multiple / single selection support and button customization.
Apache License 2.0
421 stars 49 forks source link

'checkedId' in onCheckedChanged() #39

Closed Sultan91 closed 5 years ago

Sultan91 commented 5 years ago

I have problem with checkedID when setting setOnCheckedChangeListener() checkedID does not resets after activity has finished and reopened once again checkedID grows

nex3z commented 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).

Sultan91 commented 5 years ago

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

nex3z commented 5 years ago

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.

Sultan91 commented 5 years ago

Yes but toggle.setId(123) requires R.id. element(which is usually quite big number) not a simple integer

nex3z commented 5 years ago

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.

zoro238 commented 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).

how can I . I mean choice_a Check pls code sample

nex3z commented 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).

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.

zoro238 commented 5 years ago

THANK YOU