Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place Compound drawable on top left of Text View

We can set compound drawables for textview as below using drawableLeft or setCompundDrawables but using this its only possible to place the drawable on left / right / top / bottom.But i want to put drawables only at top-left and top-right of the Textview. How can i do that ?

like image 737
Bunny Rabbit Avatar asked Feb 06 '13 10:02

Bunny Rabbit


2 Answers

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView1"
    android:text="@string/text" />

This would do it.

like image 164
jlopez Avatar answered Oct 14 '22 00:10

jlopez


I think, there's an easier and faster way than have 3 views. You need to prepare proper 9patch for icon and then use FrameLayout. It's even possible to have only single EditText / TextView using the same drawable as their background. More details are described in this answer.

like image 33
sandrstar Avatar answered Oct 14 '22 02:10

sandrstar