Showing the last character of a password in an EditText - android

I have an app where people need to login with a password. I would like for only the last character typed to be shown, but all I seem to get is all chars dots or all chars visible.
I tried a few things:
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
and setting inputtype in the xml.
I have only tested on a galaxy s2 as that is the only android device at my office at the moment, so I don't know if the problem is only with my device.
edit:
Just tested on an HTC Sensation from a colleague and it does work as intended on his phone, but the question remains how to get this same thing on the Galaxy S2?

It's been almost 1.5 years since this was asked :P. But I had the same requirement and was successfully able to implement it. Pass an object of the MyTransformation class as a parameter in the setTransformationMethod and you're good to go :) Here's the code.
public class MyTransformation 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) {
//This is the check which makes sure the last character is shown
if(index != mSource.length()-1)
return '•';
else
return mSource.charAt(index);
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
}

Improving on AndyFaizan's answer, my solution doesn't show characters while using backspace.
Without this (simple) change it'd be possible to reveal the whole password by deleting characters at the end of the input.
public class CustomPasswordTransformation extends PasswordTransformationMethod {
boolean lastActionWasDelete = false;
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
super.onTextChanged(s, start, before, count);
this.lastActionWasDelete = before > count;
}
private class PasswordCharSequence implements CharSequence {
private CharSequence source;
PasswordCharSequence(CharSequence source) {
this.source = source;
}
public char charAt(int index) {
//This is the check which makes sure the last character is shown
if (!lastActionWasDelete && index == source.length() - 1) return source.charAt(index);
return '•';
}
public int length() {
return source.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return source.subSequence(start, end); // Return default
}
}
}

As ethan already mentioned ages ago, the user can toggle the password character display behaviour in the system security settings.
To hide the password just use
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
the rest is taken care of by the operation system.
In my case i wondered about different behaviour on a variation of devices.
After some investigation the security setting 'Visible Passwords' was turned off on the device which did not show the last character.
If you must ignore the system wide setting, AndyFaizan 's password transformation solution is still the way to go.

Related

editable.getSpans is returning a StyleSpan, even when there isn't any style rendered on screen

I am writing a RichTextEditor class, which has an inner RichTextEditorTextWatcher class. I am seeing a discrepancy from what I see on the screen, versus what I get when I call e.getSpans inside beforeTextChanged.
On the screen, I see text on the screen (in my case, a single character) that does not have any style applied to it, but the e.getSpans() call actually says that I have a bold style applied.
Is this a known Android bug?
public class RichTextEditor extends AppCompatEditText
{
// other code not shown
public class RichTextEditorTextWatcher implements TextWatcher
{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
if (after == 0) //deletion occurred
{
isDeletion = true;
Editable e = RichTextEditor.this.getText();
/** The next line is the problematic line!
* this.prevStyles returns a StyleSpan (bold) even when I don't see it on the screen for that character.
*/
this.prevStyles = e.getSpans(start, start+count, CharacterStyle.class);
for (CharacterStyle c : this.prevStyles)
{
if (c instanceof StyleSpan)
{
if (((StyleSpan)c).getStyle() == Typeface.BOLD)
boldButton.setChecked(true);
else
boldButton.setChecked(false);
}
}
}
else
isDeletion = false;
}
}
}

Android Studio IDE Error Indication

I am relatively new to StackOverflow and Android Studio so apologies for newbie question!
I recently switched over to Android Studio from Eclipse.
I am noticing that when one function in one of my source files has an error that ALL functions in that file on the project view pane on the top left are showing the red squiggly so it is a little more time consuming to actually get to the error. Any ideas as to why this is happening?
Thank you
As Shlublu points out, it is difficult to say without an example. Here is one possible situation. Take this class:
public class MyClass {
private int myNumber;
public MyClass( int number) {
this.myNumber = number;
}
public int getAnswer() {
return this.myNumber;
}
private void someMethod() {
int x = 0;
} // remove this } and all below is an error
#Override
public String toString() {
return String.valueOf(this.myNumber);
}
#Override
public boolean equals(Object obj) {
return true;
}
}
Removing the } at the end of the someMethod() causes all methods below that to have an error.

Android - can you have multiple variable types for single method() parameter?

Can you have two or more variable types for a single method() parameter?
At the moment, you can do:
method(String string, int int, etc...) {
}
What if you wanted to do something like
method(String or int stringint) {
}
can this be done?
I'd do it like this:
private boolean function(String str){
// Do stuff
}
private boolean function(int intStr){
String str = convertToString(intStr);
return function(str);
}
Avoids unnecessary classes, etc.
Just overload the method signature.
public void someMethod(string argument){
}
public void someMethod(int argument){
}
If you have to stick to one single parameter you can use a wrapper, which contains two fiels:
public class MyWrapper
{
String stringValue;
int intValue;
}
public void someMethod(MyWrapper arg)
{
if(arg.stringField != null)
{
// do something with the string
}
/* checking for the default value 0 makes no sense here, since it
might be a value you actually want to pass - The first conditional
statement covers the case you actually only passed a string
*/
else
{
// do something with the int
}
}

How to change password field to diplay asterisks instead of dots

I am working on the task that requires the password field (i.e.the Edit Text) to hide user input using asterisks(*) rather than dots(.). Currently it shows as dots.
Kindly tell me the way to do it if its possible using android's native methods. Or please post the code to do it if anyone has already done that.
Thanks in advance..
Very late answer, and I'm sure you don't care anymore, but someone else might.
Initialize EditText Field .
EditText UPL =(EditText) findViewById(R.id.UserPasswordToLogin) ;
UPL.setTransformationMethod(new AsteriskPasswordTransformationMethod());
Then Create a new java class ,Called AsteriskPasswordTransformationMethod.java Which extends PasswordTransformationMethod
Here is code :
import android.text.method.PasswordTransformationMethod;
import android.view.View;
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
}
}
};
public final void setTransformationMethod (TransformationMethod method)
Since: API Level 1
Sets the transformation that is applied to the text that this TextView is displaying.
Related XML Attributes
android:password
android:singleLine
allows you to change any char
I would imagine you could override the listener class methods to modify the text to display so that it reads as "*", but keep the actual string in the background somewhere. So each time the user enters a letter, you add it to your cumulative "password" string, and instead, replace that character in the displayed string with *

Set edittext field to be a password?

android:password="true"
This hides the letters (****) but not immediately! When I type the letters, it take a while to hide.
For example, at the moment I type "a"; it shows ***a then it becomes ****.
How can I transform it immediately?
I believe this behaviour is intentional, as Android is used on smartphones with tiny little keyboards (physical and on-screen) where it's easy to make a typo. Displaying the letter briefly is so that the user can see if they typed something wrong, rather than hiding it and having no idea until they get an "incorrect password, your account has now been locked" type error!
I believe that the android:password="true" assigns a TransformationMethod to the text field which is responsible for converting the text into dots. I'm not an Android developer, but from reading the documentation I would imagine that this TransformationMethod has the delay built into the afterTextChanged callback. You could try writing your own TransformationMethod and play around with this and see if you can create your own version of the password masking rather than using the built-in one.
Just keep in mind the warnings in the doc, though, about avoiding infinite loops, because updating the text can re-trigger the events that you were notified about initially.
Implementation of TransformationMethod to hide letters when writing password:
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// example of usage
((TextView) findViewById(R.id.password)).setTransformationMethod(new HiddenPassTransformationMethod());
}
private class HiddenPassTransformationMethod implements TransformationMethod {
private char DOT = '\u2022';
#Override
public CharSequence getTransformation(final CharSequence charSequence, final View view) {
return new PassCharSequence(charSequence);
}
#Override
public void onFocusChanged(final View view, final CharSequence charSequence, final boolean b, final int i,
final Rect rect) {
//nothing to do here
}
private class PassCharSequence implements CharSequence {
private final CharSequence charSequence;
public PassCharSequence(final CharSequence charSequence) {
this.charSequence = charSequence;
}
#Override
public char charAt(final int index) {
return DOT;
}
#Override
public int length() {
return charSequence.length();
}
#Override
public CharSequence subSequence(final int start, final int end) {
return new PassCharSequence(charSequence.subSequence(start, end));
}
}
}
}

Categories

Resources