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);
Related
I am developing an Android quiz game.
When a user replies to the first question I open this dialog to show its progress:
This is the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/txtLevel1"
android:layout_alignTop="#+id/txtLevel1"
android:layout_centerInParent="true"
android:background="#color/orange" />
<TextView
android:id="#+id/txtLevel3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5.000" />
<TextView
android:id="#+id/txtLevel2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtLevel3"
android:text="2.500"/>
<TextView
android:id="#+id/txtLevel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtLevel2"
android:text="1.000"/>
</RelativeLayout>
Is there any way to move the View element (the orange box) from txtLevel1 (1.000 points) to txtLevel2 (2.500 points) maybe with an animation/transition and not statically?
You could try this to move the orange box from txtLevel1 to txtLevel2:
View orangeBox = getActivity().findViewById(R.id.orange_box);
TextView tv = getActivity().findViewById(R.id.txtLevel2);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(300, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
p.addRule(RelativeLayout.ALIGN_TOP, tv.getId());
orangeBox.setLayoutParams(p);
For the animation this might help you.
I'm trying to set programmatically a ToggleButton inside of a Gridview. All seems that is working, but I can't set some properties like the margins between the togglebuttons that I'm adding in a loop for and this property: toogleBtn.setAllCaps(false); don't work.
Thanks in advance
This is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/screen"
android:id="#+id/ly_general"
android:fitsSystemWindows="true"
android:background="#color/colorPrimary">
<TextView
android:text="#string/your_interest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="#dimen/text_size_22"
android:id="#+id/text_init_session"
android:layout_marginTop="#dimen/margin_26"
android:layout_marginLeft="#dimen/padding_20"
android:layout_below="#+id/image"/>
<TextView
android:text="#string/select_categories"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_init_session"
android:layout_alignLeft="#+id/text_init_session"
android:layout_alignStart="#+id/text_init_session"
android:layout_marginTop="22dp"
android:id="#+id/textViewExplanation" />
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
style="#style/screen"
android:id="#+id/scroll"
android:layout_below="#+id/textViewExplanation">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lyCategory"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:columnCount="3"
android:layout_marginTop="#dimen/padding_40">
</GridLayout>
</ScrollView>
</RelativeLayout>
This is my code in java class:
for (int i = 0; i < arrayList.size(); i++) {
final ToggleButton toogleBtn = new ToggleButton (RegisterThreeActivityNew.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, GridLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 20, 20, 20);
toogleBtn.setLayoutParams(params);
toogleBtn.setWidth(140);
toogleBtn.setHeight(20);
toogleBtn.setTextSize(12);
toogleBtn.setAllCaps(false);
toogleBtn.setText(arrayList.get(i).getName());
toogleBtn.setTextOff(arrayList.get(i).getName());
toogleBtn.setTextOn(arrayList.get(i).getName());
toogleBtn.setTextColor(ResourcesCompat.getColor(getResources(), R.color.white, null));
toogleBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.btn_category, null));
layoutCategories.addView(toogleBtn);
}
In your xml file inside toggle button set these properties
android:minWidth="10dp" // use what ever minimum value you need
android:minHeight="10dp"
if you dont decrease the minwidth in xml then how much ever you try to decrease its width & height it won't work.I suffered with this problem for 2 days
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);
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.