Adjust TextView in Layout when TextView is empty - android

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);

Related

Different Gravity of two textviews in a horizontal Linear Layout

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">

how to display textview below button in relativeLayout

in the below xml layout, i have two buttons at the same position and they will do their function based on the visibilty set. now i tried to place two textviews
below the buttons, i want the text views to be below both buttons so I used
android:layout_below="#id/actConnect2_btn_connect"
but at run time when the connect-button is visible the text view appears below it, and if pair-button is visible it overlap
how to display the textview below both buttons?
Note: i know that i can use android:layout:marginTop but i want to solve it without it
code:
<Button
android:id="#+id/actConnect2_btn_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_tv_label_devClass"
android:layout_centerInParent="true"
android:text="#string/str_pair"/>
<Button
android:id="#+id/actConnect2_btn_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/str_connect"
android:layout_below="#+id/actConnect2_tv_label_devClass"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/actConnect2_tv_label_uuids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_btn_connect"
android:text="Service's UUID: ">
</TextView>
<TextView
android:id="#+id/actConnect2_tv_uuids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_tv_label_uuids">
</TextView>
Put both buttons in a LinearLayout then put the textview below the LinearLayout
take both button in one layout
<RelativeLayout android:id="#+id/relativeButton"
android:layout_width="wrap_content"
android:layout_below="#id/actConnect2_tv_label_devClass"
android:layout_height="wrap_content">
<Button
android:id="#+id/actConnect2_btn_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/str_pair"/>
<Button
android:id="#+id/actConnect2_btn_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/str_connect"
android:layout_alignParentStart="true" />
</RelativeLayout>
and use this for your textview
android:layout_below="#id/relativeButton"
You have to place the buttons in a Layout (any type and arrange them accordingly)
Assign an ID to that layout.
Place the textView below that layout ID.

LinearLayout used as item layout in ListView won't go to the right

I am trying to align to the right some ListView items, but they won't budge from the left hand side. The gravity and layout_gravity attributes don't appear to be working.
This code also aligns the layout to the right in the XML code, but doesn't at run time when populating the ListView.
Here is the XML I am using, which also uses a 9 patch image as the background of the LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:background="#drawable/my_9Patch_image"
android:id="#+id/contentLayout"
android:orientation="horizontal"
android:layout_gravity="right"
android:gravity="right"
>
<TextView
android:layout_width="wrap_content"
android:maxWidth="330dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:textColor="#e5e5e5"
android:minHeight="0dp"
android:singleLine="false"
android:textSize="16dp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#e5e5e5"
android:minHeight="0dp"
android:textSize="10dp"
android:layout_gravity="left|bottom"
android:gravity="left|bottom"
/>
</LinearLayout>
I also tried to set the params in my adapter's getView method using:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
mLinearLayout.setLayoutParams(params);
Thanks in advance for any help.
I would look at rowbuttonlayout.xml from http://www.vogella.com/tutorials/AndroidListView/article.html
Basically, you are trying to change the gravity of the items, but you are trying form the outside of the list view. Likely it is trying to adhere by moving the whole list view to the right, but the list view takes up all of the space so you don't even notice it
If you want individual elements to be left justified, you will want to make custom cells that are left justified that are made when you want left justified, or make your own cell, and left justify the stuff in that cell when necessary
Preliminary solution (I'll be back). This seems to be inefficient but you can add another LinearLayout to wrap the other LinearLayout. Take the containter LinearLayout and set both the height and width to match_parent, and put the gravity attribute to right. Then add the 9 patch image to the nested LinearLayout instead of having it at the parent container LinearLayout view level, and change the its height and width to wrap_content.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contentLayout"
android:orientation="horizontal"
android:gravity="right"
>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/purple_right_2"
>
<TextView
android:id="#+id/message_outgoing"
android:layout_width="wrap_content"
android:maxWidth="330dp"
android:layout_height="wrap_content"
android:textColor="#e5e5e5"
android:minHeight="0dp"
android:singleLine="false"
android:textSize="16dp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="#+id/clockView"
android:textColor="#e5e5e5"
android:minHeight="0dp"
android:textSize="10dp"
android:layout_gravity="left|bottom"
/>
</LinearLayout>
</LinearLayout>

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.

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