@@ -101,18 +101,23 @@ public class SmartMaterialSpinner extends AppCompatSpinner implements ValueAnima
101
101
//AttributeSet
102
102
private int baseColor ;
103
103
private int highlightColor ;
104
+ private float errorTextSize ;
104
105
private int errorColor ;
105
106
private int disabledColor ;
106
107
private int underlineColor ;
107
108
private float underlineSize ;
108
109
private CharSequence error ;
109
110
private CharSequence hint ;
110
111
private int hintColor ;
112
+ private int itemListHintColor ;
113
+ private int itemListHintBackground ;
114
+ private float itemSize ;
111
115
private int itemColor ;
112
116
private int itemListColor ;
113
117
private int selectedItemColor ;
114
118
private float hintTextSize ;
115
119
private CharSequence floatingLabelText ;
120
+ private float floatingLabelSize ;
116
121
private int floatingLabelColor ;
117
122
private boolean multilineError ;
118
123
private Typeface typeface ;
@@ -180,26 +185,32 @@ private void init(Context context, AttributeSet attrs) {
180
185
181
186
private void initAttributes (Context context , AttributeSet attrs ) {
182
187
TypedArray defaultArray = context .obtainStyledAttributes (new int []{R .attr .colorControlNormal , R .attr .colorAccent });
183
- int defaultBaseColor = defaultArray .getColor (0 , 0 );
184
- int defaultHighlightColor = defaultArray .getColor (1 , 0 );
188
+ int defaultBaseColor = context .getResources ().getColor (R .color .smsp_base_color );
189
+ //int defaultHighlightColor = defaultArray.getColor(1, 0);
190
+ int defaultHighlightColor = context .getResources ().getColor (R .color .smsp_base_color );
185
191
int defaultErrorColor = context .getResources ().getColor (R .color .smsp_error_color );
186
192
defaultArray .recycle ();
187
193
188
194
TypedArray typedArray = context .obtainStyledAttributes (attrs , R .styleable .SmartMaterialSpinner );
189
195
baseColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_baseColor , defaultBaseColor );
190
196
highlightColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_highlightColor , defaultHighlightColor );
197
+ errorTextSize = typedArray .getDimension (R .styleable .SmartMaterialSpinner_smsp_errorTextSize , getResources ().getDimension (R .dimen .smsp_default_error_text_size ));
191
198
errorColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_errorColor , defaultErrorColor );
192
199
disabledColor = context .getResources ().getColor (R .color .smsp_disabled_color );
193
- underlineColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_underlineColor , defaultBaseColor );
200
+ underlineColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_underlineColor , getResources (). getColor ( R . color . smsp_underline_color ) );
194
201
error = typedArray .getString (R .styleable .SmartMaterialSpinner_smsp_error );
195
202
hint = typedArray .getString (R .styleable .SmartMaterialSpinner_smsp_hint );
196
203
floatingLabelText = typedArray .getString (R .styleable .SmartMaterialSpinner_smsp_floatingLabelText );
197
- hintColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_hintColor , baseColor );
204
+ hintColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_hintColor , getResources ().getColor (R .color .smsp_hint_color ));
205
+ itemListHintColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_itemListHintColor , getResources ().getColor (R .color .smsp_item_list_hint_color ));
206
+ itemListHintBackground = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_itemListHintBackgroundColor , getResources ().getColor (R .color .smsp_item_list_hint_background ));
207
+ itemSize = typedArray .getDimension (R .styleable .SmartMaterialSpinner_smsp_itemSize , getResources ().getDimension (R .dimen .smsp_default_text_and_hint_size ));
198
208
itemColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_itemColor , Color .BLACK );
199
209
itemListColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_itemListColor , Color .BLACK );
200
- selectedItemColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_selectedItemColor , itemColor );
210
+ selectedItemColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_selectedItemColor , getResources (). getColor ( R . color . smsp_selected_color ) );
201
211
hintTextSize = typedArray .getDimension (R .styleable .SmartMaterialSpinner_smsp_hintTextSize , getResources ().getDimension (R .dimen .smsp_default_hint_size ));
202
- floatingLabelColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_floatingLabelColor , baseColor );
212
+ floatingLabelSize = typedArray .getDimension (R .styleable .SmartMaterialSpinner_smsp_floatingLabelSize , getResources ().getDimension (R .dimen .smsp_default_floating_label_size ));
213
+ floatingLabelColor = typedArray .getColor (R .styleable .SmartMaterialSpinner_smsp_floatingLabelColor , getResources ().getColor (R .color .smsp_floating_label_color ));
203
214
multilineError = typedArray .getBoolean (R .styleable .SmartMaterialSpinner_smsp_multilineError , true );
204
215
minNbErrorLines = typedArray .getInt (R .styleable .SmartMaterialSpinner_smsp_nbErrorLines , 1 );
205
216
alignLabels = typedArray .getBoolean (R .styleable .SmartMaterialSpinner_smsp_alignLabels , true );
@@ -323,7 +334,7 @@ private void updateBottomPadding() {
323
334
Paint .FontMetrics textMetrics = textPaint .getFontMetrics ();
324
335
extraPaddingBottom = underlineTopSpacing + underlineBottomSpacing ;
325
336
if (error != null ) {
326
- extraPaddingBottom = underlineTopSpacing + dpToPx (2 );
337
+ extraPaddingBottom = underlineTopSpacing + dpToPx (4 );
327
338
}
328
339
if (enableErrorLabel ) {
329
340
extraPaddingBottom += (int ) ((textMetrics .descent - textMetrics .ascent ) * currentNbErrorLines );
@@ -497,14 +508,16 @@ protected void onDraw(Canvas canvas) {
497
508
if (error != null && enableErrorLabel ) {
498
509
lineHeight = dpToPx (underlineSize );
499
510
int startYErrorLabel = startYLine + errorLabelSpacing + lineHeight ;
500
- paint .setColor (errorColor );
511
+ paint .setColor (underlineColor );
501
512
textPaint .setColor (errorColor );
513
+ textPaint .setTextSize (errorTextSize );
502
514
//Error Label Drawing
503
515
if (multilineError ) {
504
516
canvas .save ();
505
517
canvas .translate (startX + rightLeftSpinnerPadding , startYErrorLabel - errorLabelSpacing );
506
518
if (staticLayout == null ) {
507
- prepareBottomPadding ();
519
+ int mWidth = getWidth () - getPaddingRight () - getPaddingLeft ();
520
+ staticLayout = new StaticLayout (error , textPaint , mWidth , Layout .Alignment .ALIGN_NORMAL , 1.0f , 0.0f , true );
508
521
}
509
522
staticLayout .draw (canvas );
510
523
canvas .restore ();
@@ -518,11 +531,10 @@ protected void onDraw(Canvas canvas) {
518
531
canvas .restore ();
519
532
}
520
533
}
521
-
522
534
} else {
523
535
lineHeight = dpToPx (underlineSize );
524
536
if (isSelected || hasFocus ()) {
525
- paint .setColor (highlightColor );
537
+ paint .setColor (underlineColor ); // highlightColor
526
538
} else {
527
539
paint .setColor (isEnabled () ? underlineColor : disabledColor );
528
540
}
@@ -534,21 +546,22 @@ protected void onDraw(Canvas canvas) {
534
546
//Floating Label Drawing
535
547
if ((hint != null || floatingLabelText != null ) && enableFloatingLabel ) {
536
548
if (isSelected || hasFocus ()) {
537
- textPaint .setColor (highlightColor );
549
+ textPaint .setColor (floatingLabelColor ); // highlightColor
538
550
} else {
539
551
textPaint .setColor (isEnabled () ? floatingLabelColor : disabledColor );
540
552
}
541
553
if (floatingLabelAnimator .isRunning () || !floatingLabelVisible ) {
542
554
textPaint .setAlpha ((int ) ((0.8 * floatingLabelPercent + 0.2 ) * baseAlpha * floatingLabelPercent ));
543
555
}
556
+ textPaint .setTextSize (floatingLabelSize );
544
557
String textToDraw = floatingLabelText != null ? floatingLabelText .toString () : hint .toString ();
545
558
if (isRtl ) {
546
559
canvas .drawText (textToDraw , getWidth () - rightLeftSpinnerPadding - textPaint .measureText (textToDraw ), startYFloatingLabel , textPaint );
547
560
} else {
548
561
canvas .drawText (textToDraw , startX + rightLeftSpinnerPadding , startYFloatingLabel , textPaint );
549
562
}
550
563
}
551
- drawSelector (canvas , getWidth () - rightLeftSpinnerPadding , getPaddingTop () + dpToPx (8 ));
564
+ drawSelector (canvas , getWidth () - rightLeftSpinnerPadding , getPaddingTop () + dpToPx (6 ));
552
565
}
553
566
554
567
private void drawSelector (Canvas canvas , int posX , int posY ) {
@@ -589,11 +602,12 @@ public boolean onTouchEvent(MotionEvent event) {
589
602
if (getContext () instanceof Activity ) {
590
603
SoftKeyboardUtil .hideSoftKeyboard ((Activity ) getContext ());
591
604
}
605
+ break ;
606
+ case MotionEvent .ACTION_UP :
592
607
if (!isShowEmptyDropdown && getCount () == 1 && hint != null && onEmptySpinnerClickListener != null ) {
593
608
onEmptySpinnerClickListener .onEmptySpinnerClicked ();
594
609
}
595
610
break ;
596
- case MotionEvent .ACTION_UP :
597
611
case MotionEvent .ACTION_CANCEL :
598
612
isSelected = false ;
599
613
break ;
@@ -633,6 +647,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
633
647
if (position >= 0 ) {
634
648
if (view instanceof TextView ) {
635
649
TextView selectedItem = (TextView ) parent .getChildAt (0 );
650
+ selectedItem .setTextSize (TypedValue .COMPLEX_UNIT_PX , itemSize );
636
651
selectedItem .setTextColor (itemColor );
637
652
}
638
653
listener .onItemSelected (parent , view , position , id );
@@ -645,6 +660,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
645
660
if (position > 0 && !isStartup ) {
646
661
if (view instanceof TextView ) {
647
662
TextView selectedItem = (TextView ) parent .getChildAt (0 );
663
+ selectedItem .setTextSize (TypedValue .COMPLEX_UNIT_PX , itemSize );
648
664
selectedItem .setTextColor (itemColor );
649
665
}
650
666
}
@@ -713,12 +729,25 @@ public void setHintColor(int hintColor) {
713
729
invalidate ();
714
730
}
715
731
732
+ public void setItemListHintColor (int itemListHintColor ) {
733
+ this .itemListHintColor = itemListHintColor ;
734
+ }
735
+
736
+ public void setItemListHintBackground (int itemListHintBackground ) {
737
+ this .itemListHintBackground = itemListHintBackground ;
738
+ }
739
+
716
740
public float getHintTextSize () {
717
741
return hintTextSize ;
718
742
}
719
743
720
744
public void setHintTextSize (float hintTextSize ) {
721
- this .hintTextSize = hintTextSize ;
745
+ this .hintTextSize = dpToPx (hintTextSize );
746
+ invalidate ();
747
+ }
748
+
749
+ public void setErrorTextSize (float errorTextSize ) {
750
+ this .errorTextSize = dpToPx (errorTextSize );
722
751
invalidate ();
723
752
}
724
753
@@ -776,6 +805,11 @@ public CharSequence getFloatingLabelText() {
776
805
return this .floatingLabelText ;
777
806
}
778
807
808
+ public void setFloatingLabelSize (float floatingLabelSize ) {
809
+ this .floatingLabelSize = dpToPx (floatingLabelSize );
810
+ invalidate ();
811
+ }
812
+
779
813
public int getFloatingLabelColor () {
780
814
return floatingLabelColor ;
781
815
}
@@ -910,6 +944,11 @@ public void setUnderlineColor(int color) {
910
944
invalidate ();
911
945
}
912
946
947
+ public void setItemSize (float itemSize ) {
948
+ this .itemSize = dpToPx (itemSize );
949
+ invalidate ();
950
+ }
951
+
913
952
public void setItemColor (int color ) {
914
953
this .itemColor = color ;
915
954
}
@@ -1160,6 +1199,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
1160
1199
1161
1200
@ Override
1162
1201
public View getDropDownView (int position , View convertView , ViewGroup parent ) {
1202
+ parent .setPadding (0 , 0 , 0 , 0 );
1163
1203
return buildView (position , convertView , parent , true );
1164
1204
}
1165
1205
@@ -1185,13 +1225,17 @@ private View buildView(int position, View convertView, ViewGroup parent, boolean
1185
1225
1186
1226
private View getHintView (final View convertView , final ViewGroup parent , final boolean isDropDownView ) {
1187
1227
final LayoutInflater inflater = LayoutInflater .from (mContext );
1188
- final int resid = isDropDownView ? mDropdownView : mHintView ;
1189
- final TextView textView = (TextView ) inflater .inflate (resid , parent , false );
1228
+ final int resId = isDropDownView ? mDropdownView : mHintView ;
1229
+ final TextView textView = (TextView ) inflater .inflate (resId , parent , false );
1190
1230
textView .setText (hint );
1191
1231
textView .setTextColor (SmartMaterialSpinner .this .isEnabled () ? hintColor : disabledColor );
1192
- textView .setTag (HINT_TYPE );
1193
1232
if (hintTextSize != -1 )
1194
1233
textView .setTextSize (TypedValue .COMPLEX_UNIT_PX , hintTextSize );
1234
+ if (isDropDownView ) {
1235
+ textView .setTextColor (itemListHintColor );
1236
+ textView .setBackgroundColor (itemListHintBackground );
1237
+ }
1238
+ textView .setTag (HINT_TYPE );
1195
1239
return textView ;
1196
1240
}
1197
1241
0 commit comments