|
| 1 | +package com.amit.views; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.ContextWrapper; |
| 5 | +import android.content.res.TypedArray; |
| 6 | +import android.graphics.PorterDuff; |
| 7 | +import android.graphics.PorterDuffColorFilter; |
| 8 | +import android.graphics.drawable.Drawable; |
| 9 | +import android.support.annotation.ColorInt; |
| 10 | +import android.support.v4.app.FragmentActivity; |
| 11 | +import android.support.v4.content.ContextCompat; |
| 12 | +import android.support.v7.widget.AppCompatEditText; |
| 13 | +import android.util.AttributeSet; |
| 14 | + |
| 15 | +import com.amit.R; |
| 16 | + |
| 17 | +/** |
| 18 | + * Created by AMIT JANGID on 25/02/2019. |
| 19 | +**/ |
| 20 | +public class ASpinner extends AppCompatEditText |
| 21 | +{ |
| 22 | + private int mExpandTint; |
| 23 | + private String[] mItems; |
| 24 | + private int mSelected = -1; |
| 25 | + private String mTitle = "Select"; |
| 26 | + |
| 27 | + @ColorInt |
| 28 | + private int titleColor; |
| 29 | + |
| 30 | + private Drawable mExpandDrawable; |
| 31 | + |
| 32 | + private OnItemClickListener mOnItemClickListener; |
| 33 | + |
| 34 | + public ASpinner(Context context) |
| 35 | + { |
| 36 | + super(context); |
| 37 | + init(null); |
| 38 | + } |
| 39 | + |
| 40 | + public ASpinner(Context context, AttributeSet attrs) |
| 41 | + { |
| 42 | + super(context, attrs); |
| 43 | + init(attrs); |
| 44 | + } |
| 45 | + |
| 46 | + public ASpinner(Context context, AttributeSet attrs, int defStyleAttr) |
| 47 | + { |
| 48 | + super(context, attrs, defStyleAttr); |
| 49 | + init(attrs); |
| 50 | + } |
| 51 | + |
| 52 | + private void init(AttributeSet attrs) |
| 53 | + { |
| 54 | + setFocusable(false); |
| 55 | + setSingleLine(true); |
| 56 | + setLongClickable(false); |
| 57 | + |
| 58 | + mExpandTint = ContextCompat.getColor(getContext(), R.color.translucent_90); |
| 59 | + |
| 60 | + if (attrs != null) |
| 61 | + { |
| 62 | + TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ASpinner); |
| 63 | + |
| 64 | + if (typedArray != null) |
| 65 | + { |
| 66 | + mTitle = typedArray.getString(R.styleable.ASpinner_spinner_dialog_title) == null ? |
| 67 | + "Select" : |
| 68 | + typedArray.getString(R.styleable.ASpinner_spinner_dialog_title); |
| 69 | + |
| 70 | + mExpandTint = typedArray.getColor(R.styleable.ASpinner_spinner_dialog_icon_tint, mExpandTint); |
| 71 | + titleColor = typedArray.getColor(R.styleable.ASpinner_spinner_dialog_title_color, getResources().getColor(R.color.black)); |
| 72 | + |
| 73 | + typedArray.recycle(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + mExpandDrawable = ContextCompat.getDrawable(getContext(), R.drawable.spinner_expand_icon); |
| 78 | + mExpandDrawable.setColorFilter(new PorterDuffColorFilter(mExpandTint, PorterDuff.Mode.SRC_IN)); |
| 79 | + |
| 80 | + // calling set icon method |
| 81 | + setIcon(); |
| 82 | + } |
| 83 | + |
| 84 | + private void setIcon() |
| 85 | + { |
| 86 | + Drawable[] drawables = getCompoundDrawables(); |
| 87 | + setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], mExpandDrawable, drawables[3]); |
| 88 | + } |
| 89 | + |
| 90 | + public void setExpandTint(int expandTint) |
| 91 | + { |
| 92 | + this.mExpandTint = expandTint; |
| 93 | + postInvalidate(); |
| 94 | + } |
| 95 | + |
| 96 | + public void setItems(String[] items) |
| 97 | + { |
| 98 | + this.mItems = items; |
| 99 | + postInvalidate(); |
| 100 | + } |
| 101 | + |
| 102 | + public void setTitle(String title) |
| 103 | + { |
| 104 | + this.mTitle = title; |
| 105 | + postInvalidate(); |
| 106 | + } |
| 107 | + |
| 108 | + public void setOnItemClickListener(OnItemClickListener onItemClickListener) |
| 109 | + { |
| 110 | + this.mOnItemClickListener = onItemClickListener; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void postInvalidate() |
| 115 | + { |
| 116 | + super.postInvalidate(); |
| 117 | + mExpandDrawable.setColorFilter(new PorterDuffColorFilter(mExpandTint, PorterDuff.Mode.SRC_IN)); |
| 118 | + |
| 119 | + // calling set icon method |
| 120 | + setIcon(); |
| 121 | + } |
| 122 | + |
| 123 | + protected void setSelected(int selected) |
| 124 | + { |
| 125 | + this.mSelected = selected; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean performClick() |
| 130 | + { |
| 131 | + ASpinnerDialog dialog = ASpinnerDialog.newInstance(mTitle, mItems, mSelected, titleColor); |
| 132 | + dialog.setListener(mOnItemClickListener, ASpinner.this); |
| 133 | + dialog.show(findActivity(getContext()).getSupportFragmentManager(), dialog.getTag()); |
| 134 | + return super.performClick(); |
| 135 | + } |
| 136 | + |
| 137 | + public void clear() |
| 138 | + { |
| 139 | + setText(""); |
| 140 | + mSelected = -1; |
| 141 | + } |
| 142 | + |
| 143 | + public static <T extends FragmentActivity> T findActivity(Context context) |
| 144 | + { |
| 145 | + if (context == null) |
| 146 | + { |
| 147 | + throw new IllegalArgumentException("Context cannot be null."); |
| 148 | + } |
| 149 | + |
| 150 | + if (context instanceof FragmentActivity) |
| 151 | + { |
| 152 | + return (T) context; |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + ContextWrapper contextWrapper = (ContextWrapper) context; |
| 157 | + Context baseContext = contextWrapper.getBaseContext(); |
| 158 | + |
| 159 | + if (baseContext == null) |
| 160 | + { |
| 161 | + throw new IllegalStateException("Activity was not found as base context view!"); |
| 162 | + } |
| 163 | + |
| 164 | + return findActivity(baseContext); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public interface OnItemClickListener |
| 169 | + { |
| 170 | + void onItemClick(String selectedItem, int position); |
| 171 | + } |
| 172 | +} |
0 commit comments