I want to align the text of this two labels on it's bottom, so the two values are aligned on the same height.
Sadly, I dont find an option like aligning it inside its own area.
gravity wont work. It has to be in xml.
Thank you!
<TextView
android:id="#+id/feed_euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/feed_cent"
android:height="35dp"
android:text="23"
android:textColor="#color/colorTextBlack"
android:textSize="25dp" />
<TextView
android:id="#+id/feed_cent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:height="35dp"
android:text=",39 €"
android:textAlignment="textStart"
android:textColor="#color/colorTextBlack" />
If you want to set your textview text bottom
android:gravity="bottom"
or
if you want to set your whole component bottom
android:layout_gravity="bottom"
To align the text inside a TextView to its bottom, you can use
android:gravity="bottom"
(inside TextView tags)
You can change the gravity to bottom like below.
<TextView
android:id="#+id/feed_euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/feed_cent"
android:height="35dp"
android:text="23"
android:textColor="#color/colorTextBlack"
android:textSize="25dp" />
<TextView
android:id="#+id/feed_cent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:height="35dp"
android:text=",39 €"
android:gravity="bottom"
android:textAlignment="textStart"
android:textColor="#color/colorTextBlack" />
And also you can try android:gravity="bottom" to the layout you have used to hold the both TextView and change the 2nd TextView height into wrap_content.
Hope you got the answer.
Related
I have 2 rows of "buttons" which are composed of a vertical LinearLayout of an ImageView and a TextView. The problem I'm having right now is trying to set the ImageView aspect ratio without hardcoding its dimensions.
I have already tried playing with the layout weights but it still forces the TextView to be partially cut off.
The xml structure I have is 2 horizontal LinearLayouts and each button is described above.
I've included a picture of my problem. You can see the first button is forcing the TextView out of the bounds. The rest of the buttons you see are hardcoded dimensions, but I don't want to do that because smaller screen sizes won't work.
Here is my layout for one button:
<LinearLayout
android:gravity="center_horizontal"
android:layout_weight="1"
android:padding="5dp"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="match_parent">
<ImageView
android:id="#+id/setting_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#null"
android:clickable="true"
android:onClick="openSettings"
android:src="#drawable/menu" />
<TextView
android:id="#+id/setting_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:textAlignment="center"
android:textColor="#android:color/white"
android:autoSizeTextType="uniform" />
</LinearLayout>
Try using images with lower dimensions.
Try using a RelativeLayout and setting the TextView as anchor for the image:
<RelativeLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp">
<TextView
android:id="#+id/setting_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="Settings"
android:textAlignment="center"
android:textColor="#android:color/white"
android:autoSizeTextType="uniform" />
<ImageView
android:id="#+id/setting_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#id/setting_text"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="#null"
android:clickable="true"
android:onClick="openSettings"
android:src="#drawable/menu" />
</RelativeLayout>
I have two TextViews aligned in the "row", using the following layout:
<RelativeLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp">
<TextView
android:id="#+id/big_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="21sp"/>
<TextView
android:id="#+id/small_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
But, what happens is that since the left one is larger than the right one, the result looks like this (picture is just for illustration):
Instead, this is what I'm looking for:
Tried adding gravity="bottom" to the small TextView but it didn't work.
Also tried to wrap the small TextView in another layout, set it's layout height to match_parent (so it would take the height of the big TextView) and then set the small TextView gravity to bottom, but it also didn't work.
Edit:
plot twist: how to align it to the CENTER of the big TextView?
Add this line to your smaller textview
android:layout_alignBaseline="#+id/big_text"
or
android:layout_alignBottom="#+id/big_text"
like this:
<RelativeLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp">
<TextView
android:id="#+id/big_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="21sp"/>
<TextView
android:id="#+id/small_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_alignBaseline="#+id/big_text"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Try it in the second textview
<TextView
android:id="#+id/small_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/big_text"
android:layout_alignTop="#+id/big_text"
android:gravity="bottom"
android:textSize="14sp"
android:layout_alignParentRight="true"/>
Try this
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="BIG"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="small"
android:textSize="15sp"/>
</LinearLayout>
I am wanting a button to be aligned to the the bottom and right of a EditText box.
My code is below:
<RelativeLayout
android:id="#+id/global_only_available_home_delivery_postcode_check_layout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_gravity="right"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/global_only_available_home_delivery_postcode_check_text_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/delivery_edit_hint"
android:layout_marginLeft="#dimen/pdp_status_delivery_margin_5"
android:layout_marginTop="#dimen/pdp_status_delivery_margin_10"
android:textSize="14sp"
android:inputType="text" />
<Button
android:id="#+id/fragment_pdp_deliverygo"
style="#style/alternatesmallbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="35dp"
android:minHeight="35dp"
android:layout_marginLeft="10dp"
android:text="#string/pdp_delivery_go"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/global_only_available_home_delivery_postcode_check_text_phone"
android:layout_alignBottom="#+id/global_only_available_home_delivery_postcode_check_text_phone"
android:visibility="visible" />
</RelativeLayout>
and here is what I get button alignment
As you can see from the image, the button doesn't seem to be aligned to the bottom of the textview.
Can anyone help?
This is because you have android:layout_margin="5dp" for the button. This assigns a margin of 5dp to the bottom of the button and it appears as if it is not aligned to the bottom. You should remove it and assign margins individually to other sides if you want eg:
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" // etc (optional)
Try this..
Use android:layout_alignParentRight="true" and android:layout_centerVertical="true"
<Button
android:id="#+id/fragment_pdp_deliverygo"
style="#style/alternatesmallbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="35dp"
android:minHeight="35dp"
android:layout_marginLeft="10dp"
android:text="#string/pdp_delivery_go"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/global_only_available_home_delivery_postcode_check_text_phone"
android:layout_alignBottom="#+id/global_only_available_home_delivery_postcode_check_text_phone"
android:visibility="visible" />
Try using layout_below instead of layout_alignBottom
I have a problem with the page heading to be left-center of background irrespective of text size.
The text should be center of background irrespective of the text size.
I have used the following layout.xml for the textview:
<TableLayout
android:id="#+id/widget3d8"
android:layout_width="306dip"
android:layout_height="26dip"
android:gravity="fill_horizontal"
android:paddingRight="8px"
android:layout_x="5px"
android:layout_y="10px"
android:layout_weight="1"
android:background="#104082">
<TextView android:id="#+id/committeename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0px"
android:layout_y="100px"
android:paddingLeft="10px"
android:text=" "
android:textSize="12sp"
android:textStyle="bold"
android:textColor="#ffffff"/>
</TableLayout>
Please help me how to make the text to be left-centered of the backgound irrespective of the text size. Thanks in advance.
I'd make the TextView have the same height as it's parent and set the gravity to vertically center the content.
<TextView android:id="#+id/committeename"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_x="0px"
android:layout_y="100px"
android:paddingLeft="10px"
android:text=" "
android:textSize="12sp"
android:textStyle="bold"
android:textColor="#ffffff"/>
try
android:gravity="left|center_vertical"
Also set the textview to
android:layout_width="fill_parent"
android:layout_height="wrap_content"
I know it sounds easy. I need to put a text in center, but when the text is too long it needs to go below, but still align in the center of my xml.
Here's my code :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_centerHorizontal="true"
>
<TextView
android:id="#+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I put paddingTop and Bottom because I need some space.
PS: My code is bigger; it's in a RelativeLayout.
Set also android:gravity parameter in TextView to center.
For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.
use this way
txt.setGravity(Gravity.CENTER);
use this way in xml
<TextView
android:id="#+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time is precious, so love now."
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
/>
You can use the following:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your text"
android:typeface="serif" />
</LinearLayout>
The layout needs to be relative for the "center" property to be used.
Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear)!
Also, you should avoid using dp for font size. Use sp instead.
Just add these 2 lines;
android:gravity="center_horizontal"
android:textAlignment="center"
add layout_gravity and gravity with center value on TextView
<TextView
android:text="welcome text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
/>
android:layout_gravity="center"
or
android:gravity="center"
both works for me.
Or check this out this will help align all the elements at once.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
>
<TextView
android:id="#+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
If you want to change the alignment from the mainActivity.java file, then use the gravity. For example:
chatMsg.setGravity(Gravity.RIGHT);
chatMsg is my textView. I wanted the text to be right aligned and this code worked well.
you can easily done this by adding these lines
android:layout_width="match_parent"
android:gravity="center"
android:textAlignment="center"