I'm stuck with the following:
Trying to place a left-aligned multiline EditText to the left of a ToggleButton for each row of a table.
Layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center_vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="none"
android:text="A very long line that should be wrapped into two or more lines as it doesn't fit on one line" />
<ToggleButton
android:id="#+id/toggleButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
</TableRow>
</TableLayout>
</LinearLayout>
This produces exactly what I'm looking for (Example: http://s8.postimg.org/6ldgsovr9/before.png).
But then I try to add a new row with this code:
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
EditText et = new EditText(this);
et.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams params = (TableRow.LayoutParams) et.getLayoutParams();
params.height = TableRow.LayoutParams.WRAP_CONTENT;
et.setLayoutParams(params);
et.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
et.setBackgroundColor(Color.TRANSPARENT);
et.setText("A very long line that should be wrapped into two ore more lines as it doesn't fit on one line");
row.addView(et);
ToggleButton tb = new ToggleButton (this);
row.addView(tb);
tl.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
Which messes up the existing line and the new line does not wrap at all (Example: http://s24.postimg.org/3ty1ne71x/after.png)
What am I missing here?
Thanks!
I face similar issue... Though its late reply, Just posting so it may help somebody...
I solved issue by adding these attributes to TableLayout
android:stretchColumns="0"
android:shrinkColumns="0"
and also set inputType for EditText
android:inputType="textMultiLine"
Try commenting the following line.
//et.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
Hope I can help you :D
If you want to set Multline on XML. Add this betwween TextView Tag:
android:maxLines="4"
android:lines="4"
If you want to set it on run time. Use this.
mTextView.setMaxLines(maxlines)
mTextView.setLines(lines)
It looks like the following line solves the display issue:
et.setEms(10);
I don't know why this makes a difference (the font size stays the same) but now the text is aligned correctly.
Related
I'm kind of stuck with a problem and I hope you can help me. Im developing an Android application with Android Studio.
I have a TableLayout with 2 elements. The first column is a LinearLayout with a percentage painted in the background, and the second column, just a simple textview (for the example, I put two rows in it).
I have solved the problem with XML representation, and I need to do the same but programmatically. Here is my XML:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id = "#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/orionBackground"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:background="#color/orionRed">
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text=""
android:layout_gravity="right"
android:textColor="#color/orionWhite"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_gravity="right"
android:background="#color/orionBackground"
android:textColor="#color/orionWhite"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/orionBackground"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:background="#color/orionRed">
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text=""
android:textColor="#color/orionWhite"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There"
android:layout_gravity="right"
android:background="#color/orionBackground"
android:textColor="#color/orionWhite"/>
</TableRow>
</TableLayout>
I need to do the same but programmatically and I don't know how could be wrong with what Im doing. Here is my code:
TableLayout table = (TableLayout)findViewById(R.id.tableLayout);
/*TextView*/
TextView textViewHelloWorld = new TextView(getBaseContext());
textViewHelloWorld.setText("");
textViewHelloWorld.setLayoutParams(
new ViewGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT));
textViewHelloWorld.setTextColor(Color.parseColor("#FFFFFF"));
/*Child Layout*/
LinearLayout linearLayout = new LinearLayout(getBaseContext());
/*Here I set the percentage of the LinearLayout to 10*/
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
0, ViewGroup.LayoutParams.MATCH_PARENT, 10f);
linearLayout.setLayoutParams(params1);
linearLayout.setBackgroundColor(Color.parseColor("#A23934"));
/*Textbox Added | I tested before the above line*/
linearLayout.addView(textViewHelloWorld);
/*Parent Layout*/
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,100f);
LinearLayout parentLayout = new LinearLayout(getBaseContext());
parentLayout.setOrientation(LinearLayout.HORIZONTAL);
/*Finally, I added the LinearLayout into de parent LinearLayout*/
parentLayout.addView(linearLayout,params);
TableRow rowHeader = new TableRow(getBaseContext());
TableRow.LayoutParams params10 = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
rowHeader.setLayoutParams(params10);
/*Then, the linear layout is Added into de TableRow*/
rowHeader.addView(parentLayout);
/*And added to the row*/
table.addView(rowHeader);
Visually, this is how I need to see it:
What I want to accomplish
This is for an app that will show the percentage of amount in a order book of cryptocurrencies.
My issue is that my solution print the whole background and should be print only a part of the background. The image above, prints the percentage, 30 and 60, defined by the layout_weight attribute, but when I do it programmatically doesnt work at all.
Why isnt working?
I really hope you can help me with this.
Thanks a lot
try to set elevation to TextView like ViewCompat.setElevation(textViewHelloWorld, 10);
so this be like
TextView textViewHelloWorld = new TextView(getBaseContext());
textViewHelloWorld.setText("sample text");
textViewHelloWorld.setLayoutParams(
new ViewGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT));
textViewHelloWorld.setTextColor(Color.parseColor("#FFFFFF"));
ViewCompat.setElevation(textViewHelloWorld, 10);
I am writing an Android app that requires me to programmatically add a TextView to a TableRow which is, in turn, added to a TableLayout. For the design to look proper, the TextView width must be the same as the TableRow width. The problem I am having is that even though I have attempted to set the width of the TextView to match using MATCH_PARENT, it is not taking effect.
Here is a sample of the Java that I am trying to use to do this:
final TableRow myTableRow = new TableRow(getApplicationContext());
final TextView myTextView = new TextView(getApplicationContext());
myTextView.setWidth(TableRow.LayoutParams.MATCH_PARENT);
myTextView.setPadding(5, 0, 5, 0);
myTextView.setTextColor(Color.BLACK);
myTextView.setTextSize(30);
myTextView.setBackgroundColor(Color.RED);
myTextView.setText("1");
myTableRow.addView(myTextView);
myTableLayout.addView(myTableRow); // Assume 'myTableLayout' has already been declared.
Here is the XML for myTableLayout:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myTableLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowOne">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textViewOne"
android:layout_weight="1"
android:textSize="40sp"
android:paddingStart="5dp"
android:paddingEnd="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowTwo">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewTwo"
android:textColor="#000000"
android:textSize="30sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowThree">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewThree"
android:textColor="#000000"
android:textSize="20sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
</TableLayout>
Does anyone have any idea what I could be doing wrong?
Here is a picture of the way the layout appears. The code posted here is not identical to the actual app code, but it is enough to show the problem. Specifically, the code that was removed concerns the replies. In the example code posted as part of the question, only one TextView is shown in the Java code, but in the actually project, there are two.
Layout Example Image
There are several ways you can handle this.
TableLayout and TableRow both extends linearlayout, so maybe you need to set Orientation.
you could set the layout params(margin params) like this
layout_width = 0dp ;
layout_weight = 1 ;
The best way to give height and width property to a view is to use Layout Parameters. most of times (almost all the times for me) setting width and height directly wont work.
TableRow.LayoutParams mParam=new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// set layout parameter to view
textView.setLayoutParams(mParam);
I have a layout file called row which is basically a wrapper for how i want my row to look like
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/tableRow"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/oid"
android:layout_width="0dp"
android:layout_weight="1" android:background="#dcdcdc"
android:textColor="#000000"
android:padding="5dip"
/>
<TextView
android:id="#+id/value"
android:layout_width="0dp"
android:layout_weight="1" android:background="#d3d3d3"
android:textColor="#000000"
android:padding="5dip"
/>
<TextView
android:id="#+id/type"
android:layout_width="0dp"
android:layout_weight="1" android:background="#dcdcdc"
android:textColor="#000000"
android:padding="5dip"
/>
</TableRow>
</LinearLayout>
Now i d like to be able to create a row using this layout and place it in my table that is defined in the fragment layout. This is my current code for creating a row(it doesn't use the row layout file) and adding it to the table, but the row is not formatted as i d like it to be.
TableLayout varbindTable = (TableLayout) view2.findViewById(R.id.tableLayout1);
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
varbindTable.addView(row);
TextView name = new TextView(getActivity());
name.setText(((EditText)view.findViewById(R.id.oid)).getText().toString());
name.setTextColor(Color.BLACK);
name.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
TextView value = new TextView(getActivity());
value.setText(((EditText)view.findViewById(R.id.value)).getText().toString());
value.setTextColor(Color.BLACK);
value.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
TextView type = new TextView(getActivity());
type.setText(((Spinner)view.findViewById(R.id.data_type_spinner)).getSelectedItem().toString());
type.setTextColor(Color.BLACK);
type.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
row.addView(name);
row.addView(value);
row.addView(type);
Well, at first glance, the issue looks like your settings are different in the 2 examples. In the xml, you are using layout_width="0dp" and layout_weight="1". However, in the code you are using MATCH_PARENT.
Closest example I could find:
How do you setLayoutParams() for an ImageView?
I'm trying to build a dynamic table in my Android application. I would like to have a 70% wide column and a 30% wide column.
If I do it with the layout xml using android:layout_weight I don't have any problem:
<TableLayout
android:id="#+id/TableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#ffffff" >
<TableRow
android:id="#+id/TableRow4"
style="#style/RigaTabella"
android:background="#d2ef7b" >
<TextView
android:id="#+id/TitoloSpesaTabella"
style="#style/Titolo"
android:layout_width="0px"
android:layout_weight="0.7"
android:background="#cdcdcd"
android:text="#string/spesa" />
<TextView
android:id="#+id/TitoloCostoTabella"
style="#style/Titolo"
android:layout_width="0px"
android:layout_weight="0.3"
android:background="#ababab"
android:text="#string/costo" />
</TableRow>
</TableLayout>
This is what I obtain: http://i.imgur.com/DHpXBzJ.jpg
(I used this gray background to show the real dimension of the two textView)
However if I try with this code:
TableLayout TableLayout = (TableLayout) findViewById(R.id.TableLayout);
for (int i = 0; i < 10; i++) {
TableRow Row = (TableRow) getLayoutInflater().inflate(R.layout.table_row_style, null);
if (i % 2 == 0)
Row.setBackgroundColor(Color.parseColor("#ffffff"));
else
Row.setBackgroundColor(Color.parseColor("#f1f1f1"));
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.testo_sx_style, null);
tv.setText("Test Test");
TextView tv2 = (TextView) getLayoutInflater().inflate(R.layout.testo_dx_style, null);
tv2.setText("$ 1000");
Row.addView(tv);
Row.addView(tv2);
TableLayout.addView(Row);
}
where testo_sx_style.xml is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="left"
android:textSize="13sp"
android:textColor="#161616"
android:typeface="monospace"
android:background="#cdcdcd"
/>
and where testo_dx_style.xml is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="left"
android:textSize="13sp"
android:textColor="#161616"
android:typeface="monospace"
android:background="#ababab"
/>
I obtain this: http://i.imgur.com/oe6YHsz.png
I really don't know what it's wrong. I also tried, for example, to set in testo_sx_style.xml the layout_width to 100px but it doesn't change anything. It seems that some properties aren't applied to my code.
Anyone know what is the error? Thanks in advance.
Have a good day :)
p.s. Sorry for my English
Solved! With just this line of code:
tv.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.7f));
With this I can set the width to 0px, the height to WRAP_CONTENT and the weight to 0.7.
It works great :)
p.s. I found the solution here: Set the layout weight of a TextView programmatically
You could possibly try something like this, using the weight parameter of the layout:
TableRow tr1 = new TableRow(this);
tabInfo = new TableLayout(this);
tabInfo.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 4.0f));
tr1.addView(tabInfo);
Dynamically adding TextViews to this layout results in text not being wrapped.
<!-- R.layout.description_card -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout_InfoShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/table_row_padding_bottom" >
<TableLayout
android:background="#drawable/card_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/TableRow_DescriptionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/DescriptionTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/page_name_size"
android:text="Loading data..." />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/DescriptionTitle">
<LinearLayout android:id="#+id/LinearLayout_Description"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Dynamically added TextView here -->
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
I dynamically add the TextViews to this layout using these methods:
View view = inflater.inflate(R.layout.description_card, null);
TextView title = (TextView) view.findViewById(R.id.DescriptionTitle);
LinearLayout description = (LinearLayout) view.findViewById(R.id.LinearLayout_Description);
ArrayList<TextView> textViews = des.getText();
Iterator<TextView> iter = textViews.iterator();
while(iter.hasNext()){
TextView textView = iter.next();
description.addView(textView);
}
This results in my textviews not wrapping onto new lines, even though it works perfectly fine if I include the TextView directly in the XML file.
It results in this:
Edit:
TextView layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TextViewDescription"
style="#style/AppTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="8dp"
android:text="Loading data..."
android:textSize="#dimen/description_size"
android:layout_weight="1"
android:ellipsize="none"
android:scrollHorizontally="false" />
You probably need to set it to multi line. Try
setInputType (InputType.TYPE_TEXT_FLAG_MULTI_LINE);
If you have other InputType attributes, you'll need to "or" them together.
At this part of your code, you are getting your TextView array:
ArrayList<TextView> textViews = des.getText();
And we don't see how those TextView's are created. But I assume you did not set single_line or max_lines to TextViews.
Try this code while adding your TextViews to your LinearLayout:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
while (iter.hasNext()) {
TextView textView = iter.next();
description.addView(textView, params);
}
Edit:
Use below layout while creating your TextViews(I've removed some attrs and style.):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TextViewDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="8dp"
android:text="Loading data..."
android:textSize="#dimen/description_size"/>
Edit2: Change your layout_width param to wrap_content. Also see updated param at code above. (This worked for me)
<LinearLayout android:id="#+id/LinearLayout_Description"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Dynamically added TextView here -->
</LinearLayout>
Try using something other than TableLayout. I'm not very experienced with TableLayout but I think it needs the child views to have relatively short and consistent widths.
Instead, try using LinearLayout, like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout_InfoShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/table_row_padding_bottom" >
<TextView
android:id="#+id/DescriptionTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/page_name_size"
android:text="Loading data..." />
<!-- Dynamically added TextView here -->
</LinearLayout>
And add the views with more-or-less the same code you had before. Note I changed the parent view for supporting the adds -- outerLayout.
View view = inflater.inflate(R.layout.description_card, null);
TextView outerView = (TextView) view.findViewById(R.id.LinearLayout_InfoShow);
ArrayList<TextView> textViews = des.getText();
Iterator<TextView> iter = textViews.iterator();
while(iter.hasNext()) {
TextView textView = iter.next();
outerView.addView(textView);
}
Found the reason.
The inflater needed a ViewGroup for the layout params to take effect.
I had to change the way I pass the text through to the iterator but this is basically what needed changing:
TextView textView = (TextView) inflater.inflate(text.getViewID(), description, false);
For the problem...
When you have
<TableLayout>
<TableRow>
<TextView>
</TextView>
</TableRow>
</TableLayout>
to make the text appear without truncation,
add
android:layout_weight="1"
inside the textview
<TextView>
</TextView>