Change EditText password mask character to asterisk (*) - android

Is there any way to change the password text from dot(.) to asterisk(*) .
Password is entering in edittext.
<EditText
android:id="#+id/passWord1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="number"
android:password="true"/>

Insert edittext in your xml file,
<EditText
android:id="#+id/passWordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"/>
and your class file go on and get findViewById from edittext and implement for this,
EditText edittext = (EditText)findViewById(R.id.passWordEditText);
edittext.setTransformationMethod(new AsteriskPasswordTransformationMethod());
and This class implement for that,
public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
And If your code is Kotlin then you have to make separate java file then you have to use java with kotlin code.

<EditText
android:id="#+id/passWord1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"//here is the change. check it once in your xml
android:password="true"/>
In eclipse there will be hints when you click Ctrl + Space when you move cursor at android:inputType. Then you can see list of options. there you can select textPassword
If you want to see * in place of . then check this Android: Asterisk Password Field

Got answer with help of link posted by Ram kiran
text.setTransformationMethod(new AsteriskPasswordTransformationMethod());
public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};

Kotlin
class AsteriskPasswordTransformationMethod : PasswordTransformationMethod() {
override fun getTransformation(source: CharSequence, view: View): CharSequence {
return PasswordCharSequence(source)
}
inner class PasswordCharSequence (private val source: CharSequence) : CharSequence {
override val length: Int
get() = source.length
override fun get(index: Int): Char = '*'
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
return source.subSequence(startIndex, endIndex)
}
}
}

EditText.setTransformationMethod(new PasswordTransformationMethod());

In Kotlin way:
class AsteriskPasswordTransformationMethod: PasswordTransformationMethod() {
override fun getTransformation(source: CharSequence?, view: View?): CharSequence {
return super.getTransformation(source, view)
}
abstract inner class PasswordCharSequence(val source: CharSequence) : CharSequence {
override val length: Int
get() = source.length
override fun get(index: Int): Char =
'*'
override fun subSequence(start: Int, end: Int): CharSequence {
return source.subSequence(start, end) // Return default
}
}
}`

For android:inputType, there is a type of password.

Try this
android:inputType="textPassword"

Related

How to mock an EditText in android using Mockito

I am trying to write a Unit Test for a validator of my android Application. The validator accepts as parameter EditText, therefore I need to mock it. However the mocking does not work, forcing the Test to crash on calling the when() method with the exception:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
My code is:
#RunWith(MockitoJUnitRunner.class)
public class MyUnitTest
{
#Mock
Context mMockContext;
#Test
public void validateIsCorrect() {
final EditText input = Mockito.mock(EditText.class);
when(input.getText()).thenReturn(Editable.Factory.getInstance().newEditable("123"));
...
}
}
The dependencies in build.gradle file are:
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
The method getText() of the EditText is not private or final. What am I doing wrong? Is it possible to mock an EditText this way? How?
When you're running a unit test, you're using a standard JVM context, not Android's context and that's why it's crashing: The Editable.Factory class and it's methods (like getInstance()) are not in the classpath. And they have not been mocked either.
What I'd do is to create a class that implements Editable with a private member to hold a string reference and use it to mock the getText() method.
Something like this:
class MockEditable implements Editable {
private String str;
public MockEditable(String str) {
this.str = str;
}
#Override #NonNull
public String toString() {
return str;
}
#Override
public int length() {
return str.length();
}
#Override
public char charAt(int i) {
return str.charAt(i);
}
#Override
public CharSequence subSequence(int i, int i1) {
return str.subSequence(i, i1);
}
#Override
public Editable replace(int i, int i1, CharSequence charSequence, int i2, int i3) {
return this;
}
#Override
public Editable replace(int i, int i1, CharSequence charSequence) {
return this;
}
#Override
public Editable insert(int i, CharSequence charSequence, int i1, int i2) {
return this;
}
#Override
public Editable insert(int i, CharSequence charSequence) {
return this;
}
#Override
public Editable delete(int i, int i1) {
return this;
}
#Override
public Editable append(CharSequence charSequence) {
return this;
}
#Override
public Editable append(CharSequence charSequence, int i, int i1) {
return this;
}
#Override
public Editable append(char c) {
return this;
}
#Override
public void clear() {
}
#Override
public void clearSpans() {
}
#Override
public void setFilters(InputFilter[] inputFilters) {
}
#Override
public InputFilter[] getFilters() {
return new InputFilter[0];
}
#Override
public void getChars(int i, int i1, char[] chars, int i2) {
}
#Override
public void setSpan(Object o, int i, int i1, int i2) {
}
#Override
public void removeSpan(Object o) {
}
#Override
public <T> T[] getSpans(int i, int i1, Class<T> aClass) {
return null;
}
#Override
public int getSpanStart(Object o) {
return 0;
}
#Override
public int getSpanEnd(Object o) {
return 0;
}
#Override
public int getSpanFlags(Object o) {
return 0;
}
#Override
public int nextSpanTransition(int i, int i1, Class aClass) {
return 0;
}
}
You can then make use of this class
Mockito.when(input.getText()).thenReturn(new MockEditable("123"));
Looking at this from a bit further away; I am asking myself: why does your validator need to know anything about Android specific classes?
What I mean is: I assume that your validator (in the end) has to check the properties of maybe a String, or something alike?
I would thus suggest to focus on separating concerns here:
Create a component that fetches a String from your EditText
Create a validator that works with such strings
Then you don't need any specific mocking for your validator in the first place!
final EditText editText = Mockito.mock(EditText.class);
final ArgumentCaptor<Editable> captor = ArgumentCaptor.forClass(Editable.class);
Mockito.doNothing().when(editText).setText(captor.capture());
Mockito.when(editText.getText()).thenAnswer(new Answer<Object>() {
#Override
public Object answer(InvocationOnMock invocation) {
return captor.getValue();
}
});
What about this guys, it works for me:
Please don't forget to add the MockitoAnnotations.init(this); and also use
#Mock private EditTextView passwordField;
#Before
public void init() {
MockitoAnnotations.initMocks(this);
when(rootView.findViewById(R.id.button_logout)).thenReturn(buttonLogout);
when(rootView.findViewById(R.id.button_unlock)).thenReturn(buttonUnlock);
when(rootView.findViewById(R.id.ScreenLock_PasswordTextField)).thenReturn(passwordField);
when(passwordField.getText()).thenReturn(Editable.Factory.getInstance().newEditable("asd"));
when(application.getPassword()).thenReturn("asd");
sut = new ScreenLockPresenterImpl(application, rootView, screenLockListener,
logoutButtonClickListener);
}
#Test
public void testOnClickWhenOk() {
sut.onClick(null);
verify(passwordField).getText();
verify(screenLockListener).unLock();
}
I think this is what you are looking for:
when(passwordField.getText()).thenReturn(Editable.Factory.getInstance().newEditable("asd"));

Custom bullet in editText inputType password

I need to create custom bullet for password for editText and put some padding between bullet symbols.
Is there some way to do this?
For changing the character that is displayed you can call the method setTransformationMethod (TransformationMethod method) and pass it a custom PasswordTransformationMethod.
This could look something like the following:
public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
and then set it like this:
textView.setTransformationMethod(new AsteriskPasswordTransformationMethod());
For changing the padding and such you should use either use textScaleX or if you are at API Level 21+ use letterSpacing in the XML.

Set unchangeable some part of editText android

I have some EditText for mobile number input. App must add unique text for every country. For example for Armenia is must add +374 , and user must fill other numbers. Also +374 must be unchangeable, user can't change or remove it. So is there some kind of ways for doing this?
EDIT:
I don't want to use textView or another view with this text and put it left of the ediText. I want to find some way with less operations. I need text to be frozen not to check every text changes or add missing text when user will delete some part of it.
Create a custom drawable class that will help to convert text into drawable.
public class TextDrawable extends Drawable {
private final String text;
private final Paint paint;
public TextDrawable(String text) {
this.text = text;
this.paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(16f);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.LEFT);
}
#Override
public void draw(Canvas canvas) {
canvas.drawText(text, 0, 6, paint);
}
#Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
#Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
#Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
Then set the drawable to left of the edittext as
EditText et = (EditText)findViewById(R.id.editText1);
String code = "+374";
et.setCompoundDrawablesWithIntrinsicBounds(new TextDrawable(code), null, null, null);
et.setCompoundDrawablePadding(code.length()*10);
Where the edittext is defined in the layout file as
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:ems="10" >
<requestFocus />
</EditText>
Final Output looks like
public class MainActivity extends Activity {
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
editText.setText("+374");
Selection.setSelection(editText.getText(), editText.getText().length());
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().startsWith("+374")) {
editText.setText("+374");
Selection.setSelection(editText.getText(), editText
.getText().length());
}
}
});
}
}
you can create class extend AppCompatEditText or EditText.
public class CEditTextMaster extends AppCompatEditText {
private boolean isNeedNoChangeSomeCharacters;
private String charactersNoChange;
public CEditTextMaster(Context context) {
super(context);
init(context, null);
}
public CEditTextMaster(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CEditTextMaster(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public void init(Context context, #Nullable AttributeSet attrs) {
if (isInEditMode())
return;
addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (isNeedNoChangeSomeCharacters && charactersNoChange != null) {
if (!getText().toString().startsWith(charactersNoChange)) {
removeTextChangedListener(this);
if (charactersNoChange.length() > s.length()) {
setText(charactersNoChange);
} else {
setText(charactersNoChange + getText());
}
setSelection(getText().toString().length());
addTextChangedListener(this);
}
}
}
});
}
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
if (isNeedNoChangeSomeCharacters && charactersNoChange != null) {
if (length() > charactersNoChange.length() && selStart < charactersNoChange.length()) {
setSelection(charactersNoChange.length(),selEnd);
}
}
}
#Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if (isNeedNoChangeSomeCharacters && charactersNoChange != null) {
if (!getText().toString().trim().startsWith(charactersNoChange)) {
setText(charactersNoChange + getText());
}
}
}
public void setCharactersNoChangeInitial(String charactersNoChange) {
isNeedNoChangeSomeCharacters = true;
this.charactersNoChange = charactersNoChange;
if (!getText().toString().trim().startsWith(charactersNoChange)) {
setText(getText());
}
}
}
for use:
edt.setCharactersNoChangeInitial("+734 ");
Use Text watcher and when user enters +3 it completes with +374 and it compares the first four characters and if it is same disable the back press of the softkeyboard so that it will not change the text and enters the other digit which append for the text used from the textwatcher
I opted for a solution, similar to another one submitted, of extending EditText and modifying onSelectionChanged. This prevents the user from even entering that region.
Here's what I'm using:
import android.content.Context
import android.text.Editable
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
class PrefixEditText(context: Context, attrs: AttributeSet?) : AppCompatEditText(context, attrs) {
private var prefix: String? = null
fun setPrefix(prefix: String) {
this.prefix = prefix
setText(prefix)
addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
override fun afterTextChanged(s: Editable?) {
// Block deleting the prefix
if (s?.startsWith(prefix) == false) {
setText(prefix)
}
}
})
}
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
var newStart = selStart
var newEnd = selEnd
prefix?.length?.let {
newStart = if (selStart < it) it else selStart
newEnd = if (selEnd < it) it else selEnd
}
if (selStart != newStart || selEnd != newEnd) {
setSelection(newStart, newEnd)
} else {
super.onSelectionChanged(selStart, selEnd)
}
}
// Here to conform to EditText's API
// Could also just override getText()
fun getPostText(): Editable? {
return prefix?.let {
Editable.Factory.getInstance().newEditable(text)?.delete(0, it.length)
} ?: run {
text
}
}
// Here for convenience, to avoid instantiating a new Editable, if the text is all you need
fun getPostCharSeq(): CharSequence? {
return prefix?.let {
text?.substring(it.length)
} ?: run {
text
}
}
}
With tests:
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import com.roosterteeth.roosterteeth.TestActivity
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
#RunWith(AndroidJUnit4::class)
class PrefixEditTextTest {
#Rule
#JvmField
var activityRule: ActivityTestRule<TestActivity> = ActivityTestRule(TestActivity::class.java, true, true)
private fun setupView(prefix: String, message: String): PrefixEditText {
val editText = PrefixEditText(activityRule.activity, null)
val lp = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
activityRule.activity.addContentView(editText, lp)
editText.setPrefix(prefix)
editText.append(message)
return editText
}
#Test
fun testSelection() {
activityRule.runOnUiThread {
val prefix = "pre: "
val message = "hello world"
val editText = setupView(prefix, message)
editText.setSelection(0)
Assert.assertEquals(editText.selectionStart, prefix.length)
Assert.assertEquals(editText.selectionEnd, prefix.length)
editText.setSelection(0, editText.length())
Assert.assertEquals(editText.selectionStart, prefix.length)
Assert.assertEquals(editText.selectionEnd, editText.length())
}
}
#Test
fun testGetPostText() {
activityRule.runOnUiThread {
val prefix = "pre: "
val message = "hello world"
val editText = setupView(prefix, message)
Assert.assertEquals(message, editText.getPostText().toString())
// This test is after to make sure that getting the post text did not actually modify the contents
Assert.assertEquals("pre: $message", editText.text.toString())
}
}
#Test
fun testGetPostCharSeq() {
activityRule.runOnUiThread {
val prefix = "pre: "
val message = "hello world"
val editText = setupView(prefix, message)
Assert.assertEquals(message, editText.getPostCharSeq())
// This test is after to make sure that getting the post text did not actually modify the contents
Assert.assertEquals("pre: $message", editText.text.toString())
}
}
}

How can I change the (.) of password to some other image dot?

I have a EditText which is a password field. By default android has its (.) dot which i want to replace with some image dot. How can I do that any suggestions.
You can try the PasswordTransformationMethod Class Check this Link
public class MyPasswordTransformationMethod extends PasswordTransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
// Call the above class using this:
text.setTransformationMethod(new MyPasswordTransformationMethod());

how to use TransformationMethod in android

I want to create an EditText which accepts passwords. I want to hide the character as soon as it is typed. So, I used a TransformationMethod.
I am new to this so, I tried the following code.
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTransformationMethod(new PasswordTransformationMethod());
private class PasswordTransformationMethod extends Transformation implements TransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return "/";
}
#Override
public void onFocusChanged(View view, CharSequence source, boolean focused, int direction, Rect previouslyFocusedRect) {
source = getTransformation(source, view);
}
}
However, it throws,
01-03 10:22:35.750: E/AndroidRuntime(2102): java.lang.IndexOutOfBoundsException
I am missing something. Any help will be appreciated.
The above method has lots of errors.
So, I am sharing the code that I use to convert passwords into dots.
Create a separate class in the same Java file like this,
public class MyPasswordTransformationMethod extends PasswordTransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source;
}
public char charAt(int index) {
return '.';
}
public int length() {
return mSource.length();
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
The implementation goes like this,
passwordEditText = (EditText) findViewById(R.id.passwordEditText);
passwordEditText.setTransformationMethod(new MyPasswordTransformationMethod());

Categories

Resources