android EditText not automatically wrapping text - android

I have looked all over the place and there are a ton of similar questions on this site, but I haven't found one that works and I'm wondering what I'm doing wrong. I have an EditText that I have to add programatically, it typically has only one line of text but I need it to wrap if the text is longer than the width of the view.
the only xml i am using is for the table layout the edit text is eventually added to
<TableLayout
android:id="#+id/LineItemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:stretchColumns="4"
>
</TableLayout>
The edit text is the one in the 4th column but even when I removed the stretchColumns attribute it still wasn't working. (i was thinking maybe the stretching column was messing up the width and it couldn't calculate when to wrap)
Here is the code that gets looped through for each row I add to the table, p is the EditText I cant get working properly. just a bit of explanation, the row contains an EditText for a number (this one is fine, i do not want it multiline), then two Buttons and a TextView, and then the last item is the EditText that stretches the rest of the width and should be wrapping text. i put them all here in case they are causing problems because at this point i have no idea what to try next.
final TableRow l = new TableRow(EnvelopeModify.this);
TextView t = new TextView(EnvelopeModify.this);
t.setText("$" + lineItems.get(x).getTotal() + ", " + lineItems.get(x).getQuantity() + " " + lineItems.get(x).getItemUnits() + "");
t.setPadding(5, 5, 30, 5);
EditText p = new EditText(EnvelopeModify.this);
p.setText(lineItems.get(x).getProposalDesc());
p.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
p.setBackgroundResource(R.color.blueEditText);
p.setMinLines(1);
p.setHorizontallyScrolling(false);
p.setImeOptions(EditorInfo.IME_ACTION_DONE);
//TableLayout.LayoutParams tempParams = new TableLayout.LayoutParams(0,LayoutParams.WRAP_CONTENT,1f);
//p.setLayoutParams(tempParams);
EditText o = new EditText(EnvelopeModify.this);
o.setText(lineItems.get(x)._order);
o.setWidth(50);
o.setInputType(InputType.TYPE_CLASS_NUMBER);
Button d = new Button(EnvelopeModify.this);
d.setText("Remove");
Button e = new Button(EnvelopeModify.this);
e.setText("Edit");
l.addView(o);
l.addView(d);
l.addView(e);
l.addView(t);
l.addView(p);
lineItemLayouts.add(l); // this is the tableLayout in the xml above
supposedly setHorizontallyScrolling(false) will allow wrapping but adding that just made things worse. Not only does the text not wrap, but the text that should be wrapping is not visible because the edittext is only showing the first part of the text (not scrolling). I can still hit enter and a new line will be created as expected but I cannot get any text to wrap.

The problem is with the TableLayout that the EditText is in. The EditText is in the stretchable column, setting the column to also be shrinkable fixed the issue. Here is the change I made to the xml layout.
<TableLayout
android:id="#+id/LineItemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:stretchColumns="4"
android:shrinkColumns="4"
>
And here is what I have in the coding side that ended up working for me:
EditText p = new EditText(EnvelopeModify.this);
p.setText(lineItems.get(x).getProposalDesc());
p.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
p.setBackgroundResource(R.color.usedOnProposal);
p.setMinLines(1);

You're disabling horizontal scrolling in this line.-
p.setHorizontallyScrolling(false);
As for the multiline issue, you could try.-
p.setSingleLine(false);
p.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);

Related

Android EditText moving when MultiLine in TableRow

I have a Tablelayout with many TableRows inside. And inside these TableRows, many EditText. Like a spreadsheet.
My problem is the following: when something is wrote in the EditBox, the EditBoxs are still aligned:
But when the content of the EditBox goes to multi-line, my EditBox are not aligned anymore:
I thought that maybe the problem comes from the fact that my EditBox is not centered vertically in my TableRow, but this is something that I couldn't do.
Here is my code for the EditText, I add dynamically each EditText in my TableRows:
private EditText editTextCellule(int num, String texte, boolean pair){
EditText cellule = new EditText(this);
cellule.setLayoutParams(new TableRow.LayoutParams(num));
cellule.setWidth(LARGEUR_CELLULE);
cellule.setHeight(HAUTEUR_CELLULE);
cellule.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_size_medium_minus));
cellule.setText(texte);
cellule.setKeyListener( new PerfKeyListener() );
return cellule;
}
What can I do?
You provide layout weight to the following rows so they won't move.....
android:layout_weight = "1"
in table rows....

Android Making a table look like I want

I am having a little bit of trouble making the table look like I intend to.
These are a few questions, but since they all refer to the picture below and the details I provide I thought they should all be in a single post.
Here is what I achieved so far:
The header row contains one element of type Button.
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
Button bt = new Button(getContext());
bt.setText("Column1");
mHeader.addView(bt, params);
mHeader.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
addView(mHeader);
The rest of the table is poulated like this:
(Messagerow extends TableRow and has a TextView member)
for(int i = 0; i < 100; i++) {
MessageRow mr = new MessageRow(getContext());
// stuff to set the TexView text and color
mr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
this.addView(mr);
}
1. How can I make the header row height be more like the rows?
2. How can I make the button occupy the full width of the row?
If the table is empty, no text rows just header, then the button matches the row width. As soon as I add a row of text, the column width is adapted but the button width is not.
3. How can I make the row fill the screen width? (MATCH_PARENT does not do it)
4. How can I draw a thin line between the table rows?
I tried to override the onDraw() function on MessageRow, but it never gets called, not even once.
Don't get me wrong. I am not asking that you do my work for me. These are issues I tried to solve by myself and googled them and read similar posts, but did not find an answer.Note: I find that UI design in Javascript for Android lacks clear control and clear documentation over all these little details.
Edit
This is how I create the table:
TableLayout mTable = new TableLayout(this);
HorizontalScrollView hview = (HorizontalScrollView) findViewById(R.id.hscroll);
populate(mTable);
mTable.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
mTable.setBackgroundColor(Color.WHITE);
hview.addView(mTable);
How can I make the header row height be more like the rows?
Using the default Button there isn't much to do. The Button uses a nine-patch image that has some space between the button's text and the borders that you see. You could use a smaller font but that you'll probably look ugly. Another thing to try is using your own background for the Button and get rid of the default extra space(of the default nine-patch image) so the final height is near the height of the text from the TextViews. Or try to enforce a standard height for all rows using a fixed value.
How can I make the button occupy the full width of the row?
I think that you have more then one TextView in MessageRow so when you add the Button it moves to the first column(corresponding to the first TextView). If this is the case, make your Button span across the number of columns representing the number of TextViews in MessageRow:
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
params.span = 3; // if you have 3 `TextView` in the MessageRow
Button bt = new Button(getContext());
bt.setText("Column1");
mHeader.addView(bt, params);
If this is not the case add more details.
How can I make the row fill the screen width? (MATCH_PARENT does not do it)
As I said on one of your previous questions, I don't know why that happens(but I gave you some solutions there to overcome this issue). Also:
mHeader and the other MessageRow are children of a Tablelayout and the correct LayoutParams to use on them is the LayoutParams of the parent: TableLayout.LayoutParams and not TableRow.LayoutParams.
You add some TextView in the MessageRow(from what I seen in your previous questions), add those child views with TableRow.LayoutParams to MessageRow.
You use only WRAP_CONTENT for your LayoutParams everywhere in your code, you might want to set the width(the first parameter in the constructor) to FILL_PARENT/MATCH_PARENT
How can I draw a thin line between the table rows?
You could use a simple View that will act as a separator:
for(int i = 0; i < 100; i++) {
MessageRow mr = new MessageRow(getContext());
// stuff to set the TexView text and color
mr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
this.addView(mr);
View separator = new View(getContext());
separator.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, 3)));
separator.setBackgroundColor(Color.RED);
this.addView(separator);
}
Because you have 100 rows you could try to set a drawable with a separator line as the background for theTableRow(header and MessageRow) instead of the above method that adds another 100 Views to the layout.
Extra Note:
You have a lot of views to add to a single activity layout, you are talking about 100 rows, and if your MessageRow is more complex than a simple TextView(and I think it is) you could get in some performances problems. I suggest you take a look at the wonderful ListView widget.
Don't have a programming environment here, but I'll try and answer some of your questions.
The reason your header row (button) is taller than your test based rows is because the button requires more space and the row accomodates it. The default button has padding on both the top/bottom of the text. I think your best option is to create your own button, which gives you the additional benefit of being able to control the look and feel. It seems like other people have had this issue before: Can't get rid of bottom padding on button
Your button is set to wrap_content which means it won't be any bigger than it needs to be (It will grow/shrink so it can fit the text "Column1" or whatever you put there). Instead of making the Button WRAP, I suspect you'll need to make it FILL_PARENT.
It's not your Table Row that needs to fill the screen width, it's your table that needs to fill the screen. Wherever you define your table, it's probably set to WRAP_CONTENT for the Horizontal dimension. Set it to FILL_PARENT and your table should expand to the full width of whatever it's container is (In this case, it should expand the full width of the screen)
There are probably several different ways you can do this. One method I used somewhat recently is to utilize the View tag which essentially looks like a horizontal bar across the screen. Below is a link to how to implement it.
http://sonnygill.net/android/horizontal-rule/

Android - Add Lines to Textview

Ive got a TextView and some String Arrays. I want to add a Line (trough Java) everytime the next Item is displayed(Ive got a for loop, so it loops until every Item is displayed in the TextView). Does anybody know how to do that?
Thank you
EDIT:
I want It to look like a ListView. But I dont want to use a listView insted because I want to set the Line just for certain events.
for(int i=0;i<lenght;i++)
{
//textview is tv
tv.setText(listOfString[i]);
//view is viewWithHorizontalLine with hight = dp and width = fill_parent and background = #000000
viewWithHorizontalLine = view;
//LinearLayout is ll
ll.addView(tv);
ll.addView(viewWithHorizontalLine);
}
XML
<LinearLayout>
<ScrollView>
<LinearLayout orientation=vertical/>
</ScrollView>
</LinearLayout>
What kind of line? If you just want a vertical line in your text, add "|" between each word. If you want a new line, use "\n".
Have you tried adding a newline (\n) character each time you add one of your strings?

Android Tablelayout problem!

I have a TableLayout in which I have one row holding two TextViews, the first one holds a String representation of a Date, the second one holds a title. My problem is the first one should always be 100dp wide, the seconde one should use the spaces that's left. The first TextView works fine and it will go to a new line if the string cannot be displayed in one line. But the problem is the second TextView, it is always too wide (its width expands out of the screen), no matter how I change its size, and I can only see part of the text that's inside it. Anyone knows how to fix this?
txtView1 = new TextView(this);
LinearLayout lLay= new LinarLayout(this);
txtView2 = new TextView(this);
lLay.addView(txtView2);
row.addview(txtView1);
row.addview(lLay,220,Layout.Wrap_Content);
just try this coz it solve my problem.

How to automatically resize an EditText widget (with some attributes) in a TableLayout

I have a layout issue. What I do is this:
create TableLayout in xml with zero children:
<TableLayout android:id="#+id/t_layout_contents"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/l_layout_tags"
android:stretchColumns="1"
android:paddingLeft="5dip"
android:paddingRight="5dip" />
Insert first row programmatically in onCreate():
TableLayout tLayoutContents = (TableLayout)findViewById(R.id.t_layout_contents);
NoteElement nr_1 = new NoteElement(this);
tLayoutContents.addView(nr_1);
Class "NoteElement" extends TableRow. The 1st row just consists of a blank ImageView as a placeholder and an EditText to enter text. NoteElement's constructor looks like this:
public NoteElement(Context c) {
super(c);
this.context = c;
defaultText = c.getResources().getString(R.string.create_note_help_text);
imageView = new ImageView(context);
imageView.setImageResource(android.R.color.transparent);
LayoutParams params = new LayoutParams(0);
imageView.setLayoutParams(params);
addView(imageView);
addView(addTextField());
}
Method addTextField() specifies the attributes for the EditText widget:
private EditText addTextField() {
editText = new EditText(context);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setMinLines(4);
editText.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setHint(R.string.create_note_et_blank_text);
editText.setAutoLinkMask(Linkify.ALL);
editText.setPadding(5, 0, 0, 0);
editText.setGravity(Gravity.TOP);
editText.setVerticalScrollBarEnabled(true);
LayoutParams params = new LayoutParams(1);
editText.setLayoutParams(params);
return editText;
}
So far, so good. But my problem occurs as soon as the available space for the chars is depleted. The EditText does not resize itself but switches to a single line EditText.
I am desperatly looking for a way in which the EditText resizes itself in its height dynamically, being dependant on the inserted text length.
Does anyone have a hint on this?
Okay, I got it. This seems to be an issue of TableLayout in general. I reimplemented the Layout with a simple LinearLayout. I serves the purpose equally and the EditText is displayed properly. In fact I don't see a reason to use a TableLayout and right now I can't think of a situation in which one would actually need it, i.e. a LinearLayout would be insufficient.
So I recommend using other Layouts like LinearLayout or RelativeLayout whenever possible. But note that these are just my two cents...
I just took another look at TableLayout (for an entirely different purpose) and stumbled upon setColumnShrinkable(int columnIndex, boolean isShrinkable) which should have helped me out on my former issue.
See the documentation for details:
https://developer.android.com/reference/android/widget/TableLayout.html#setColumnShrinkable%28int,%20boolean%29
Please note that I haven't tested this.

Categories

Resources