Why my TextView
doesn't go right?
Update: Well, now I don't just need to set TextView
to the right. Now it is very interesting why layout_gravity
doesn't work as expected, namely - set the View
to the position inside it parent container.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
style="@style/activated_item"
android:orientation="horizontal">
<CheckBox
android:id="@+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
style="?android:attr/starStyle"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="8dp"
android:layout_gravity="right" //////// HERE I AM
android:textStyle="bold"
android:textColor="@color/item_text_color"/>
</LinearLayout>
The layout_gravity attribute can be used to position the Button as a whole with the Button text centered in side it. The gravity attribute can be used to position the text inside the Button. See the screenshot below showing the use of the layout_gravity attribute and the gravity attribute for the Button widget.
android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on. android:layout_gravity sets the gravity of the View or Layout relative to its parent.
android:gravity is an attribute that sets the gravity of the content of the view its used on. The android:gravity specifies how an object should position its content on both X and Y axis. The possible values of android:gravity are top, bottom, left, right, center, center_vertical, center_horizontal etc.
I have found 2 solutions to that:
android:orientation="vertical"
.FrameLayout
instead of LinearLayout
as the parent for your text/checkbox widgets. It is not primary usage of FrameLayout
but that advice is also noted in Android documentation:
... You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
Try to use gravity
instead of layout_gravity
.
Because you set orientation
to horizontal, which means you cannot manually change horizontal position of your child views.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With