Different Gravity of two textviews in a horizontal Linear Layout - android

I am using the following code and when I set gravity to the first textview to center, automatically the second textview's text is also getting aligned with the first one. Even Though I set the gravity of second view to top
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="96dp"
android:text="New Text"
android:id="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="48dp"
android:text="New Text"
android:id="#+id/textView2" />
</LinearLayout>
There was a solution in another question which says to wrap the 2nd textview in another LinearLayout. But why is it so?

A horizontal LinearLayout aligns its child Views by their baselines by default, so the second TextView is being moved to align its text with the first's. To fix your problem, simply set the LinearLayout's baselineAligned attribute to false.
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false">

Related

How to align two TextView in a LinearLayout in the same line

I have two TextView in a LinearLayout, I want to align them one to the left (or center) and one to right in the same line. How to do this? I try to use gravity but they ignore it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TextView" />
</LinearLayout>
The easiest way is to change your LinearLayout to a RelativeLayout.
You can use android:layout_alignParentRight="true" and android:layout_alignParentLeft="true". Or to center it use android:layout_centerInParent="true"
See here why gravity won't work
You are using gravity instead of layout_gravity which is what you would want. This post should help clarify the difference
The docs show you available properties.
android:gravity is used to set the gravity of content inside the view. However, in your case the width is wrap_content, hence the content has nowhere to go in the text views.
Use a RelativeLayout with layout_width as match_parent. Then use the android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"with the textViews.
Use it with or without the android:gravity in the second textview and try .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="1" />
</LinearLayout>
If LinearLayout is Vertical, you can put only an object per line.
You can use RelativeLayout, or else put in a line a LinearLayout Horizontal, that contains textviews
ex.
<LinearLayout vertical>
<LinearLayout horizontal>
<textview 1></>
<textview 2></>
</LinearLayout>
</LinearLayout>
I fixed all my issues with GridLayout...is the best thing bcos u don't need to align anithing to nothing...just put what u want into the matrix (row,column)...and this will allow you to visualize all the field in exactly wrap content of your datas also in landscape is perfect!

TextView not properly in the center

i want to place a textview in the center of a linearlayout but i am having problems with it. my linearlayout is match_parent in layout_width. textview is centered by assigning center to layout_gravity. textview is wrap_content in layout_width. here is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=" Text"
android:textSize="40sp"
android:textStyle="bold"
android:typeface="monospace" />
</LinearLayout>
</LinearLayout>
Two things:
1) set
android:gravity="center"
android:layout_gravity="center"
in your textview;
2) The second linearlayout (inside) is useless. Just remove it. What's more, if you only have one view, consider using FrameLayout instead of LinearLayout, then use <merge> tag to optimize the performance (see details here)
if you want to center the text inside your TextView you have to use android:gravity="center"
Change
android:layout_gravity="center" to
android:gravity="center"
Set these properties in the second Linearlayout.
android:gravity="center"
android:layout_gravity="center"
to make your textview center set
android:gravity="center"
android:layout_gravity="center"
of your textview.
see the difference here:-
android:gravity :-- sets the gravity of the content of the View its used on.
android:layout_gravity:-- sets the gravity of the View or Layout in its parent.

Adjust TextView in Layout when TextView is empty

I have a basic ListView with two TextViews in the cells and I would like for the TextViews to center to the middle of the Layout when there isn't any text in the other TextView.
Here is my current XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:minHeight="80dp"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<TextView android:id="#+id/titleTextView"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:textSize="16sp"/>
<TextView android:id="#+id/detailsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_below="#id/titleTextView"
android:textSize="12sp"/>
</RelativeLayout>
I'm using a RelativeLayout because I am hoping to utilize it when I add more elements, but I want to figure this part out first. Switching to a LinearLayout didn't have any effect on adjusting the TextViews.
Will I have to adjust the views programmatically? If so, how should I handle that? Remove the one view from the layout (Which will require a LinearLayout, right)?
Simply make the TextView "gone" when it is empty, when a view is "gone" it has no effect on the layout it is in, thus the other TextView would center because of the gravity property you have added.
textView.setVisibility(View.GONE);

LinearLayout: how to place a view below/right of a fill_parent view?

I want to have a TextView on the left that fills space, plus a right-justified TextView on the right. I've tried fiddling with gravity, layout_gravity, and making the second TextView fill_parent also, but the second TextView always ends up invisible as long as the first one is fill_parent. I notice that the same problem occurs if the LinearLayout is vertical. What to do?
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Left Side"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right side"
android:background="#ff606060"/>
</LinearLayout>
Target: Android 2.2
I think this is what you want.
You can also add singleLine (and ellipsize) to the left view if you don't want it to wrap text.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Left Side"
android:layout_weight="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right side"
/>
</LinearLayout>
use layout_weight and set layout_width to 0dp on both your TextView.
So weight set to 3 on the first TextView, then 1 on the second for example/
Why not use a RelativeLayout?
Set the right-justified TextView to layout_alignParentRight, then set the other one that fills the space to layout_alignParentLeft and layout_toLeftOf the right-justified TextView

How to layout view right aligned and bottom of an LinearLayout

I am trying to layout 1 textview (upText) left aligned and 1 textview (downText) and an image view (image) both on the same line and right aligned.
how can I do that? I tried that, but both 'textview' and image view at left aligned.
<TextView
android:id="#+id/uptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"/>
<TextView
android:id="#+id/downtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="right|bottom"/>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|bottom"/>
</LinearLayout>
Don't use a LinearLayout. Use a RelativeLayout, with
your first TextView set with android:layout_alignParentLeft="true"
your second TextView set with android:layout_alignParentBottom="true" and android:layout_alignParentRight="true"
something similar for your ImageView, which presently looks like it is going to overlap the second TextView
I realize this post is a bit old but just in case someone comes across this in their search for clarity;
The parent linear layout is where gravity needs to be specified for the child to align with the desired behavior which is why the above posts are explaining that linear layout is not possible for two separate behaviors to occur since a child cannot decide where to align itself within a linear layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="bottom|right">
<TextView
android:layout_width="40dp"
android:layout_height="20dp"
android:text="test"/></LinearLayout>
It should also be said that the parent linear layout must have a defined size and not be wrap-content or this will not work since wrap content implies that there will be no extra space in the layout for positioning, so at least 'match-parent' for width and height is necessary as well as having a parent with a greater size than wrap-content for the child linear layout itself.
Hope this helps.
Using RelativeLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hi"
android:textStyle="bold|italic"
android:gravity="right"/>
<TextView
android:id="#+id/text2"
android:layout_below="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="All"
android:gravity="right"/>
</RelativeLayout>

Categories

Resources