简体   繁体   中英

How to add two drawableRight in EditText?

I am able to add one drawableRight in EditText in android easly and on click event working perfectly in case of one drawableRight . But I need two drawableRight in EditText .

So, How can I add two drawableRight in EditText ? and I also need to perform click event on both drawableRight separately.

For example I want to add yellow star in EditText like as given below image and on click on rightmost image I want to open contact book of phone and on click of yellow star I want to call user's favourite numbers list.

So How can I do this? Any Idea?

多个可绘制

There is no native support for this, so you got two options here:

Easy solution: Create a Linear layout with tow image views on the right side, those will be your drawables.

The hard way: Extend a Drawable class and implement your own onDraw method where you will draw the two drawables. Than use that one for your text view.

You can't. The TextView can contain only one drawable on either of its sides. The only options you have are:

  1. Create custom View .
  2. Take some of the ViewGroup descendants ( RelativeLayout / FrameLayout /etc) and put the TextView along with the two ImageView s in it.

Just place two Drawables into EditText using RelativeLayout. To set inner padding, place an invisible drawableRight into EditText:

/res/values/dimens.xml

<resources>
    <dimen name="iconSize">32dp</dimen>
</resources>

/res/layout/my_layout.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/editText"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:inputType="textAutoComplete"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="@dimen/iconSize"
        android:layout_height="@dimen/iconSize"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_action_1"/>

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="@dimen/iconSize"
        android:layout_height="@dimen/iconSize"
        android:layout_toLeftOf="@+id/imageButton1"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_action_2"/>

</RelativeLayout>

In your Activity:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    EditText editText = (EditText) findViewById(R.id.editText);

    int iconSize = (int) getResources().getDimension(R.dimen.iconSize)

    Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_action_1);
    drawable.setBounds(0, 0, iconSize * 2, 0); // that is the trick!

    editText.setCompoundDrawables(null, null, drawable, null);

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM