Two identical TableLayout's identical TableRow's not showing equal android - android

I created two identical TableLayout's in my xml file
And programmatically added two Identical(everything is same except variable names) TableRows with TextView's but they're not showing the same in app.
Here's the xml and the image:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/detected_drones"
android:textSize = "25sp"
android:textStyle="bold"
android:textColor="#android:color/black">
</TextView>
//FIRST TABLELAYOUT
<TableLayout
android:id="#+id/zaberlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2">
</TableLayout>
//SECOND TABLELAYOUT
<TableLayout
android:id="#+id/naberlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/zaberlist"//ONLY DIFFERENCE BETWEEN THEM
android:stretchColumns="0,1,2">
</TableLayout>
<Button
android:id="#+id/scanBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:visibility="visible"
android:background="#android:color/holo_green_dark"
android:text="#string/start_scan"
android:layout_alignParentBottom="true"/>
<Button
android:id="#+id/scanBtnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:visibility="gone"
android:background="#android:color/holo_red_dark"
android:baselineAligned="false"
android:text="#string/stop_scan"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
IMAGE
edit:
here's how I add those rows to the table:
val tr_head1 = TableRow(this#WIFIScannerActivity)
tr_head1.layoutParams = TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT
)
val label_name1 = TextView(this#WIFIScannerActivity)
label_name1.text = scanResult.SSID
if(scanResult.SSID==""){
label_name1.text = "*hidden*"
}
label_name1.textSize = 14F
label_name1.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1F)
label_name1.setPadding(20,0,0,0)
label_name1.setTypeface(null, Typeface.BOLD)
tr_head1.addView(label_name1) // add the column to the table row here
val label_mac1 = TextView(this#WIFIScannerActivity) // part3
label_mac1.text = scanResult.BSSID
label_mac1.textSize = 14F
label_mac1.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1F)
label_mac1.setTypeface(null, Typeface.BOLD)
tr_head1.addView(label_mac1) // add the column to the table row here
val label_distance1 = TextView(this#WIFIScannerActivity) // part3
label_distance1.text = String.format("%.2f", Math.pow(10.0, (((-20 - scanResult.level).toDouble())/20)))//Math.pow(10.0, ((-20 - scanResult.level.toDouble()) / 20)).toString()
label_distance1.textSize = 14F
label_distance1.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1F)
label_distance1.setPadding(0,0,20,0)
label_distance1.setTypeface(null, Typeface.BOLD)
tr_head1.addView(label_distance1)
tr_head1.setBackgroundColor(Color.LTGRAY)
tr_head1.setPadding(0,10,0,10)
//I only change xdlist here to add to different tables
xdlist!!.addView(tr_head1, TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT
))

If the data in the two tables is identical (including spaces) then in theory the results should be the same for the two tables BUT in real usage it's unlikely that you would be displaying the identical data in two tables next to each other.
So if you want to align column's correctly just use one TableLayout and multiple TableRow items, because a TableLayout is only designed to align within each Table and not between tables.
Alternative use a different type of Layout, probably better to be one based on a RecyclerView, or a ConstraintLayout, but with understanding all the type of data and how you want the data to be presented I cannot advise on the best method.

Related

Percentage in background

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);

Programmatically placing a layout view inside a table view

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?

Apply style programmatically

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);

Android: Dynamically added TextView not wrapping text

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>

Table in Android

In my Android application there is a screen with some data to be displayed in table format. This table will have around 5 columns and at least 50 rows.
How do I do I create a table in android?
I searched through and everybody seems to recommend to use TableLayout and textView for each cell. Using this technique seems to be a bit difficult as there are lots of textView Components.
Is there any other solution to this?
TableLayout lTableLayout;
lTableLayout = (TableLayout)findViewById(R.id.tblayout);
for(int i=0;i<10;i++){
TableRow tr1 = new TableRow(this);
tr1.setPadding(0, 5, 0, 0);
lTableLayout.addView(tr1, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView tv11 = new TextView(this);
tv11.setPadding(2, 0, 0, 0);
tv11.setTextSize(12);
tr1.addView(tv11,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); // Can give width and height in numbers also.
}
These will create 10 rows and 1 textview in each row in taken table layout.
take one table layout in your xml file like below :
<TableLayout
android:id="#+id/tblayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
Use ListView with Custom adapter , create five textview's in listview item as columns..
create listitem like this
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
Its not necessary to user TableLayout.
you can user Custom ListView And set header of ListView as your Column name
To create custom listview see this example

Categories

Resources