TextView text and its drawable to the center of textview - android

I have a textview with a drawableleft. As the textview is covering whole width of the screen so i want textview text and its drawable left to the center of the textview. At present only text is aligning to the center of it and drawable is always in the extreme left of it. So please suggest me something.
<TextView
android:layout_marginLeft="#dimen/_10sdp"
android:layout_width="0dp"
android:layout_height="#dimen/_40sdp"
android:layout_weight="5"
android:gravity="center"
android:drawableLeft="#drawable/ic_cloud"
android:paddingLeft="#dimen/_10sdp"
android:text="#string/up"
/>

You cannot achieve this using drawableleft. Use separate Layout with ImageView and TextView as shown below:
<RelativeLayout
//add necessary attributes
android:gravity="center_horizontal">
<ImageView
//Add your image here
/>
<TextView
android:layout_marginLeft="#dimen/_10sdp"
android:layout_width="0dp"
android:layout_height="#dimen/_40sdp"
android:layout_weight="5"
android:paddingLeft="#dimen/_10sdp"
android:text="#string/up" />
</RelativeLayout/>
Hope this helps

Related

How to allign a textView according to its center in Android

Very simply put, I want to allign a textView according to its center instead of its border, so lets say, when using android:layout_marginRight="50dp", instead of the right border being 50dp away, the center of the textView being 50dp away from the parent's right border. Here is my code for reference:
<RelativeLayout
android:id="#+id/breakfastStats"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/breakfastAdd"
android:layout_marginTop="1dp"
android:background="#color/lightblack">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Carbohydrate"
android:textColor="#color/white"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:gravity="center"
android:layout_marginTop="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fat"
android:textColor="#color/white"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
/>
Fixed by using a GridLayout instead of a RelativeLayout.
You can use margin and padding value together for the TextView.
marginRight = "25dp"
paddingRight = "25dp"
gravity = "center"

Textview next to an ImageView

I am trying to have a TextView with an ImageView directly to its right. Right now the image partially overlaps the far right of the text, so if the text inputted is long, then the image is "over" the text. I have an ellipse setting on the text but I need the TextView to "stop" right at the left border of the ImageView.
TextView layout settings:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
ImageView layout settings:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
Try this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/iv"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"
.... />
<!-- background == #null if you don't want default button background -->
<ImageButton
android:id="#id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="3dp"
android:background="#null"
.... />
</RelativeLayout>
EDIT
See the comments in regards to why this works and the posted question doesn't. Also take a look at
Android Relative Layout alignParentRight and alignParentEnd
for a reference to end/right and start/left attributes.

how to align the drawable to left of text in textview?

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?
Use these tags to set the image in your textView.
android:drawableLeft="#drawable/your_image"
android:drawablePadding="6dp"
Hope this helps!
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" />
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"/>
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"
/>
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
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!

Customize AppCompatButton drawableLeft?

How can I place drawableLeft and button text of AppCompatButton like in the image below?
I have tried to apply paddingLeft, but the text is moving away from the drawable to the right.
<android.support.v7.widget.AppCompatButton
android:id="#+id/filterBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_filter"
android:paddingLeft="20dp"
android:text="#string/filter_by"
android:textAllCaps="false" />
And so far the result is looking like
Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/profileHeader"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/findFriends"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_search"
android:text="#string/find_friends"
android:textAllCaps="false" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/filterBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_filter"
android:text="#string/filter_by"
android:textAllCaps="false" />
</LinearLayout>
And the interpreted requirement for making things clear.
Using layout_weight property am adjusting the width of both buttons and what i have to do is, the text should be in center of the button
and the drawable should be aligned near left of the text.
Any suggestion please?
use
android:drawablePadding=""
probably you can't, if you want like this then you have to make it using complex layout like this see

Align ImageView to center of a TextView in RelativeLayout

I have a Relative Layout as below:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.38"
android:background="#drawable/layout"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:layout_marginLeft="80dp"
android:textColor="#color/text1color"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/text1"
android:layout_below="#+id/text1"
android:background="#drawable/text2_background"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:textColor="#color/text2color"
android:textSize="13sp" />
<com.widgets.CustomImageView
android:id="#+id/myImage"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="0dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/text2"
android:src="#drawable/logo" />
</RelativeLayout>
The ImageView #+id/myImage needs to align to the center of #+id/text2 TextView! Currently, it is aligning to the top of #+id/text2 and is aligning to the layout's parentBottom. I don't want this hack.
Is there a way of aligning the ImageView to the TextView's center?
This is a perfect case for a compound drawable (which is a best practice, to flatten your design).
The ImageView goes INSIDE the TextView text2, at its bottom:
Just add this to your text2 definition:
android:drawableBottom="#drawable/logo"
You can add some padding, too
android:drawablePadding="4dp"
The image is horizontally aligned to its container.
If the text grows vertically, the image will be pulled down (as the TextView grows).
To have the image left aligned and vertically centered, just change drawableTop with drawableLeft and you're done:
android:drawableLeft="#drawable/logo"

Categories

Resources