well basicly I'm getting data from a database and I want to adapt this data in a kind of "DataGridView" on any other lenguaje, Basicly I have an GridView in a Main layout define like this:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gastoGridView"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="1"
android:clipChildren="true"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip" />
other hand I have a second xml layout file than define every item(row) for this gridView, It is my item_gridview xml file:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/TableLayout01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableRow android:id="#+id/TableRow01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<TextView android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fechaAtt"
android:gravity="left"
android:layout_gravity="left" />
<TextView android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/descpAtt"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" />
<TextView android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/saldoAtt"
android:layout_gravity="right" />
</TableRow>
</TableLayout>
Ok, basicly I'm getting 3 attributes from database, I already developed the adapter to this gridview, so I show the information on it, but not in the way I want, I'll try to explain you, Currently it's being show like this
| Date | Description | Price |
2014-11-17 any description it have 50.85$
I'm trying to divide my tablerow (width:fill_parent) on 3 sections(columns) I'm not sure if It's possible, because I'm not very involved on this subject, but I want to divide this tablerow on those 3 section, I want a small section on the left side of the row which will be my date, a large section in the center_horizontal wich will be my Description, and another left section wich will be my price, I'm not sure if you guys get my point but I want some like this.
| Date | Description | Price |
2014-11-17 Get the description Centered Here 50.85$
I've tried to use the layout_span and layout_column on every TextView, but I get a Null Pointer error which I don't understand, maybe I'm doing that in a wrong way.
Could you guys help me to get this style? I've been reading about it a lot, It's a kind of difficult because Android do not support an DataGridView tool as others lenguages do.
Thanks you beforehand, I really need it
Your difficulty stems from the fact that your trying to bring your concept of the DataGridView into Android which is problematic. What you really want to do use a ListView with a proper Adapter and Loader (use a Loader if possible).
Now, with a ListView what happens is it creates View for every row returned from the Cursor using the Adapter to create (inflate) this view and populate it (binding). This is useful since you can now think about each row as a set of three items and lay them out appropriately. I recommend just using the regular LinearLayout with the appropriate layout_weight set for your layout. You'll have to remember to set the LinearLayout to horizontal.
Edit:
For clarification. With LinearLayout you can specify in the layout.xml file the android:layout_weight parameter. This allows you to set 'relative' sizes (width or height depending on horizontal or vertical LinearLayout). Once you do this the android:layout_width is ignored but you should set it to 0dp. An Example:
<LinearLayout
android:id="#+id/row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
Now you have three TextView in a horizontal LinearLayout each with a weight set to 1. That would make them all equal size. You can adjust the weight values to change their relative sizes to each other and the parent width.
I am using GridLayout from the support Library. Some of the cells contain player information - I use a Linear Layout with two TextViews. I have not been able to get the name (the second text view) to wrap.
Current behavior:
123
John Cochtousczyon
Desired behavior:
123
John
Cochtousczyon
Here's the current state of the cell layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/player_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="test" />
<TextView
android:id="#+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:minLines="2"
android:scrollHorizontally="false"
android:text="testing thisout with extra sauce"
android:textSize="18sp" />
</LinearLayout>
and the code that adds him to the grid:
lp = (GridLayout.LayoutParams) playerCell.getLayoutParams();
lp.columnSpec = GridLayout.spec(nameCol);
lp.rowSpec = GridLayout.spec(nameRow);
playerGrid.addView(playerCell, lp);
Ideally I would like a solution that sets the column width for the widest cell AFTER wrapping the name. I've gotten close to the desired result using maxEms, but that requires balancing the most likely needed limit against how much truncation to tolerate with longer names.
P'r'aps there's nothing for it but to fix the width - just thought I'd ask in case all the smarter people than me out there have solved this.
Here's what I've settled on for now, until something better comes along:
<TextView
android:id="#+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="4"
android:maxLines="2"
android:text="testing thisout with extra sauce"
android:textSize="18sp" />
combination of fill_parent, ellipsize END, maxEms and maxLines gets me close enough.
If anyone is still looking this in 2020 Android now has a inbuilt way of doing this. Just add this code to your java file
TextviewCompat.setAutoSizeTypeWithDefaults(<your textview refernce here>, TextviewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
Also make sure you add this to your textview xml
android:ellipsize="none"
Hopefully this will help someone;
I simply used the good old "\n" to wrap.
I have a textbox with a hint but I want the hint to always be shown, even when there is an input in the TB. Example is the "To" field in the Gmail app.
There are 3 approaches you could use (spoiler - use number 3), since as mentioned in my comment, in the Gmail example , it is not an actual hint:
Using a Linear Layout, getting a cleaner look in my opinion:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="#string/Hint"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:layout_height="wrap_content"/>
</LinearLayout>
Using a Relative Layout, getting a result that mimics the Gmail App:
Note: might be problematic since the text will be displayed on top of the hint, see solution 3.
<RelativeLayout android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="#string/Hint"
android:paddingLeft="10dp"
android:layout_marginBottom="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:layout_height="wrap_content"/>
Result are as shown in this image:
Edit:
using a Drawable:
This seems a better solution (I personally just created it from snipping the 1:1 display of the TextView, will be in correct measurements this way), this will allow a cleaner layout code, and the text will be aware of the drawable:
<RelativeLayout android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:gravity="top"
android:drawableLeft="#drawable/Hint"
android:layout_height="wrap_content"/>
The first thing that came to mind is to insert two editText layout relative layout.
First over second.at the top of that - put background as
android.R.color.transparent
Work with top elevent of relative layout! In item below - set hint(that you want) and try to put text. May be you will have specify padding of first element of top to greate display.
Oh. And one more thing.
Maybe put to editText background background image - but is the bad to scalability.
I have a rather simple ListView row:
<?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:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="25dp" />
</RelativeLayout>
When the text of "#+id/tournament_name"is long - it overlaps with the one from "#+id/tournament_winner" and I don't understand why.
I tried using android:singleLine="false"to no avail. I also tried using android:inputType="textMultiLine"as the docu says android:singleLine="false" is deprecated but then I get the warning: Attribute android:inputType should not be used with <TextView>: Change element type to
<EditText> ? so no good here as well.
I also tried using android:ellipsize="end" but this doesn't work. I assume it is because the text in the left TextView ("#+id/tournament_name") is NOT long enough to fill up the full width of the ListView (which code is not sowing here).
I was sure that if I use android:layout_width="wrap_content"the two TextView fields shouldn't overlap.
Here is an example (see the second line):
Any further ideas how this could be fixed?
android:layout_toLeftOf="#id/tournament_winner" in First TextView.
Also set android:maxLines="1" and Fix width for tournament winner because when it gets long tournament name cant see...
row.xml
<?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:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/tournament_winner"
android:maxLines="1"
android:text="NAMEEEEEEEEEEEEEEEEEEEEEEEEEEE"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content" android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="WINER"
android:textSize="25dp" />
</RelativeLayout>
Thank you very much for your answer - sorry it took me some time to respond. I ended up using your android:layout_toLeftOf="#+id/tournament_winner" but left the single line and the margin to the left unused, as the result seemed perfect to me (hope this is also the case for other devices).
One thing though - in the first text view (tournament_name) I had to use android:layout_toLeftOf="#+id/tournament_winner"and not android:layout_toLeftOf="#id/tournament_winner" - pay attention to the added +. For some reason I get an error using android:layout_toLeftOf="#id/tournament_winner": Error: No resource found that matches the given name... so it seems that it is possible and NEEDED to define the resource in the time of calling it because the system doesn't know it before it was defined.
<?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:orientation="horizontal" >
<TextView
android:id="#+id/tournament_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tournament_winner"
android:layout_alignParentLeft="true"
android:textSize="25dp" />
<TextView
android:id="#+id/tournament_winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="25dp" />
</RelativeLayout>
You can use single text view in place of two and simply display both strings in one text view !!
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.