Reputation: 8129
I want to display a drawable and text in a textview. When I use below code it is displaying both but drawable is in left most side of textview.
<app.com.myapp.views.TextViewMedium
android:id="@+id/approve_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/color_5eba6f"
android:gravity="center"
android:drawableLeft="@drawable/approve"
android:text="@string/notification_approve"
android:textColor="@color/color_FFFFFF"
android:textSize="@dimen/dp_14" />
My text is aligned in center. I want to align drawable to left of that text.Gap between drawable and text should be just 6 dp. Is it possible to do? Or do I need to create a layout with imageview and textview to achieve the same?
Upvotes: 3
Views: 3608
Reputation: 2128
to do that you must use relative layout like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--Your text view-->
<TextView
android:id="@+id/myText"/>
<!--Your image view-->
<ImageView
android:layout_toLeftOf="@+id/myText"
android:padding_right="6dp"/>
</RlativeLayout>
with that code you ImageView
is forced to be at the left of your TextView
and by 6dp
spacing
Don't forget you must use relativeLayout
as a parent layout so you can use toLeftOf
Upvotes: 0
Reputation: 2509
This is possible way to do it
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"
android:id="@+id/yourTextView"
android:textSize="12sp"
android:drawableLeft="@drawable/yourDrawable"
android:drawablePadding="6dp"/>
If not satisfies your demands you must use separate ImageView and TextView i think.. Hope to help you!
Upvotes: -1
Reputation: 9225
You need to use drawableLeft
attribute as below:
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/userFname"
android:drawableLeft="@drawable/user_name"
android:drawablePadding="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:textColor="@android:color/white"
/>
Upvotes: 0
Reputation: 627
This can be possible way :
<TextView
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="@drawable/edittext"
android:drawableLeft="@mipmap/icon_username"
android:drawablePadding="6dp"
android:gravity="fill_horizontal|fill|left"
android:padding="5dp"
android:singleLine="true"
android:textSize="15dp"
android:windowSoftInputMode="stateAlwaysHidden" />
Upvotes: 1
Reputation: 2849
Try this
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:id="@+id/handling"
android:textSize="20dp"
android:drawableLeft="@drawable/emoticons_bear"
android:drawablePadding="6dp"/>
Upvotes: 0
Reputation: 1472
Use these tags to set the image in your textView.
android:drawableLeft="@drawable/your_image"
android:drawablePadding="6dp"
Hope this helps!
Upvotes: 3