Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center text & images in a CompoundDrawable?

Tags:

A CompoundDrawable is a TextView with an image and a text right? So if it's a combination of the two, how can you put the text in the center with respect to the image? Or how can you put the image in the center with respect to the text if the text is a bit smaller compared to the image? Or what if the image is smaller than the text? How could I cover these scenarios using XML?

Here is an example of a CompoundDrawable:

<TextView      android:id="@+id/image"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:drawableTop="@drawable/image"      android:text="@string/text" /> 
like image 639
Giant Avatar asked Apr 26 '13 01:04

Giant


2 Answers

Add android:gravity="center" to your TextView.

like image 129
Karakuri Avatar answered Oct 03 '22 03:10

Karakuri


cool stuff with android:drawablePadding="-20dp" android:paddingLeft="20dp"    <Button         android:id="@+id/btn_pre"         style="?android:buttonBarStyle"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginRight="1dp"         android:layout_weight="1"         android:background="@color/bottom_tab"         android:contentDescription="@string/image_view"         android:drawableLeft="@drawable/prev"         android:drawablePadding="-20dp"         android:onClick="navigateStaticPages"         android:paddingLeft="20dp"         android:text="@string/pre"         android:textColor="#000"         android:textSize="@dimen/login_textSize" /> 
like image 42
Vinod Joshi Avatar answered Oct 03 '22 03:10

Vinod Joshi