@@ -65,6 +65,7 @@ public class MultiStateSwitch extends View {
65
65
private int disabledTextColor ;
66
66
@ Dimension
67
67
private int disabledTextSize ;
68
+ private int maxNumberStates ;
68
69
69
70
private final Rect drawingArea = new Rect ();
70
71
@@ -135,6 +136,7 @@ private void processAttributes(@NonNull Context context, @Nullable AttributeSet
135
136
disabledBackgroundColor = a .getColor (R .styleable .MultiStateSwitch_multistateswitch_disabled_background_color , 0 );
136
137
disabledTextColor = a .getColor (R .styleable .MultiStateSwitch_multistateswitch_disabled_text_color , 0 );
137
138
disabledTextSize = a .getDimensionPixelSize (R .styleable .MultiStateSwitch_multistateswitch_disabled_text_size , 0 );
139
+ maxNumberStates = a .getInt (R .styleable .MultiStateSwitch_multistateswitch_max_number_states , -1 );
138
140
} finally {
139
141
a .recycle ();
140
142
}
@@ -158,9 +160,10 @@ private void configEditMode() {
158
160
if (!isInEditMode ()) {
159
161
return ;
160
162
}
161
- addState (new State ("ONE" ));
162
- addState (new State ("TWO" ));
163
- addState (new State ("THREE" ));
163
+ int numMockStates = maxNumberStates > 0 ? maxNumberStates : 3 ;
164
+ for (int i = 1 ; i <= numMockStates ; i ++) {
165
+ addStateFromString (Integer .toString (i ));
166
+ }
164
167
}
165
168
166
169
/**
@@ -171,6 +174,9 @@ public void addState(@NonNull State state, @Nullable StateStyle stateStyle) {
171
174
if (states == null ) {
172
175
createDataStructures (DEFAULT_NUM_STATES );
173
176
}
177
+ if (hasMaxNumberStates () && getNumberStates () >= getMaxNumberStates ()) {
178
+ return ;
179
+ }
174
180
states .add (state );
175
181
if (stateStyle != null ) {
176
182
statesStyles .put (states .size () - 1 , stateStyle );
@@ -197,6 +203,9 @@ public void addStates(@NonNull List<State> states, @Nullable List<StateStyle> st
197
203
createDataStructures (states .size ());
198
204
}
199
205
for (int i = 0 ; i < states .size (); i ++) {
206
+ if (hasMaxNumberStates () && getNumberStates () >= getMaxNumberStates ()) {
207
+ return ;
208
+ }
200
209
this .states .add (states .get (i ));
201
210
if (stateStyles != null ) {
202
211
statesStyles .put (states .size () - 1 , stateStyles .get (i ));
@@ -622,6 +631,35 @@ public int getNumberStates() {
622
631
return states != null ? states .size () : 0 ;
623
632
}
624
633
634
+ /**
635
+ * Sets the max number of states. If you try to add a new state but the number of states is
636
+ * already maxNumberStates the state will be ignored. By default is -1 which means that there
637
+ * is no restriction.
638
+ * This parameter is also used to determine how many states to show in the editor preview. If
639
+ * it is set to no limit, 3 will be rendered by default, if not the number of states drawn will
640
+ * match maxNumberStates.
641
+ */
642
+ public void setMaxNumberStates (int maxNumberStates ) {
643
+ if (maxNumberStates == 0 ) {
644
+ throw new IllegalArgumentException ("Max number of states cannot be zero!" );
645
+ }
646
+ this .maxNumberStates = maxNumberStates ;
647
+ }
648
+
649
+ /**
650
+ * Returns max number of states. By default is -1 which means that there is no restriction.
651
+ */
652
+ public int getMaxNumberStates () {
653
+ return maxNumberStates ;
654
+ }
655
+
656
+ /**
657
+ * Checks whether there is a limit in the number of states or not.
658
+ */
659
+ public boolean hasMaxNumberStates () {
660
+ return getMaxNumberStates () > 0 ;
661
+ }
662
+
625
663
/**
626
664
* Sets typeface.
627
665
*/
0 commit comments