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....
Related
I've been making all of my views dynamically, and now I've come to the point where I want to add an EditText for people to write in.
I've been able to accomplish this for the most part, but it doesn't look right. I have a linear layout that I'm adding a relative layout to. I'm making the relative layout have a white background, then adding the EditText. Problem is, it always adds it to the direct center of the relative layout, and options to align it vertically to the top have so far failed.
I also need to be able to pull the text from it later when a separate button was pressed (I know how to make the button work, it's the pulling text from it part I'm a bit iffy on). Here's my code so far:
public void addEditText(LinearLayout L){
EditText myEditText = new EditText(c);
myEditText.setSingleLine(false);
RelativeLayout l1 = new RelativeLayout(c);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(scWidth1, 300);
lp.gravity=Gravity.CENTER;
l1.setLayoutParams(lp);
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER_VERTICAL);
l1.addView(myEditText);
L.addView(l1);
}
l1.setGravity(Gravity.CENTER_VERTICAL); places the EditText in center vertical of the parent container i.e RelativeLayout, remove that line.
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);
I have a simple layout in a table that consists of 3 rows. The second column may have either an EditText or a TextView like in the picture below:
Now, my question is how can I align the start of the text in EditText and TextView so that they are visually under each other (the texts, not the controls)? In the image above, the text in the second line starts too much to the left. I don't want to hardcode the padding as it may not work the same way on different devices.
try setting a padding for the second textview.
android:padding="5dp"
Instead of making the TextView match the EditText, make the EditText match the TextView by making its background null:
<EditText ... android:background="#null" ... />
The underline in the EditText then goes away and everything lines up perfectly.
I have a form with a format of:
<LinearLayout>
<RelativeLayout>TextView EditText Button</RelativeLayout>
<RelativeLayout>TextView EditText Button</RelativeLayout>
<RelativeLayout>TextView EditText Button</RelativeLayout>
<RelativeLayout>TextView EditText Button</RelativeLayout>
</LinearLayout>
I have aligned the TextView to parentLeft, the Button to parentRight. And so far, the EditText to toLeftOf Button. Now, I want the EditText to all line up with the longest TextView, however I can't seem to use toRightOf of a TextView from a different layout. I'm not even sure that's the best way to do it.
What is the proper way to get everything to line up straight?
Consider using TableLayout instead
You can try replacing the LinearLayout with RelativeLayoutand and remove the RelativeLayout for each row or use TableLayout as asahi suggested.
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.