I have this TextView:
<TextView
android:id="#+id/silverCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="silver"
android:drawableTop="#drawable/silver"
android:text="3" />
Which looks like this:
How can I center the 3 inside the View to be situated directly below the drawable I set with android:drawableTop?
You need to use gravity instead of layout_gravity:
<TextView
android:id="#+id/silverCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:contentDescription="silver"
android:drawableTop="#drawable/silver"
android:text="3" />
This will center the 3 inside the TextView to appear directly below the drawable like this:
Related
I am having two TextView in LinearLayout.
<LinearLayout
android:layout_marginTop="-10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/product_discounted_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/margin_between_prices"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="AED200000000.00"
android:textColor="#color/black"
android:textSize="#dimen/font_size_sub_normal"
android:visibility="visible" />
<TextView
android:id="#+id/product_unit_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/activity_vertical_margin"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="AED200000000000.00"
android:textSize="#dimen/font_size_emphasized"
android:visibility="visible" />
</LinearLayout>
I want something like if 2nd TextView doesnt come in single line than it should shift to second line.
Issue is :
With SingeLine=true
With maxLine=1
What I want :
change
android:orientation="horizontal"
to
android:orientation="vertical"
and also add maxline and singleline to textView
It is easy put your text views in a RelativeLayout and all you need to set layout_above or layout_bottom for your TextViews
or you can change orientation:vertical and then the gravity works.
You can also set layout_gravity
i have this code for displaying an image on a button.
How to position the image so that it doesn't touch the frame/border?
this the code:
<Button
android:layout_width="0dp"
android:layout_weight="0.45"
android:onClick="EXIT"
android:layout_height="95dp"
android:text="Exit"
android:textSize="22sp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:background="#drawable/buttonnew"
android:textStyle="bold"
android:drawableTop="#drawable/zexit"
android:textColor="#000000"
android:layout_gravity="bottom"
/>
Since you are referring to the Button rather than the drawable, it would be best to use padding instead of margin, e.g. android:paddingTop
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!
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
Here's the layout (below). I'm trying to reposition the location of the checkbox; move it to the right of the view. android:gravity and android:layout_gravity seem to have no effect. Any explanation? This LinearLayout is a child of a Relative Layout.
<LinearLayout
android:id="#+id/deviceLinear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#id/view1" >
<ImageView
android:contentDescription="#string/devices_icon"
android:id="#+id/devices_img"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:layout_gravity="center"
android:src="#drawable/hardware_phone" />
<TextView
android:id="#+id/devices"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.43"
android:text="#string/devices"
android:textSize="20sp" />
<CheckBox
android:id="#+id/check_devices"
android:button="#drawable/custom_checkbox"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.42"
android:onClick="onCheckboxClicked" />
</LinearLayout>
I assume that you want the CheckBox to be on the right side of the LinearLayout. Since you have given it a layout_weight it will always take up some fixed percentage of the width regardless of its size as you can see by that blue bounding box.
Try wrapping the checkbox in another LinearLayout that has android:orientation='horizontal'. Give the 0.42 weight to the wrapping LinearLayout and then set the LinearLayout's android:gravity to be right. That should keep your spacing while moving the checkbox to the far right.
Something like this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_weight="0.42"
android:layout_height="wrap_content"
android:gravity="right" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
You might also want to consider a RelativeLayout which enables you to position Views based on where other views are.
You are giving layout_weight="0.42" to your checkbox, that means that it will be measured with a width based on the LinearLayout's width. By this approach you will never manage to put it on the right.
The best way to achieve your goal is to use a RelativeLayout. Here is my list item layout with a checkbox on the right. Sorry about the merge tag but it is merged inside a RelativeLayout with
android:layout_width="match_parent"
android:layout_height="wrap_content"
attributes
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="#+id/imgChannelIcon"
android:layout_alignParentLeft="true"
style="?muListItemImage60x60" />
<CheckBox android:id="#+id/chkIsChecked"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignTop="#+id/imgChannelIcon"
android:layout_alignParentRight="true"
android:layout_alignBottom="#+id/imgChannelIcon"
android:layout_marginLeft="#dimen/list_item_checkbox_margin_left"
android:layout_marginRight="#dimen/list_item_checkbox_margin_right"
android:layout_marginTop="#dimen/list_item_checkbox_margin_top"
android:layout_marginBottom="#dimen/list_item_checkbox_margin_bottom"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView android:id="#+id/lblChannelTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgChannelIcon"
android:layout_alignTop="#+id/imgChannelIcon"
android:layout_toLeftOf="#+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:textStyle="bold"
android:text="#string/channel_item_dummy_title"
style="?muListItemTextBig" />
<TextView android:id="#+id/lblChannelSubtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgChannelIcon"
android:layout_below="#+id/lblChannelTitle"
android:layout_toLeftOf="#+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:textStyle="normal"
android:text="#string/channel_item_dummy_subtitle"
style="?muListItemTextNormal" />
<TextView android:id="#+id/lblChannelCategory"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="#+id/imgChannelIcon"
android:layout_below="#+id/lblChannelSubtitle"
android:layout_alignBottom="#+id/imgChannelIcon"
android:layout_toLeftOf="#+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:paddingLeft="3dp"
android:gravity="center_vertical|left"
android:textStyle="italic"
android:text="#string/channel_item_dummy_category"
style="?muListItemTextNormal_Inverse" />
<TextView android:id="#+id/lblChannelUpdate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/imgChannelIcon"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:includeFontPadding="false"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textStyle="italic"
android:text="#string/channel_item_dummy_update"
style="?muListItemTextTiny" />
</merge>
As you can see I first place an image with layout_alignParentLeft="true" then the checkbox with layout_alignParentRight="true" and finally I arrange the other components based on those two. From your image I can see that you can use your devices_img as the left component (but you will have to give it a fixed height and maybe some margins in order to became the Layout's height) and your check box as your right.
Keep in mind that in a RelativeLayout with wrap_content as height you cannot use the AlignParentBottom attribute.
Hope this helps...