so I'm currently working on an app on Android, and I got stuck on a specific problem regarding the RelativeLayout, which I can't find a way to solve.
I have in the layout three views as follows: TextView, Textview and ImageView (laid horizontally), here is a screenshot of the ios counterpart:
the Textview at the middle should stick to the first one, until he gets to the Imageview, when he does, he keeps his minimum size (wrap content), while the first Textview truncate.
On IOS I setted priorities to the constraint to accomplish this, but I can't figure out how to solve this on Android.
Here what I tried:
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/daily_movie_title_box">
<TextView
android:id="#+id/daily_header_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="New Text aawi oa ioawfwi"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#color/white"
android:lines="1"
android:singleLine="true"
android:layout_alignParentStart="true"
/>
<TextView
android:id="#+id/duration_text"
android:text="138 mins"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#color/white"
android:lines="1"
android:layout_alignBaseline="#id/daily_header_textview"
android:layout_toStartOf="#+id/certification_icon"
android:layout_toEndOf="#id/daily_header_textview"
/>
<ImageView
android:id="#id/certification_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:src="#drawable/uk12a"
android:layout_alignBottom="#id/daily_header_textview"
app:layout_aspectRatio="100%"/>
</android.support.percent.PercentRelativeLayout>
Which resulted in this (which is what I want):
But when I increase the first Textview text it's not behaving as I desire...
Is it possible to achieve the behaviour I want in Android (keep the middle Textview wrap content, and truncate the first one if needed)?
I will post an update if I find a solution eventually, just wanted to see if anyone can find an easy way to achieve this behaviour, as I suspect there is.
Thanks.
From my understanding, you want the first TextView to be as large as possible, without adding space after the text if the text is too small. The second TextView should only wrap_content, but it should fill the rest of the parent layout when the row doesn't. The ImageView is set to wrap_content.
I tested it with this layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Shrinking text dddddddddddddddddddddd"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Midle column"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"/>
</TableRow>
</TableLayout>
</LinearLayout>
The only problem is that if the second column has a incredibly large text, it will push the other views out of the parent. But in your case, I don't think that will be a problem. Otherwise, I think it does the job.
These are some suggested solutions:
You can use LinearLayout with horizontal orientation and weight for each component (TextViews and ImageView).
You can set the minimum and maximum text length for the second TextView.
But i prefer to apply the first solution. You can assign a weight for each component ( amount of space on the screen ) using:
android:layout_height
I have tried this. I planned to do like this:
But it showing in this way:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvHiragana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hiragana"
android:layout_weight="2"
android:textSize="22sp" />
<TextView
android:id="#+id/tvKanji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kanji"
android:textColor="#color/redColor"
android:layout_weight="1"
android:textSize="20sp" />
</LinearLayout>
What should I do make it work correct in the terms of layout_weight? How to avoid making new lines in TextView?
What should I do make it work correct in the terms of layout_weight?
First, when using weight with a horizontal layout, your width should be 0dp. Similarly, if your layout is vertical then your height should be 0dp.
How to avoid making new lines in TextView?
use android:singleLine="true" if you want to
Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines
TextView Docs
If this doesn't answer your question then please be a little more clear because its a bit confusing on what you're asking.
I am wondering how to have a TextView display its content on several lines without hardcoding the width in the XML.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Any thought welcome.
EDIT: my problem is that when the text exceeds the width set (because it reaches the end of the screen) a portion of the text is just not displayed. I would expect the text to be split on two lines
Though I cannot reproduce the not wrapping problem, you can fix the positioning problem by using a weight on the first TextView. Using the following XML gives the expected output in the graphical layout view in Eclipse:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Also add
android:minLines="2"
android:scrollHorizontally="false"
You could try
android:inputType="textMultiLine"
in your TextView XML. This worked for me.
I think I had very similar problem. I had a TextView with a text, where I was not sure how much lines will it take. It was encapsulated by a LinearLayout having android:layout_width="match_parent" to ensure my text will fill out all the space horizontally. However, the problem was that my text did not fit into 1 line and when it did break into a new line, the next view component below it did not move downwards to give enough space for the second line to be viewable fully.
I could achieve the solution by changing the LinearLayout that was containing my TextView into a RelativeLayout. By this way, the element below the text (actually below the Layout itself) was moved automatically to give enough space for the multi-line text.
I have two buttons which I would like to appear side-by-side horizontally, but together they fill the horizontal length of the phone. The height is wrap content, that's fine. My issue right now is that only one button is showing up (stretching across the screen).
Here is my XML code:
<LinearLayout
android:id="#+id/page_buttons"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<Button
android:id="#+id/prevButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Previous"
/>
<Button
android:id="#+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
/>
Change your Buttons XML to include the layout_weight attribute:
<Button android:id="#+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next"/>
Since nobody is explaining why their solutions work, I will give it a go.
The problem lies in the layout of the buttons. The two concerned attributes are layout_width and layout_weight.
In other layout systems, when you indicate that each element of a layout has to fill the parent (layout_width="fill_parent"), they do so by distributing equally the space of the parent between them. So, each of them would have the same size.
Instead, in Android's layout system, if both elements have
layout_width="fill_parent" the first element (in your case, the
Previews button) will be stretched to fill the parent and the second
(or third, or etc.) will not have any space left to distribute, so it
will not be visible.
Now, to make it understand that you want both buttons to show, you set the layout_weight for each button. To make the buttons have the same size, set the same layout weight to both of them.
The layout_weight defines how many "parts" (or segments) of the parent each of the buttons occupy. The parent will be cut into a number of segments equal to the sum of the children's segments. If you want to make one button three times bigger then the other, you have to assign it the number of parts equal to the number of parts of the first button, multiplied by three.
So if you want your Next button to be two times bigger then the
Previews button, you can do this:
for Previews button: layout_weight=1
for Next button: layout_weight=2
In consequence, the parent will be cut in 3 parts, 2 of which will be allocated to the Next button and 1 to the Previews button.
The example taken here is for buttons and horizontal layout, but this will work just fine for any type of object and also for vertical layout parent.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/entry"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/space"/>
<TextView
android:id="#+id/space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/button02"
android:layout_toRightOf="#id/button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/entry"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Seems like you want two equal buttons, not wrapped content. I created a centered spacer using TextView, and relatively aligned to that. Left button to parent left and spacer, Right button to Left Button and parent right.
Parent of your Button is Linear-Layout, and you are setting Button's width as fill parent and hence it is taking the whole space. You hav two options to Implement this...
Give the fixed width for your button(eg 50dip).
Give the width as 0dp and insert one more attribute in both button "weight" and give 0.5dip for each...
The two buttons should be on a linearlayout. The linear layout should be horizontal and each button has a weight of 1. This should give you two buttons with equal size along the screen width. A sample is here
Horizontal orientation: check the 2 Buttons at the end of this xml layout
Your Requirement is
<LinearLayout
android:id="#+id/page_buttons"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/prevButton"
android:layout_width="0dp"
android:weight="0.5"
android:layout_height="wrap_content"
android:text="Previous"
/>
<Button
android:id="#+id/nextButton"
android:layout_width="0dp"
android:weight="0.5"
android:layout_height="wrap_content"
android:text="Next"
/>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<TextView
android:text="#+id/SomeText"
android:id="#+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:background="#android:drawable/bottom_bar"
android:paddingLeft="4.0dip"
android:paddingTop="5.0dip"
android:paddingRight="4.0dip"
android:paddingBottom="1.0dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="#+id/TextView01">
<Button
android:id="#+id/allow"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:text="Allow"
android:layout_weight="1.0" />
<Button
android:id="#+id/deny"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:text="Deny"
android:layout_weight="1.0" />
</LinearLayout>
</RelativeLayout>
I found the leading issue for people who still seems to not get what they want.
when using buttons with their conventional (out-of-the-box/default) background, it has a built in margin.
If you try changing the background manually with a single color, you can tell easily that you just need to set a separate background.
other than that when you have the weights put in, it will work.
Can anyone tell me what's going wrong with the text? Text longer than one line doesn't wrap to the next line but goes beyond the screen.
Following is the code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<TextView
android:id="#+id/reviewItemEntityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="event/venue"
android:textColor="#color/maroon"
android:singleLine="true"
android:ellipsize="end"
android:textSize="14sp"
android:textStyle="bold"
android:layout_weight="1" />
<ImageView
android:id="#+id/reviewItemStarRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:src="#drawable/title_1_star" />
</LinearLayout>
<TextView
android:id="#+id/reviewItemDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Description comes here"
android:textSize="12sp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
I fixed it myself, the key is android:width="0dip"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<TextView
android:id="#+id/reviewItemEntityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/maroon"
android:singleLine="true"
android:ellipsize="end"
android:textSize="14sp"
android:textStyle="bold"
android:layout_weight="1" />
<ImageView
android:id="#+id/reviewItemStarRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
</LinearLayout>
<TextView
android:id="#+id/reviewItemDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:width="0dip" />
</LinearLayout>
<ImageView
android:id="#+id/widget01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow_nxt"
android:layout_gravity="center_vertical"
android:paddingRight="5dip" />
</LinearLayout>
The only correct answer to this question is that you need to set the parents to a proper width (in this case FILL_PARENT, WRAP_CONTENT) and use android:layout_weight=1 for the textview that needs to be wrapped.
SingleLine is on by default so that won't make any changes.
A width set to 0px will work but is not a good solution.
Some example (in a tableview this time), using xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="*"
android:id="#+id/tableLayout1">
<TableRow android:id="#+id/tableRow1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="test1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="0" />
<TextView android:layout_weight="1"
android:text="test2 very long text that needs to be wrapped properly using layout_weight property and ignoring singleline since that is set by default..."
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</LinearLayout>
If you want to set this in code you're looking for the layout_weight as a third parameter as in this example where it is set to 1:
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label = new TextView(getApplicationContext());
label.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1f));
You must use 2 parameters :
android:ellipsize="none" : the text is not cut on textview width
android:scrollHorizontally="false" the text wraps on as many lines as necessary
It is enough to use in your xml file.
android:singleLine="false".
Hope it will work.
All the best!
I could not get any of these solutions working when using a TableLayout>TableRow>TextView. I then found TableLayout.shrinkColumns="*". The only other solution that worked was forcing the TextView to layout_width:250px etc but i don't like forcing widths like that.
Try something like this if working with tables.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*">
Note you need shrinkColumns="*"
This is obviously within a <LinearLayout>. So something like <LinearLayout> <TableLayout> <TableRow> <TextView>
references:
TableLayout
http://code.google.com/p/android/issues/detail?id=4000
Hope that helps someone.
One of your layout parameters is wrong in your code. In the first TextView
android:layout_width="wrap_content"
change to
android:layout_width="fill_parent"
The text that out of screen width size will wrap to next line and set android:singleline="false".
Set the height of the text view android:minHeight="some pixes" or android:width="some pixels". It will solve the problem.
For my case removing input type did the trick, i was using android:inputType="textPostalAddress" due to that my textview was sticked to one line and was not wrapping, removing this fixed the issue.
I'm an Android (and GUI) beginner, but have lots of experience with software. I've gone through a few of the tutorials, and this is my understanding:
layout_width and layout_height are attributes of the TextView. They are instructions for how the TextView should shape itself, they aren't referring to how to handle the content within the TextView.
If you use "fill_parent", you are saying that the TextView should shape itself relative to it's parent view, it should fill it.
If you use "wrap_content", you are saying that you should ignore the parent view, and let the contents of the TextView define it's shape.
I think this is the confusing point. "wrap_content" isn't telling the TextView how to manage it's contents (to wrap the lines), it's telling it that it should shape itself relative to it's contents. In this case, with no new line characters, it shapes itself so that all the text is on a single line (which unfortunately is overflowing the parent).
I think you want it to fill the parent horizontally, and to wrap it's contents vertically.
Even-though this is an old thread, i'd like to share my experience as it helped me. My application was working fine for OS 2.0 & 4.0+ but for a HTC phone running OS 3.x the text was not wrapping. What worked for me was to include both of these tags.
android:maxLines="100"
android:scrollHorizontally="false"
If you eliminate either it was not working for only the os 3.0 device. "ellipsize" parameter had neutral effect. Here is the full textview tag below
<TextView
android:id="#+id/cell_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:maxLines="100"
android:scrollHorizontally="false"
android:textStyle="normal"
android:textSize="11sp"
android:textColor="#color/listcell_detail"/>
Hope this would help someone.
Increase the height i.e.. android:height="Some size" , it is working fine for me.
I had a similar problem where were my two horizontally weighted TextViews didn't wrap the text. I later found out that the issue was because my viewparents parent had wrap_content instead of match_parent.
I think it depends on the particular combination of layouts in your display. Some flags may get overridden or ignored. I have a TabHost with tabs, each tab is a list of tables. So it is a tab of ListView, each row being a TableLayout of TextView. I tried the fixes listed above and none of them worked.
I know, that in question it is correct, but in my case, the problem was in setting textSize property in 'dp' - I've changed it to 'sp' and it works fine.
In my case, with a TableRow > ScrollView > TextView nesting, I solved the problem by setting android:layout_width to fill_parent on TableRow, and to wrap_content on ScrollView and TextView.
you have to use android:singleLine="false" in ur TextView tags.
I finally managed to add some pixels to the height of the TextView to solve this issue.
First you need to actually get the height of the TextView. It's not straightforward because it's 0 before it's already painted.
Add this code to onCreate:
mReceiveInfoTextView = (TextView) findViewById(R.id.receive_info_txt);
if (mReceiveInfoTextView != null) {
final ViewTreeObserver observer = mReceiveInfoTextView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int height = mReceiveInfoTextView.getHeight();
int addHeight = getResources().getDimensionPixelSize(R.dimen.view_add_height);
mReceiveInfoTextView.setHeight(height + addHeight);
// Remove the listener if possible
ViewTreeObserver viewTreeObserver = mReceiveInfoTextView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
}
}
});
}
You need to add this line to dimens.xml
<dimen name="view_add_height">10dp</dimen>
Hope it helps.
I just removed android:lines="1" and added android:maxLines="2", this got the text to wrap automatically. The problem was the android:lines attribute. That causes the text wrapping to not happen.
I didnt have to use maxEms or singleLine="false" (deprecated API) to fix this.
I've spent hours to figure out that in the text I was trying to display contained a single quote (in string.xml) so I just escaped it with a backslash and it worked nicely => the height of the TextView was correctly wrapping text:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-smallcaps"
android:text="#string/instructions"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="#+id/keyword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/defaultKeyword"
android:textSize="22sp"
/>
I'm using constraint layout mostly.
1) android:layout_width="match_parent" = tries to stretch to meet edges
2) android:layout_width="wrap_content" = based solely on the input text without regard for other views nearby. for example adding android:textAlignment="center" will change the shape of the text
3) android:padding="12dp"
android:layout_width="0dp"
android:layout_weight="1"
android:singleLine="false"
= The text will fold to accommodate nearby layouts without regard to the text itself
In my case, My text view has a background, so applying width as 0dp does not work for me, because even for small texts the background will take the whole available space. Managed to solve it by adding
app:layout_constrainedWidth="true"
no need to set the width as 0dp, wrap content is working fine, hope this helps someone with same issue
inside your TextView write this:
android:inputType="textMultiLine"
problem solved
I put this attribute:
android:inputType="textMultiLine"
into my TextView and it has wrapping line and user can "Enter" for a new line.
============================================================
When you come to this post, you may want to create a Big TextView which can display multiple lines so these attributes may also needed
android:layout_height="Xdp" //where X is a number depends on how big Textview you want
android:gravity="top" //in order to make your text start from the top of your TextView.
I used android:ems="23" to solve my problem. Just replace 23 with the best value in your case.
<TextView
android:id="#+id/msg"
android:ems="23"
android:text="ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab "
android:textColor="#color/white"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I added a \n in the middle of my string and it looked okay.
To wrap the text and to put the text in next line we sholud use the "\n" i.e new line character in the layout xml file and check tht change on the emulator not on the layout screen.