Skip to content

Commit f31169b

Browse files
committed
1. Added custom spinner.
1 parent 5382b07 commit f31169b

17 files changed

+737
-8
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies {
4747
implementation "com.android.support:design:$supportLibraryVersion"
4848
implementation "com.android.support:cardview-v7:$supportLibraryVersion"
4949
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
50+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
5051

5152
// RxJava dependencies
5253
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'

app/src/main/java/com/amit/permission/PermissionHelper.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import android.support.v4.app.ActivityCompat
99
* Created by AMIT JANGID on 20/02/2019.
1010
*
1111
* this class will help users in requesting for permissions
12-
**/
12+
**/
1313
@Suppress("unused", "MemberVisibilityCanBePrivate")
1414
class PermissionHelper
1515
{
@@ -20,7 +20,7 @@ class PermissionHelper
2020
* get all permissions method
2121
*
2222
* this method will get all permissions mentioned in 'Manifest.xml' file
23-
**/
23+
**/
2424
private fun getAllPermissions(context: Context) : Array<String>
2525
{
2626
return context.packageManager.getPackageInfo(
@@ -32,7 +32,7 @@ class PermissionHelper
3232
* request all permissions method
3333
*
3434
* this method will request for all permissions mentioned in 'Manifest.xml' file
35-
**/
35+
**/
3636
fun requestAllPermissions(activity: Activity, requestCode: Int)
3737
{
3838
val permissions = getAllPermissions(activity)
@@ -43,7 +43,7 @@ class PermissionHelper
4343
* request permission method
4444
*
4545
* this method will handle single permission request
46-
**/
46+
**/
4747
fun requestPermission(activity: Activity, requestCode: Int, permission: String)
4848
{
4949
val permissions = arrayOf(permission)
@@ -54,7 +54,7 @@ class PermissionHelper
5454
* request permission method
5555
*
5656
* this method will handle multiple permissions added in string array
57-
**/
57+
**/
5858
fun requestPermission(activity: Activity, requestCode: Int, permissions: Array<String>)
5959
{
6060
ActivityCompat.requestPermissions(activity, permissions, requestCode)
@@ -64,7 +64,7 @@ class PermissionHelper
6464
* get permissions not granted method
6565
*
6666
* this method will get all the permissions not granted by the end user
67-
**/
67+
**/
6868
fun getPermissionsNotGranted(activity: Activity, permissions: Array<String>): Array<String>
6969
{
7070
val list = ArrayList<String>()
@@ -84,7 +84,7 @@ class PermissionHelper
8484
* get permissions granted method
8585
*
8686
* this method will get all the permissions granted by the end user
87-
**/
87+
**/
8888
fun getPermissionsGranted(activity: Activity, permissions: Array<String>): Array<String>
8989
{
9090
val list = ArrayList<String>()
@@ -102,7 +102,7 @@ class PermissionHelper
102102

103103
/**
104104
* parse permissions method
105-
**/
105+
**/
106106
fun parsePermissions(activity: Activity, requestCode: Int, permissions: Array<String>, listener: PermissionListener)
107107
{
108108
listener.onGranted(requestCode, getPermissionsGranted(activity, permissions))
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

Comments
 (0)