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 *
Related
In Android, how do I take an action whenever a variable changes?
So I want to implement a listener for an object I created. What I want it to do is execute a block of code when its value changes from false to true.
As I am following this thread, I can't understand where the person wants us to implement the last block of code containing the logic for the listener.
Could someone, hopefully, guide me in the right direction?
(This question is being asked here as I don't have enough rep. points)
That last bit of example code triggers the listener, so it basically needs to be run whenever the "event" occurs. In this case the "event" is whenever (wherever in the code) the value of the variable changes.
If you have a setter and that is the only place the value changes, that is where you'd put it. If you are changing the value in multiple places throughout your code, I would make a new private method (call it signalChanged), put your code there, and then call it immediately after the variable assignment in the cases you want the listener to fire.
Here's an example (some code borrowed from linked answer, haven't checked that it compiles).
public class MyObj
{
public MyObj(int value)
{
setValue(value);
}
private int myValue;
public int getValue() { return myValue; }
public void setValue( int value )
{
if (value != myValue)
{
myValue = value;
signalChanged();
}
}
public interface VariableChangeListener
{
public void onVariableChanged(Object... variableThatHasChanged);
}
private VariableChangeListener variableChangeListener;
public void setVariableChangeListener(VariableChangeListener variableChangeListener)
{
this.variableChangeListener = variableChangeListener;
}
private void signalChanged()
{
if (variableChangeListener != null)
variableChangeListener.onVariableChanged(myValue);
}
}
you have to create a callback interface
here is a good about custom listener tutorial
here is a sample
public class MyObj {
VariableChanger onVariableChanged ;
public void setOnVariableChanged(VariableChanger onVariableChanged) {
this.onVariableChanged = onVariableChanged;
}
void log(){
boolean changed = false;
onVariableChanged.onVariableChanged();
//this will call it
}
interface VariableChanger{
void onVariableChanged();
}
}
class logic {
MyObj mo = new MyObj();
void main(){
mo.setOnVariableChanged(new MyObj.VariableChanger() {
#Override
public void onVariableChanged() {
//do your action
}
});
}
}
In Android, like any language, most developper uses logic comparisons to check values (if, else, switch, =, !=, >, <, etc) or Event (signal)
What kind of listener do you want to implement?
I am a beginner in this field.
How can i access a method from one package to another.
For example:
package add;
public class Addfunction {
int a,b,sum;
public int add(int x,int y)
{
a=x;
b=y;
sum=a+b;
return sum;
}
}
in my second package
package com.example.demoo;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int Result;
Addfunction addfunction=new Addfunction();
Result=addfunction.add(5, 10);
Toast.makeText(getApplicationContext(), Result , Toast.LENGTH_LONG).show();
}
}
while running it shows Unfortunately app is stop.
please help me to solve this.
There's nothing wrong in how you access the Addfunction class. But consider two things.
First, you must add an import for the Addfunction class before the MainActivity class definition:
import add.Addfunction;
although you probably already have that, because it wouldn't compile otherwise.
Second, you are probably using this makeText:
static Toast makeText(Context context, int resId, int duration)
Make a standard toast that just contains a text view with the text from a resource.
instead of this:
static Toast makeText(Context context, CharSequence text, int duration)
Make a standard toast that just contains a text view.
Note the second parameter is an int in the first case which refers to a resource. And you are just passing the result of your computation which is probably an invalid resource id.
You may want to try to build a message in a String object and pass it to makeText, like this:
String msg = "Result: " + Result;
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
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
}
}
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.
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));
}
}
}
}