Skip to content

Commit 9107e06

Browse files
author
Anshul Agarwal
committed
added defualt show/hide icons
1 parent 1f71c7d commit 9107e06

File tree

12 files changed

+21
-15
lines changed

12 files changed

+21
-15
lines changed

app/src/main/res/layout/content_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
android:layout_width="match_parent"
1818
android:layout_height="wrap_content"
1919
app:hide_drawable="@drawable/hide_password"
20+
app:password_visible="false"
2021
app:show_drawable="@drawable/show_password" />
2122
</RelativeLayout>

library/src/main/java/in/anshul/libray/PasswordEditText.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import android.annotation.TargetApi;
44
import android.content.Context;
5-
import android.content.res.Resources;
65
import android.content.res.TypedArray;
76
import android.graphics.drawable.Drawable;
87
import android.os.Parcel;
98
import android.os.Parcelable;
9+
import android.support.v4.content.ContextCompat;
1010
import android.text.InputType;
1111
import android.util.AttributeSet;
1212
import android.view.MotionEvent;
@@ -30,30 +30,35 @@ public PasswordEditText(Context context) {
3030

3131
public PasswordEditText(Context context, AttributeSet attrs) {
3232
super(context, attrs);
33-
init(context, attrs);
33+
init(context, attrs, 0, 0);
3434
}
3535

3636

3737
public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr) {
3838
super(context, attrs, defStyleAttr);
39-
init(context, attrs);
39+
init(context, attrs, 0, 0);
4040
}
4141

4242
@TargetApi(21)
4343
public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
4444
super(context, attrs, defStyleAttr, defStyleRes);
45-
init(context, attrs);
45+
init(context, attrs, defStyleAttr, defStyleRes);
4646
}
4747

48-
private void init(Context context, AttributeSet attrs) {
49-
50-
Resources res = context.getResources();
51-
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.password_edit_text);
52-
53-
mHideDrawable = a.getDrawable(R.styleable.password_edit_text_hide_drawable);
54-
mShowDrawable = a.getDrawable(R.styleable.password_edit_text_show_drawable);
55-
mPasswordVisible = a.getBoolean(R.styleable.password_edit_text_password_visible, false);
56-
a.recycle();
48+
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
49+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.password_edit_text, defStyleAttr, defStyleRes);
50+
51+
int hideDrawableResId =(R.drawable.in_anshul_hide_password);
52+
int showDrawableResId = R.drawable.in_anshul_show_password;
53+
try {
54+
mPasswordVisible = a.getBoolean(R.styleable.password_edit_text_password_visible, false);
55+
hideDrawableResId = a.getResourceId(R.styleable.password_edit_text_hide_drawable, hideDrawableResId);
56+
showDrawableResId = a.getResourceId(R.styleable.password_edit_text_show_drawable, showDrawableResId);
57+
} finally {
58+
a.recycle();
59+
mHideDrawable = ContextCompat.getDrawable(getContext(), hideDrawableResId);
60+
mShowDrawable = ContextCompat.getDrawable(getContext(), showDrawableResId);
61+
}
5762
mHideDrawable.setBounds(0, 0, mHideDrawable.getIntrinsicWidth(), mHideDrawable.getIntrinsicHeight());
5863
mShowDrawable.setBounds(0, 0, mShowDrawable.getIntrinsicWidth(), mShowDrawable.getIntrinsicHeight());
5964
if (mPasswordVisible) {
@@ -73,14 +78,14 @@ private void togglePasswordView() {
7378
}
7479

7580
private void showPassword() {
76-
setCompoundDrawables(null, null, mShowDrawable, null);
81+
setCompoundDrawables(null, null, mHideDrawable, null);
7782
setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
7883
setSelection(getText().length());
7984
mPasswordVisible = true;
8085
}
8186

8287
private void hidePassword() {
83-
setCompoundDrawables(null, null, mHideDrawable, null);
88+
setCompoundDrawables(null, null, mShowDrawable, null);
8489
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
8590
setSelection(getText().length());
8691
mPasswordVisible = false;
578 Bytes
Loading
483 Bytes
Loading
387 Bytes
Loading
326 Bytes
Loading
753 Bytes
Loading
590 Bytes
Loading
1.14 KB
Loading
951 Bytes
Loading

0 commit comments

Comments
 (0)