Holi Boom
Holi Boom

Reputation: 1396

How to padding bottom of the customize EditText character?

I have a customize EditText character with some simple character and image.

Look like this image : enter image description here

But i want the image padding the bottom like this image :

enter image description here

This is my pwd_bullet.png : enter image description here

This is my code :

  1. custom_edittext.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5b5555"
    tools:context="com.example.user.myapplication.Custom_Edittext">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:id="@+id/editText"
                android:inputType="text"
                android:gravity="center_vertical"
                android:background="@null"/>
        </LinearLayout>
    </RelativeLayout>
    
  2. Custom_Edittext.java

    public class Custom_Edittext extends Activity implements TextWatcher{
         EditText editText;
         Spannable.Factory spannableFactory;
         int lastIndex = -1;
    
    protected void onCreate(Bundle savedInstanceState) {
         enter code here`super.onCreate(savedInstanceState);
         setContentView(R.layout.custom_edittext);
         editText = (EditText) findViewById(R.id.editText);
         spannableFactory = Spannable.Factory
            .getInstance();
         editText.addTextChangedListener(this);
    }
    public Spannable getIconText(Context context, CharSequence text, int index) {
        Spannable spannable = spannableFactory.newSpannable(text);
        if (index>lastIndex) {
            spannable.setSpan(new ImageSpan(context, R.drawable.pwd_bullet),
                index, index + 1,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        lastIndex=index;
        return spannable;
    }
    
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (start>4) {
            editText.removeTextChangedListener(this);
            editText.setText(getIconText(getApplicationContext(), s, start));
            editText.addTextChangedListener(this);
            editText.setSelection(s.length());
        }
    }
    
    public void afterTextChanged(Editable s) {}
    }
    

Upvotes: 0

Views: 104

Answers (2)

Holi Boom
Holi Boom

Reputation: 1396

Create an xml to custom pwd_bullet in drawable

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/layer_list"
    android:drawable="@drawable/pwd_bullet"
    android:width="10dp"
    android:height="10dp"
    android:left="3dp"
    android:bottom="5dp"/>
</layer-list>

Upvotes: 1

Harian Manda
Harian Manda

Reputation: 258

There is no need to use padding in this case, Just use a Linear layout since your items are in a straight line, and make sure you set the Linear layout to an orientation and the gravity as below. This worked for me. Good luck

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:id="@+id/editText"
        android:inputType="text"
        android:gravity="center_vertical"
        android:background="@null"
        android:text="Simple Text" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/pwd_bullet.png"/>
</LinearLayout>

Upvotes: 1

Related Questions