7

I know that in Android we can use function setCompoundDrawables(..) to add drawable.

My problem is that my EditText is multy-lines and i want to put drawable in EditText's top|left corner. But using at setCompoundDrawables() function, only give choice to put drawable in specific side.

Is it possibility at Android API to Do it?

blay
  • 464
  • 9
  • 23
  • Check this one - http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds%28int,%20int,%20int,%20int%29 – g00dy Jul 24 '13 at 07:18
  • 2
    You can replace the `setCompoundDrawables(..)` with `setCompoundDrawablesWithIntrinsicBounds()` like so -> `EditTextId.setCompoundDrawablesWithIntrinsicBounds(0, myIcon, 0, 0);` – g00dy Jul 24 '13 at 07:20
  • What value i can put in myIcon? i know that is int.. between which values i can choose? – blay Jul 24 '13 at 07:38
  • `myIcon` is just an example, but you can put the same, as you did in the `setCompoundDrawables(..) ` method, if you paste the code you got I can help you on it. :) – g00dy Jul 24 '13 at 07:39
  • I found . int = the drawble id. – blay Jul 24 '13 at 07:41
  • Ok, so try it on and see if it helps you ;-) – g00dy Jul 24 '13 at 07:42
  • `myIcon ` is the drawable resource that you want to add in the `EditText`. – g00dy Jul 24 '13 at 07:54
  • it's Not work. It's active like setCompoundDrawables(..) – blay Jul 24 '13 at 08:03
  • What do you mean it's active - (do you mean it's acting like ..) ? – g00dy Jul 24 '13 at 08:25
  • yes.. it's what i mean. it's dont give dynamism of drawable location into EditText – blay Jul 24 '13 at 08:39
  • Did you try this one: `EditText.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.drawableIdOfThePicture), null);` – g00dy Jul 24 '13 at 08:42
  • yes. its what i do first. – blay Jul 24 '13 at 08:45

2 Answers2

5

Seems it's impossible to do it with only EditText / TextView without anyside effect and additional views (I've provided the way at the end, but it needs more work with image and can be not applicable for every phone, because of different edit text backgorunds depending of manufature). The same question have been asked already many times: here and here for example.

Per my understanding the easiest and fastests way might be not as suggested in above questions (by usage of RelativeLayout), but just using FrameLayout the following way (You'll have 2 views vs 3 view using RelativeLayout+ImageView): Make your icon 9patch (using 9patch tool). E.g. you have the following icon initially:

original icon

Then You can convert it to the following one:

enter image description here (You can see black pixels which shows area to be stretched with view and areas for content)

Using the following xml:

    <!-- Main activity layout -->
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <!-- Edit text with drawable at top left -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/icon_9patch">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/edit"
                android:hint="Enter text"
                android:maxLines="10"
                android:inputType="textMultiLine" />
        </FrameLayout>
    </RelativeLayout>

Gives the following result (I've added multiple lines of text):

result image

Moreover, it's even possible to comply the task with only EditText (but You might loose default EditText background which might not be good, however You can include it into your ning-patch image; but it will still be the same on all phones instead of native one).

The following xml also works fine:

<!-- Main activity layout -->
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Edit text with drawable at top left -->
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit"
            android:hint="Enter text"
            android:maxLines="10"
            android:inputType="textMultiLine"
            android:layout_alignParentBottom="true"
            android:background="@drawable/icon_9patch" />
</RelativeLayout>

Give the following result (note that default frame for edit has disappeared): single view appearance

Community
  • 1
  • 1
sandrstar
  • 12,503
  • 8
  • 58
  • 65
0

Try this one :

           <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/scale_10"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/map_mark"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/scale_10"
                    android:gravity="top"
                    android:text="TEXT HERE"/>
            </LinearLayout>
Anand Savjani
  • 2,484
  • 3
  • 26
  • 39