I have 2 TextView one on top of the other. When they have the same text they have the same height but when one has more text from the other he will get bigger.
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingLeft="8dp"
android:paddingTop="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Math, Civics"
android:id="#+id/teaches"
android:layout_weight="1"
android:background="#A4D34A" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Computer Science"
android:id="#+id/learns"
android:layout_weight="1"
android:background="#D22ACE" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="5$/hour"
android:id="#+id/rate" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_marginRight="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Start Chat"
android:id="#+id/textView9"
android:layout_marginRight="8dp"
android:textStyle="italic"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
How can I make them stay in the same height no matter what?
You have a couple of options: You can provide equal weights to the two textviews
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
OR You can use give fixed heights to both views.
OR You can set MaxLines(Maximmum lines) attribute for the text view to be of a particular value.
Define a fixed value for yout layout_height like 24dp or something best in an dimens.xml like that:
<dimen name="textview_height">24dp</dimen>
and in your layout put both layout_height to :
#dimen/textview_height
Another option would be to set the first to set both to wrap_content and get the MeasuredHeight from both of them, then take the greater value and apply it to both TextViews
Related
I have the following LinearLayout below. I use it for a row in a ListView.
I have set the Textview's weight to 1 so they should all be of equal width.
The problem is when some of the textviews have more characters in, the width of the textview changes. This has an overall effect on how the ListView appears, as in each row the data in not lined up correctly. (i have headings above the ListView).
when some of the TextViews have more text in them, there is still space either side of the text and the textview just gets bigger, which i don't want as i've set the weights to 1 each.
Is there a way of fixing the width of each textview so it doesn't change according to the length of text in it?
thanks in advance
Matt
<?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:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/rowstarttimeweeksum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<TextView
android:id="#+id/rowplannedcallsweeksum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1" />
<TextView
android:id="#+id/rowncrsweeksum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1" />
<TextView
android:id="#+id/rowqasweeksum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<TextView
android:id="#+id/rowplannedhoursweeksum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2:33"
android:layout_weight="1"
android:gravity="right"/>
<TextView
android:id="#+id/rowactualcallsweeksum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"
android:gravity="right"/>
<TextView
android:id="#+id/rowactualhourssweeksum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2:01"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
One small change and your problem is solved
change width of all TextView from wrap_content to 0dp
android:layout_width="0dp"
Whenever you will set height/width to 0dp, it will manage height/width of view as per android:layout_weight
I have a problem with "TextView" I do not know how to solve. I have three "TextView" within a "LinearLayout" and that the texts would stay the same size, same as break line. At the moment, the texts are on the same line, but with different sizes. What to do? The following image and code:
<LinearLayout
android:id="#+id/complementaryInfoLayout"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#color/purewhite">
<TextView
android:id="#+id/textCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#545454"
android:layout_marginStart="15dp"
android:text="#string/placeholder" />
<TextView
android:id="#+id/textCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#545454"
android:text="#string/placeholder" />
<TextView
android:id="#+id/textNeighborhood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#545454"
android:text="#string/placeholder" />
</LinearLayout>
How is the tablet
Well.. The last line seems to be breaking duo the string size.
You may obligate TextView occupy one line regardless its content by using:
android:singleLine="true"
Furthermore you can give a nice touch by using:
android:ellipsize="marquee"
So it wont crop abruptly the content in it.
Hope it helps...
Try giving weights to your TextViews :
<LinearLayout
android:id="#+id/complementaryInfoLayout"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#color/purewhite">
<TextView
android:id="#+id/textCategory"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#545454"
android:layout_marginStart="15dp"
android:text="#string/placeholder" />
<TextView
android:id="#+id/textCity"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#545454"
android:text="#string/placeholder" />
<TextView
android:id="#+id/textNeighborhood"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#545454"
android:text="#string/placeholder" />
</LinearLayout>
need some clarity on your query, i hope you want to adjust three childs in linear layout horizontally, for this
you can use android:layout_weight attribute, in below code i have used layout_weight as 2, 3 and 5 respectiviely,
here 2+3+5=10 i.e total 10 parts equal to 100% width
so first textview occupies (2/10)*100 = 20% width horiontally
so second textview occupies (3/10)*100 = 30% width horiontally
so third textview occupies (5/10)*100 = 50% width horiontally
<LinearLayout
android:id="#+id/complementaryInfoLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#color/purewhite">
<TextView
android:id="#+id/textCategory"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textCity"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textNeighborhood"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"/>
</LinearLayout>
I am new to android, I have a linear layout list item and would like the TextView itemName to fill the remaining blank space on the row while the other two views (itemIcon and itemTimestamp) just take up as much space as they require hence used wrap_content.
I have tried fill_parent and match_parent on ItemName android:layout_width but it ends up hiding itemTimestamp view. I have tried RelativeLayout but had no luck, would prefer to stick with Linearlayout if possible.
Appreciate your help.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/itemNotificationBuzz"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff0000"
>
<LinearLayout
android:id="#+id/notificationHeader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#ffff0000"
>
<ImageView
android:id="#+id/notificationIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:maxHeight="35dp"
android:maxWidth="35dp"
android:background="#FF000000"
android:padding="1dp"
android:src="#drawable/ic_action_place" />
<TextView
android:id="#+id/notificationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:textSize="18sp"
android:textColor="#android:color/white"
android:background="#A4C739"
android:typeface="sans"
android:textStyle="bold"
android:maxLines="1"
android:text="Fname Lname"
/>
<TextView
android:id="#+id/notificationTimestamp"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:background="#FF000000"
android:ems="10"
android:maxLines="1"
android:text="2m"
android:textColor="#android:color/white"
android:typeface="sans" />
</LinearLayout>
</RelativeLayout>
If you want to use the LinearLayout you can achieve what you want by using weightSum attribute in your LinearLayout and layout_weight for your item Name TextView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/notificationHeader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff0000"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:id="#+id/notificationIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF000000"
android:gravity="left"
android:maxHeight="35dp"
android:maxWidth="35dp"
android:padding="1dp"
android:src="#drawable/ic_launcher"/>
<TextView
android:id="#+id/notificationName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#A4C739"
android:gravity="center"
android:maxLines="1"
android:padding="5dp"
android:text="Fname Lname"
android:textColor="#android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"/>
<TextView
android:id="#+id/notificationTimestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF000000"
android:gravity="right"
android:maxLines="1"
android:text="2m"
android:textColor="#android:color/white"
android:typeface="sans"/>
</LinearLayout>
This will make your item Name to fill all the space until it reaches the last element.
Note: When you use layout_weight you have to set the layout_width to 0dp. And also you should delete ems = 10 from your last element because it makes your notificationTimestamp element to have a larger width and it will push the Name element to the left leaving an empty space.
You can directly use RelativeLayout to achieve what you want. The answer is already here at How to make layout with View fill the remaining space?
If you want to still use the LinearLayout then that is also possible. In this case to achieve what you want, set the layout_weight property of the widgets that you want to just wrap content with to 0 (you should still set them to wrap_content) and then set the layout_weight of the widget that you want to occupy the remaining space to 1.
There is no reason to use RelativeLayout here. What you need to use is layout-weight. You need to set layout_width to 0dp and layout-weight to 1
Here is the updated layout (btw, I removed the parent RelativeLayout as it didn't have any use):
<ImageView
android:id="#+id/notificationIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:maxHeight="35dp"
android:maxWidth="35dp"
android:background="#FF000000"
android:padding="1dp"
android:src="#drawable/ic_action_place" />
<TextView
android:id="#+id/notificationName"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:textSize="18sp"
android:textColor="#android:color/white"
android:background="#A4C739"
android:typeface="sans"
android:textStyle="bold"
android:maxLines="1"
android:text="Fname Lname"
/>
<TextView
android:id="#+id/notificationTimestamp"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:background="#FF000000"
android:ems="10"
android:maxLines="1"
android:text="2m"
android:textColor="#android:color/white"
android:typeface="sans" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/lbd_user_pt_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/lbd_user_pt"
android:paddingRight="10dp"
android:text="#string/leader_board_my_pt"/>
<TextView
android:id="#+id/lbd_user_pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:text="90000"
android:textDirection="ltr" />
</RelativeLayout>
Currently I have a relative layout that is the horizonal line. And there is two textview inside . Since the length of the relative layout is too small, the 2 textview overlay or behave werid
like:
[user_pt_tag user_pt]
The problem is , I would like the user pt align right and expand to left , also , the common textsize is 20sp , if the pt overlap the tag , the size will be reduce. (the pt textview is at most 5 digits , that will no more digit than that) How to achieve that? Thanks
//try to use Linear Layout rather than Relative Layout like below
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="#+id/lbd_user_pt_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="#string/leader_board_my_pt"/>
<TextView
android:id="#+id/lbd_user_pt"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="right"
android:text="90000"
android:textDirection="ltr" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/lbd_user_pt_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:maxLength="5"//you can give the length as per your need
android:layout_toLeftOf="#+id/lbd_user_pt"
android:paddingRight="10dp"
android:text="leader_board_my_pt"/>
<TextView
android:id="#+id/lbd_user_pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:text="90000"
android:textDirection="ltr" />
</RelativeLayout>
I'm developing what I thought would be a simple listview row layout, but I'm having some trouble getting my views to line up properly. There are 4 TextViews all using layout_weight within a LinearLayout to size themselves appropriately. What I can't figure out, however, is how to align them consistently so that each TextView begins in the same position as the one above and beneath it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AAAAAA88">
<TextView
android:id="#+id/quality"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:layout_gravity="center_vertical|right"
android:textColor="#ffffff"
android:padding="5px"
/>
<TextView
android:id="#+id/route_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="3"
android:layout_gravity="center_vertical|right"
android:textColor="#ffffff"
android:padding="5px"
/>
<TextView
android:id="#+id/route_grade"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical|right"
android:textColor="#ffffff"
android:padding="5px"
/>
<TextView
android:id="#+id/route_distance"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical|right"
android:textColor="#ffffff"
android:padding="2px"
/>
</LinearLayout>
My attempts to use the :gravity tag don't seem to work. Is LinearLayout the wrong approach here?
As an edit, I'm making some headway using specific width values in dips. Now to test if it looks the same across different screens.
The gravity distributes the remainder of space available after minimum constraints not the total space.
So align different row above and below make sure the remaining space is equal on each row. A good way to accomplish this is to hard-code the minimum space each element needs:
android:layout_weight="2"
android:layout_width="1dip"
Make each TextView on every row 1dip width and split the remaining space according to weight.
you can try using RelativeLayout. Try 4 of the TextViews to be placed relatively, 3 can have fixed width in dp and the 4th can take up rest of the available space. that way each textview will start at a fixed position every time.
something like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/text_1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:gravity="left"
android:padding="8dp"
android:text="Text 1"/>
<TextView
android:id="#+id/text_2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/text_1"
android:layout_toLeftOf="#+id/text_1"
android:gravity="left"
android:padding="8dp"
android:text="Text 2"/>
<TextView
android:id="#+id/text_3"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/text_1"
android:layout_toLeftOf="#+id/text_2"
android:gravity="left"
android:padding="8dp"
android:text="Text 3"/>
<TextView
android:id="#+id/text_4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/text_1"
android:layout_toLeftOf="#+id/text_3"
android:layout_alignParentLeft="true"
android:gravity="left"
android:padding="8dp"
android:text="Text 4"/>
</RelativeLayout>