Created
January 27, 2015 18:13
-
-
Save Jogan/b5bfa36b91712124f5ef to your computer and use it in GitHub Desktop.
EditText based off of Cyril Mottier's G+ Post https://plus.google.com/118417777153109946393/posts/D2bLxYDtAsu uses TintEditText for tint support.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.support.v7.internal.widget.TintEditText; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
import android.view.inputmethod.EditorInfo; | |
/** | |
* Copyright 2015 John Hogan | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
/** | |
* Based off Cyril Mottier's G+ Post https://plus.google.com/118417777153109946393/posts/D2bLxYDtAsu | |
* | |
* To change visibility icon, set | |
* | |
* <pre> | |
* android:drawableRight="@drawable/custom_icon" | |
* </pre> | |
*/ | |
public class DisclosePasswordEditText extends TintEditText implements OnTouchListener { | |
private Drawable xD; | |
private int mPreviousInputType; | |
private boolean mIsShowingPassword = false; | |
public DisclosePasswordEditText(Context context) { | |
super(context); | |
init(); | |
} | |
public DisclosePasswordEditText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public DisclosePasswordEditText(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
if (getCompoundDrawables()[2] != null) { | |
boolean tappedX = event.getX() > (getWidth() - getPaddingRight() - xD | |
.getIntrinsicWidth()); | |
if (tappedX) { | |
final int action = event.getAction(); | |
switch (action) { | |
case MotionEvent.ACTION_DOWN: | |
mPreviousInputType = getInputType(); | |
mIsShowingPassword = true; | |
setInputType( | |
EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD, true); | |
break; | |
case MotionEvent.ACTION_UP: | |
case MotionEvent.ACTION_CANCEL: | |
mIsShowingPassword = false; | |
setInputType(mPreviousInputType, true); | |
mPreviousInputType = -1; | |
break; | |
} | |
return false; | |
} | |
} | |
return false; | |
} | |
private void setInputType(int inputType, boolean keepState) { | |
int selectionStart = -1; | |
int selectionEnd = -1; | |
if (keepState) { | |
selectionStart = getSelectionStart(); | |
selectionEnd = getSelectionEnd(); | |
} | |
setInputType(inputType); | |
if (keepState) { | |
setSelection(selectionStart, selectionEnd); | |
} | |
updateDisclosePasswordDrawable(); | |
} | |
private void init() { | |
xD = getCompoundDrawables()[2]; | |
if (xD == null) { | |
xD = getResources().getDrawable(android.R.drawable.ic_menu_view); | |
} | |
xD.setBounds(0, 0, xD.getIntrinsicWidth(), xD.getIntrinsicHeight()); | |
setCompoundDrawables(getCompoundDrawables()[0], | |
getCompoundDrawables()[1], xD, getCompoundDrawables()[3]); | |
setOnTouchListener(this); | |
} | |
protected void updateDisclosePasswordDrawable() { | |
xD.setAlpha(mIsShowingPassword ? 145 : 255); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment