Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can change the icon on an EditText?

I make EditText to receive a password from the user.

So I changed the input type to password.

<EditText
                android:id="@+id/edittext_Password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/colorPrimary"
                android:inputType="textPassword"
                android:layout_weight="1"
                />

When I put word in EditText, it shows a black dot.
I want change the dot to a drawable resource.

enter image description here

like image 807
Polaris Nation Avatar asked Oct 29 '22 13:10

Polaris Nation


1 Answers

You can write code for your requirement following this code.

The answer comes from this tutorial and it covers a behaviour when a user:

  • enters into the login screen, keyboard will open automatically.

  • tries to enter value in it then textbox background changes to textbox with star background.

  • tries to cancel/delete the input value by using back key on keyboard then textbox background will change to textbox without star background.

First of all you have to create two drawables:

enter image description here

enter image description here

Then, according to this approach, you have to implement addTextChangedListener method on your EditText. After that, as a parameter, you create a new instance of a TextWatcher class and you implement its methods:

etxtPin1.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(etxtPin1.getText().toString().trim().length()==1){

          etxtPin1.clearFocus();
          etxtPin2.requestFocus();
          etxtPin1.setBackgroundResource(R.drawable.pin_txt_bg_star);

          }
       }
    });

Then, you have to implement setOnKeyListener and its method onKey:

this.etxtPin1.setOnKeyListener(new View.OnKeyListener() {
      public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) {
           if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (LoginActivity.this.etxtPin2.getText().length() == 0)) {
               etxtPin1.requestFocus();
               etxtPin1.setBackgroundResource(R.drawable.pin_txt_bg);
               etxtPin1.setText("");
           }

           return false;
       }
    });

enter image description here


Another approach: create you own class which extends PasswordTransformationMethod.

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
        }
    }
};

Reference: In android how to show asterisk (*) in place of dots in EditText having inputtype as textPassword?

Reference

like image 183
Khemraj Sharma Avatar answered Nov 08 '22 08:11

Khemraj Sharma