I have a view with 2 TextViews in a horizontal LinearLayout, each with a weight of 1.
The second TextView was like this:
<TextView
android:id="#+id/info_button"
style="#style/TextBlue"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/bg_ten_percent_black_selector"
android:layout_height="#dimen/card_button_height"
android:text="#string/shop_info"
android:gravity="center"
/>
For some reason, the text would not center horizontally when rendered. However, if I performed a touch on it, it would center then.
I have a fix where I wrap it in an extra LinearLayout which is given the weight, and center the TextView within that, but I was wondering why that workaround is effective when this isn't.
Also, what extra information might be needed here, since this method of centered text usually works. The views are inflated and added as ListView header.
What you are looking for is textAlignment in your case:
android:textAlignment="center"
try with center_vertical and center_horizontal in the textview attribute
android:gravity="center_vertical|center_horizontal"
I suspect you mean layout_gravity, not gravity
See: Gravity and layout_gravity on Android
Related
So I have a UI element (a single line of text) that I want horizontally centered with respect to the overall device -- unless/until it collides with other UI elements in the given view group / layout. At that point I'd like it to be either centered in the space remaining or pegged as close to being centered overall as possible without colliding. [When there's finally not enough space, then I want to use ellipses.]
Is there any way to achieve this using just standard Android layouts?
I'm currently achieving this via code that adjusts layout constraints when the view group's width changes, the text changes, or related UI elements become visible/invisible. It works fine, but I can't help thinking that some layout should just do this for me.
You can use a weighted horizontal LinearLayout like this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="i am centered"
android:ellipsize="end"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="another widget"/>
.
.
.
</LinearLayout>
The TextView with width 0dp and weight 1 will use the remaining horizontal space.
You can add additional widgets to the LinearLayout, and the TextView will always take the remaining space.
For example, if you change the Visibility of the Button to GONE, you'll see the TextView will expand to use the whole width. Similarly, if you programmatically add new widgets to the LinearLayout, the available space for the TextView will adjust.
You can further add ellipsize options to control what happens when the text does not fit in the TextView size.
When I use Android:margin to position textView half-outside it's parent, it acts weirdly: changes it's own size and text is moving inside textView box. How do I prevent it?
On image: left textView has cropped text at the end, and I don't want that.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="16dp"
android:clipChildren="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="none"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="22.5dp"
android:layout_marginRight="200dp"
tools:text="0 TEXT VIEWVIEW"
tools:textColor="#000000" />
<...>
</FrameLayout>
FrameLayout always overlaps its Children. For effective placing widgets I suggest to use,
LinearLayout with layout_weight
arrange widgets relative to another widget by using RelativeLayout
You have two problems here:
You are using a FrameLayout. This means that when you keep increasing the margin of child, the view is gonna move out of the parent. Why? Because that's how FrameLayout is designed. Read documentation here: http://developer.android.com/reference/android/widget/FrameLayout.html
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
So instead you could use RelativeLayout or LinearLayout.
The second problem you have is android:singleLine="true". This means that as the text increases in length, it will still be shown in a single line and hence the text will be clipped. So set this to false, or just remove this attribute.
android:singleLine="false"
i think you need to change the line:
android:singleLine="true"
to false.
My layout is the following:
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/pollenType"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/get_pollen_index"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="51dp"
android:textSize="40sp" />
As you can tell, Philadelphia, PA is left-aligned. No matter what I change, whether it's centering horizontal and vertical to true, it stays left-aligned.
I have tried changing the layout via the XML and graphical user interface. In the graphical user interface, it indeed is "centered", but it remains non-centered. My layout positions are consistent across all my textviews, so I am unsure as to why this particular TextView is not centered.
Two options...
One: Give the TextView android:layout_width="wrap_content"
Two: Keep the android:layout_width="match_parent" and use android:gravity="center_horizontal"
layout_gravity defines gravity of the view within its parent. Your textView already takes the full width because of match_parent, so centering it horizontally inside its parent does nothing.
gravity, on the other hand, defines the gravity of its contents.
You can center the text using the gravity xml attributes :
android:gravity="center"
Your width equals match_parent and therefore the edges of your TextView already touch the edges of the parent view. You should rather center the text in the TextView with android:gravity="center".
You're telling the display_city TextView to be as wide as the screen.
android:layout_width="match_parent"
However, its text is left aligned by default. That's why it looks this way.
You have two alternatives for what you want:
Keep the width as match_parent and center the text inside the TextView (with gravity).
Set the width to wrap_content and center the View inside its parent (with layout_gravity).
I am having a spot of bother with a fairly simple layout. Here it is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:id="#+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
3 Questions:
How do I center vertical both TextViews (or perhaps better said, the text within those views) within the parent LinearLayout? The left view is vertically centered OK, but the right one (because it has a smaller font) is not. It seems to be centered vertically at the top. I obviously tried playing with layout_gravity of the second view but that makes no difference whatsoever. The only way I can solve it is to wrap the second TextView in a LinearLayout with its layout height parameter set to match_parent (but is this the correct way of doing this?)
Similarly, I want the View on the left horizontally centered on the left, and the View on the right horizontally centered on the right. Currently the right View is placed immediatly next to the left one. The only way I can solve this is by adding something like this inbetween the two text views:
<TextView
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="">
basically acting as a spacer which decreases in size depending on the legth of the text in both TextViews
I want the text in the left View to be truncated if the combined text of the Views does not fit horizontally. No wrapping onto a new line. Currently the text in the left View simply "pushes" the right one out of the parent. No idea how to achieve that (apart from adding android:maxLines="1" to stop the text from wrapping). I have tried android:ellipsize="end" but that does not seem to have any effect.
Best way is to use Relative layout , but still if you want to do the same thing in Linear layout than do some changes in your xml file
-First is set Linear layout hight as match parent :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizonatal">
-Second for making the views visible at centre vertical do
android:layout_gravity="center_vertical"
same property android:layout_gravity = "center_horizontal" , you have to add in second text view also.
It will make your both text view appear at centre vertical but one next to other.
To make the second view appear on right I think you can add
android:layout_marginLeft="xx dp"
put some value in place of xx.
For your third question about truncating your text, you should give some size to your TextView not wrap content..Like android:layout_width ="25dp"
and then use android:ellipsize="end".
I guess you will get that..Actually I am in hurry,time to leave the office.
I'm having a bit of an issue with a text view embedded within a scroll view. When the text becomes 4 lines or so, I'm unable to scroll to the top line of the text - although I can scroll to one line past the last line of the text.
Here's the layout:
<ScrollView android:layout_width="wrap_content"
android:layout_height="90sp"
android:scrollbars="vertical">
<TextView android:id="#+id/display_english"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="10dp" android:layout_gravity="center_vertical|center_horizontal"
android:textSize="30sp" android:textColor="#color/text_color" />
</ScrollView>
Any thoughts?
If you're just trying to make your TextView scrollable, then you do not need to embed it in a ScrollView. I would check out this solution.
After extensively reviewing this problem in code and online, currently the only viable solutions to get a TextView to scroll vertically inside a ScrollView appear to be:
1) If a vertically-oriented layout isn't absolutely necessary, change the containing ScrollView to a HorizontalScrollView and arrange its children (including the scrollable TextView) in a horizontal orientation. This way, the horizontal scrolling of the HorizontalScrollView will not interfere with the vertical scrolling of the TextView.
2) If a vertically-oriented layout IS absolutely necessary, don't try to get the TextView to scroll vertically inside a vertical ScrollView, at all. Instead, set the height of the TextView to "WRAP_CONTENT", since this will ensure the content of the TextView will be visible in its entirety without scrolling. This works, because you are not concerned with the absolute height of the child-layout, since you want to use a vertical ScrollView in the first place.