Skip to content

Commit b2ea88d

Browse files
committed
Add more customization and improve default behavior
1 parent 47dca6d commit b2ea88d

File tree

9 files changed

+91
-60
lines changed

9 files changed

+91
-60
lines changed

resources/src/main/java/com/chivorn/resourcemodule/MainApp.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.content.Intent;
44
import android.net.Uri;
5-
import android.support.v4.content.ContextCompat;
65
import android.support.v7.app.AppCompatActivity;
76
import android.view.View;
87
import android.widget.Button;
@@ -36,8 +35,6 @@ public void initBaseView() {
3635
githubRepo = findViewById(R.id.git_repo_container);
3736
btnShowError = findViewById(R.id.btn_show_error);
3837
githubRepoBtn = findViewById(R.id.github_repo_btn);
39-
40-
setUnderlineColor(spProvince, spProvinceDialog, spCustomColor, spSearchable);
4138
}
4239

4340
public void onClickListenter() {
@@ -64,10 +61,4 @@ public void onClick(View v) {
6461
spSearchable.setError(getResources().getString(R.string.sample_error_message));
6562
}
6663
}
67-
68-
private void setUnderlineColor(SmartMaterialSpinner... spinners) {
69-
for (SmartMaterialSpinner spinner : spinners) {
70-
spinner.setUnderlineColor(ContextCompat.getColor(getApplicationContext(), R.color.underline_color));
71-
}
72-
}
7364
}

resources/src/main/res/layout/smart_material_spinner_sample_layout.xml

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,22 @@
2626
android:layout_width="match_parent"
2727
android:layout_height="wrap_content"
2828
android:spinnerMode="dialog"
29-
app:smsp_baseColor="@color/base_color"
3029
app:smsp_error="@string/sample_error_message"
31-
app:smsp_errorColor="@color/error_color"
32-
app:smsp_floatingLabelColor="@color/floating_color"
33-
app:smsp_highlightColor="@color/colorPrimary"
30+
app:smsp_errorTextSize="12dp"
3431
app:smsp_hint="Searchable Spinner"
35-
app:smsp_hintColor="@color/hint_color"
3632
app:smsp_hintTextSize="14sp"
3733
app:smsp_isSearchable="true"
38-
app:smsp_itemColor="@color/custom_item_color"
39-
app:smsp_itemListColor="@color/custom_item_list_color"
34+
app:smsp_itemSize="14sp"
4035
app:smsp_multilineError="true"
41-
app:smsp_searchHeaderColor="@color/smsp_search_header_background"
42-
app:smsp_searchHeaderText="Province"
43-
app:smsp_searchHint="Search Item"
44-
app:smsp_selectedItemColor="@color/custom_selected_item_color" />
36+
app:smsp_searchHeaderColor="@color/smsp_search_header_background" />
4537

4638
<com.chivorn.smartmaterialspinner.SmartMaterialSpinner
4739
android:id="@+id/sp_provinces"
4840
android:layout_width="match_parent"
4941
android:layout_height="wrap_content"
50-
app:smsp_baseColor="@color/base_color"
5142
app:smsp_error="@string/sample_error_message"
5243
app:smsp_errorColor="@color/error_color"
53-
app:smsp_floatingLabelColor="@color/floating_color"
54-
app:smsp_highlightColor="@color/colorPrimary"
5544
app:smsp_hint="Province (Dropdown Mode)"
56-
app:smsp_hintColor="@color/hint_color"
5745
app:smsp_hintTextSize="14sp"
5846
app:smsp_multilineError="false" />
5947

@@ -62,12 +50,9 @@
6250
android:layout_width="match_parent"
6351
android:layout_height="wrap_content"
6452
android:spinnerMode="dialog"
65-
app:smsp_baseColor="@color/base_color"
6653
app:smsp_errorColor="@color/error_color"
6754
app:smsp_floatingLabelColor="@color/floating_color"
68-
app:smsp_highlightColor="@color/colorPrimary"
6955
app:smsp_hint="Province (Dialog Mode)"
70-
app:smsp_hintColor="@color/hint_color"
7156
app:smsp_hintTextSize="14sp"
7257
app:smsp_multilineError="true" />
7358

@@ -76,12 +61,10 @@
7661
android:layout_width="match_parent"
7762
android:layout_height="wrap_content"
7863
android:spinnerMode="dialog"
79-
app:smsp_baseColor="@color/base_color"
8064
app:smsp_errorColor="@color/error_color"
8165
app:smsp_floatingLabelColor="@color/floating_color"
82-
app:smsp_highlightColor="@color/colorPrimary"
8366
app:smsp_hint="Custom Color"
84-
app:smsp_hintColor="@color/hint_color"
67+
8568
app:smsp_hintTextSize="14sp"
8669
app:smsp_itemColor="@color/custom_item_color"
8770
app:smsp_itemListColor="@color/custom_item_list_color"
@@ -92,8 +75,8 @@
9275
android:layout_width="match_parent"
9376
android:layout_height="wrap_content"
9477
app:smsp_alwaysShowFloatingLabel="false"
95-
app:smsp_baseColor="@color/base_color"
9678
app:smsp_errorColor="@color/error_color"
79+
app:smsp_floatingLabelColor="@color/floating_color"
9780
app:smsp_floatingLabelText="floating text"
9881
app:smsp_hint="Empty Item Spinner"
9982
app:smsp_showEmptyDropdown="false"
@@ -107,7 +90,6 @@
10790
android:hint="@string/input_text"
10891
android:maxLines="1"
10992
android:padding="0dp"
110-
android:textColorHint="@color/floating_color"
11193
android:textSize="14sp" />
11294

11395
<Button
@@ -118,7 +100,7 @@
118100
android:layout_marginTop="10dp"
119101
android:layout_marginEnd="5dp"
120102
android:layout_marginRight="5dp"
121-
android:background="#2196F3"
103+
android:background="#3F51B5"
122104
android:text="@string/show_error"
123105
android:textAllCaps="false"
124106
android:textColor="@color/smsp_white_color" />

resources/src/main/res/values/colors.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<color name="base_color">#d46a6a</color>
88
<color name="hint_color">#d46a6a</color>
9-
<color name="floating_color">#d46a6a</color>
9+
<color name="floating_color">#009688</color>
1010
<color name="underline_color">#4851504e</color>
1111

1212
<color name="custom_item_color">#2196F3</color>

smartmaterialspinner/src/main/java/com/chivorn/smartmaterialspinner/SmartMaterialSpinner.java

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,23 @@ public class SmartMaterialSpinner extends AppCompatSpinner implements ValueAnima
101101
//AttributeSet
102102
private int baseColor;
103103
private int highlightColor;
104+
private float errorTextSize;
104105
private int errorColor;
105106
private int disabledColor;
106107
private int underlineColor;
107108
private float underlineSize;
108109
private CharSequence error;
109110
private CharSequence hint;
110111
private int hintColor;
112+
private int itemListHintColor;
113+
private int itemListHintBackground;
114+
private float itemSize;
111115
private int itemColor;
112116
private int itemListColor;
113117
private int selectedItemColor;
114118
private float hintTextSize;
115119
private CharSequence floatingLabelText;
120+
private float floatingLabelSize;
116121
private int floatingLabelColor;
117122
private boolean multilineError;
118123
private Typeface typeface;
@@ -180,26 +185,32 @@ private void init(Context context, AttributeSet attrs) {
180185

181186
private void initAttributes(Context context, AttributeSet attrs) {
182187
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);
185191
int defaultErrorColor = context.getResources().getColor(R.color.smsp_error_color);
186192
defaultArray.recycle();
187193

188194
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SmartMaterialSpinner);
189195
baseColor = typedArray.getColor(R.styleable.SmartMaterialSpinner_smsp_baseColor, defaultBaseColor);
190196
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));
191198
errorColor = typedArray.getColor(R.styleable.SmartMaterialSpinner_smsp_errorColor, defaultErrorColor);
192199
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));
194201
error = typedArray.getString(R.styleable.SmartMaterialSpinner_smsp_error);
195202
hint = typedArray.getString(R.styleable.SmartMaterialSpinner_smsp_hint);
196203
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));
198208
itemColor = typedArray.getColor(R.styleable.SmartMaterialSpinner_smsp_itemColor, Color.BLACK);
199209
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));
201211
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));
203214
multilineError = typedArray.getBoolean(R.styleable.SmartMaterialSpinner_smsp_multilineError, true);
204215
minNbErrorLines = typedArray.getInt(R.styleable.SmartMaterialSpinner_smsp_nbErrorLines, 1);
205216
alignLabels = typedArray.getBoolean(R.styleable.SmartMaterialSpinner_smsp_alignLabels, true);
@@ -323,7 +334,7 @@ private void updateBottomPadding() {
323334
Paint.FontMetrics textMetrics = textPaint.getFontMetrics();
324335
extraPaddingBottom = underlineTopSpacing + underlineBottomSpacing;
325336
if (error != null) {
326-
extraPaddingBottom = underlineTopSpacing + dpToPx(2);
337+
extraPaddingBottom = underlineTopSpacing + dpToPx(4);
327338
}
328339
if (enableErrorLabel) {
329340
extraPaddingBottom += (int) ((textMetrics.descent - textMetrics.ascent) * currentNbErrorLines);
@@ -497,14 +508,16 @@ protected void onDraw(Canvas canvas) {
497508
if (error != null && enableErrorLabel) {
498509
lineHeight = dpToPx(underlineSize);
499510
int startYErrorLabel = startYLine + errorLabelSpacing + lineHeight;
500-
paint.setColor(errorColor);
511+
paint.setColor(underlineColor);
501512
textPaint.setColor(errorColor);
513+
textPaint.setTextSize(errorTextSize);
502514
//Error Label Drawing
503515
if (multilineError) {
504516
canvas.save();
505517
canvas.translate(startX + rightLeftSpinnerPadding, startYErrorLabel - errorLabelSpacing);
506518
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);
508521
}
509522
staticLayout.draw(canvas);
510523
canvas.restore();
@@ -518,11 +531,10 @@ protected void onDraw(Canvas canvas) {
518531
canvas.restore();
519532
}
520533
}
521-
522534
} else {
523535
lineHeight = dpToPx(underlineSize);
524536
if (isSelected || hasFocus()) {
525-
paint.setColor(highlightColor);
537+
paint.setColor(underlineColor); // highlightColor
526538
} else {
527539
paint.setColor(isEnabled() ? underlineColor : disabledColor);
528540
}
@@ -534,21 +546,22 @@ protected void onDraw(Canvas canvas) {
534546
//Floating Label Drawing
535547
if ((hint != null || floatingLabelText != null) && enableFloatingLabel) {
536548
if (isSelected || hasFocus()) {
537-
textPaint.setColor(highlightColor);
549+
textPaint.setColor(floatingLabelColor); // highlightColor
538550
} else {
539551
textPaint.setColor(isEnabled() ? floatingLabelColor : disabledColor);
540552
}
541553
if (floatingLabelAnimator.isRunning() || !floatingLabelVisible) {
542554
textPaint.setAlpha((int) ((0.8 * floatingLabelPercent + 0.2) * baseAlpha * floatingLabelPercent));
543555
}
556+
textPaint.setTextSize(floatingLabelSize);
544557
String textToDraw = floatingLabelText != null ? floatingLabelText.toString() : hint.toString();
545558
if (isRtl) {
546559
canvas.drawText(textToDraw, getWidth() - rightLeftSpinnerPadding - textPaint.measureText(textToDraw), startYFloatingLabel, textPaint);
547560
} else {
548561
canvas.drawText(textToDraw, startX + rightLeftSpinnerPadding, startYFloatingLabel, textPaint);
549562
}
550563
}
551-
drawSelector(canvas, getWidth() - rightLeftSpinnerPadding, getPaddingTop() + dpToPx(8));
564+
drawSelector(canvas, getWidth() - rightLeftSpinnerPadding, getPaddingTop() + dpToPx(6));
552565
}
553566

554567
private void drawSelector(Canvas canvas, int posX, int posY) {
@@ -589,11 +602,12 @@ public boolean onTouchEvent(MotionEvent event) {
589602
if (getContext() instanceof Activity) {
590603
SoftKeyboardUtil.hideSoftKeyboard((Activity) getContext());
591604
}
605+
break;
606+
case MotionEvent.ACTION_UP:
592607
if (!isShowEmptyDropdown && getCount() == 1 && hint != null && onEmptySpinnerClickListener != null) {
593608
onEmptySpinnerClickListener.onEmptySpinnerClicked();
594609
}
595610
break;
596-
case MotionEvent.ACTION_UP:
597611
case MotionEvent.ACTION_CANCEL:
598612
isSelected = false;
599613
break;
@@ -633,6 +647,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
633647
if (position >= 0) {
634648
if (view instanceof TextView) {
635649
TextView selectedItem = (TextView) parent.getChildAt(0);
650+
selectedItem.setTextSize(TypedValue.COMPLEX_UNIT_PX, itemSize);
636651
selectedItem.setTextColor(itemColor);
637652
}
638653
listener.onItemSelected(parent, view, position, id);
@@ -645,6 +660,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
645660
if (position > 0 && !isStartup) {
646661
if (view instanceof TextView) {
647662
TextView selectedItem = (TextView) parent.getChildAt(0);
663+
selectedItem.setTextSize(TypedValue.COMPLEX_UNIT_PX, itemSize);
648664
selectedItem.setTextColor(itemColor);
649665
}
650666
}
@@ -713,12 +729,25 @@ public void setHintColor(int hintColor) {
713729
invalidate();
714730
}
715731

732+
public void setItemListHintColor(int itemListHintColor) {
733+
this.itemListHintColor = itemListHintColor;
734+
}
735+
736+
public void setItemListHintBackground(int itemListHintBackground) {
737+
this.itemListHintBackground = itemListHintBackground;
738+
}
739+
716740
public float getHintTextSize() {
717741
return hintTextSize;
718742
}
719743

720744
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);
722751
invalidate();
723752
}
724753

@@ -776,6 +805,11 @@ public CharSequence getFloatingLabelText() {
776805
return this.floatingLabelText;
777806
}
778807

808+
public void setFloatingLabelSize(float floatingLabelSize) {
809+
this.floatingLabelSize = dpToPx(floatingLabelSize);
810+
invalidate();
811+
}
812+
779813
public int getFloatingLabelColor() {
780814
return floatingLabelColor;
781815
}
@@ -910,6 +944,11 @@ public void setUnderlineColor(int color) {
910944
invalidate();
911945
}
912946

947+
public void setItemSize(float itemSize) {
948+
this.itemSize = dpToPx(itemSize);
949+
invalidate();
950+
}
951+
913952
public void setItemColor(int color) {
914953
this.itemColor = color;
915954
}
@@ -1160,6 +1199,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
11601199

11611200
@Override
11621201
public View getDropDownView(int position, View convertView, ViewGroup parent) {
1202+
parent.setPadding(0, 0, 0, 0);
11631203
return buildView(position, convertView, parent, true);
11641204
}
11651205

@@ -1185,13 +1225,17 @@ private View buildView(int position, View convertView, ViewGroup parent, boolean
11851225

11861226
private View getHintView(final View convertView, final ViewGroup parent, final boolean isDropDownView) {
11871227
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);
11901230
textView.setText(hint);
11911231
textView.setTextColor(SmartMaterialSpinner.this.isEnabled() ? hintColor : disabledColor);
1192-
textView.setTag(HINT_TYPE);
11931232
if (hintTextSize != -1)
11941233
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, hintTextSize);
1234+
if (isDropDownView) {
1235+
textView.setTextColor(itemListHintColor);
1236+
textView.setBackgroundColor(itemListHintBackground);
1237+
}
1238+
textView.setTag(HINT_TYPE);
11951239
return textView;
11961240
}
11971241

smartmaterialspinner/src/main/res/layout/smart_material_spinner_searchable_dialog_layout.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
android:layout_height="wrap_content"
1717
android:gravity="center"
1818
android:padding="15dp"
19-
android:text="@string/search_item"
19+
android:text="@string/search_or_select_item"
2020
android:textColor="@color/smsp_white_color"
2121
android:textSize="14sp"
2222
android:textStyle="bold" />
@@ -36,7 +36,7 @@
3636
android:layout_width="match_parent"
3737
android:layout_height="wrap_content"
3838
android:iconifiedByDefault="false"
39-
android:queryHint="Search" />
39+
android:queryHint="Search..." />
4040

4141
<ListView
4242
android:id="@+id/search_list_item"

0 commit comments

Comments
 (0)