I've got the following xml:
<AutoCompleteTextView
android:id="#+id/searchQuery"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:drawablePadding="25dp"
android:drawableRight="#drawable/ic_search_black_18dp"
android:ems="8"
android:focusable="true"
android:focusableInTouchMode="true"
android:imeOptions="actionSearch"
android:inputType="text" />
I want to make icon move, but drawablePadding has no effect. What is wrong?
drawablePadding is padding between the drawable and rest of your widget.
use padding attribute to provide padding around your widget including drawable.
According to your code, you are giving static height to your AutoCompleteTextView i.e. 30dp. That's the reason the drawable padding is not visible.
Try using wrap content if you want drawable padding visible.
drawablePadding only take effect when the widget view size is small enough.
For drawableRight and drawableLeft, drawablePdding only works when the view's width is wrap_content.
If the view's width is match_parent or it has a specific big value for width or minWidth, the drawable will be sitting on the edge.
For the layout:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="4dp"
android:drawableRight="#drawable/red_dot_indicator"
android:gravity="center"
android:text="Hello World!" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="4dp"
android:drawableRight="#drawable/red_dot_indicator"
android:gravity="center"
android:text="Hello World! 2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="4dp"
android:drawableRight="#drawable/red_dot_indicator"
android:gravity="center"
android:minWidth="200dp"
android:text="Hello World! 3" />
The screenshot is like:
Related
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"
Is it possible to add padding into TextView with drawable element on its backround? Or do I need to use Draw 9-patch ?
<TextView
android:layout_width="wrap_content"
android:layout_gravity="bottom|left"
android:layout_marginLeft="5dp"
android:layout_height="85dp"
android:background="#drawable/text_background"
android:id="#+id/counter"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="bold"
android:gravity="center"
android:paddingLeft="2dp"
android:paddingRight="2dp"
/>
This padding settings does not work. For 3-4 digits the background is displayed correctly but when it should adjust (for 5/6 digits) it stays the same.
UPDATE - add image
If You are adding drawable as background then drawablePadding doesn't works.
You just have to give proper padding to you TextView, so that it can handle most of your usecases.
<TextView
android:id="#+id/txt_bankid_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/layout_user_detail"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/spacing_huge"
android:background="#drawable/textview_selector"
android:clickable="true"
android:textAllCaps="true"
android:padding="#dimen/spacing_small"
android:text="#string/login_bankid"
android:textColor="#color/app_background_color"
android:textSize="#dimen/font_normal" />
Never give static height or width to your layouts as you have used in height. It will create problem in different screens and you will face text cutoff.
I'm facing problems with the position of an icon. I've set that icon as drawableLeft of a TextView, this is working fine but, if I set the TextView's gravity to center_horizontal, then a padding space appears between the icon and the text. The code is as follows:
<TextView
style="#style/BaseText.White.Bold"
android:gravity="center_horizontal"
android:drawableStart="#drawable/ic_text"
android:drawableLeft="#drawable/ic_text"
android:text="#string/text"
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginRight="#dimen/margin_big"
android:layout_marginLeft="#dimen/margin_big"
android:padding="0dp"
android:drawablePadding="0dp"/>
What do I have to do to ensure that the icon is shown at the text's left side but horizontally centered?
I've make two screenshots to show you the difference when adding or removing the icon's gravity.
When the textview's gravity is not set, the result is as follows:
But, when the gravity is added, then a padding appears between the text and the icon:
Thanks in advance
Only add android:drawablePadding="10dp" field in your text view refer the coad
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add New Address"
android:textAppearance="#style/Title"
android:id="#+id/tvAddNewAddress"
android:drawablePadding="20dp"
android:layout_weight="1"
android:padding="5dp"
android:textColor="#color/colorAccent"
android:drawableStart="#android:drawable/arrow_down_float"
/>
Try to give android:gravity="center" to Text View. Also add android:layout_width="wrap_content" and android:layout_height="wrap_content" properties in Text View.
Refer this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:drawableLeft="#mipmap/ic_launcher"
android:drawablePadding="0dp"
android:drawableStart="#mipmap/ic_launcher"
android:gravity="center"
android:textSize="22sp"
android:padding="0dp"
android:text="TestingTestingTestingTestingTestingTestingTestingTestingTesting" />
</RelativeLayout>
This is what it showing.
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!
I can't get text centered while I have drawableleft image next to it. And how to add padding left for that image too?
Here's my xml code:
<TextView
android:id="#+id/matematikaText"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#4CAF50"
android:drawableLeft="#drawable/mathico"
android:gravity="center"
android:height="#dimen/abc_action_bar_default_height"
android:padding="0dp"
android:text="MatemÄtika"
android:textAppearance="?android:attr/textAppearanceLarge" />
To add padding to drawable,
android:drawablePadding="8dp"
set gravity to
android:gravity="center"