display 2D array in matrix format - android

I want my 2D array to be displayed as matrix format. For that which layout is suitable? Can I have any example for that?

The simple answer is to use a TableLayout with TextViews inside.
But if you want to display a large array, you should make the matrix scrollable in both directions. Below is the code that populates a matrix that is displayed as a series of EditTexts inside rows, inside a TableLayout. The TableLayout is made scrollable in both directions using two ScrollView. Adapt the code to your needs. table is the TableLayout with the id tableLayout1.
private void fillTable(final int n, final double[][] matrix, TableLayout table) {
table.removeAllViews();
for (int i = 0; i < n; i++) {
TableRow row = new TableRow(MainActivity.this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < n; j++) {
EditText edit = new EditText(OutputActivity.this);
edit.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);
edit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
edit.setText(Double.toString(matrix[i][j]));
edit.setKeyListener(null);
row.addView(edit);
}
table.addView(row);
}
}
The layout 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"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>

Related

Table Layout setStretchAllColumns(true) inside HorizontalScrollView does not work in Lollipop

I have a table layout which I placed inside a HorizontalScrollView, as the number of edittext inside each row of table is dynamic, I am programmitically putting them. The following codes work perfectly fine in version 9 (Pie) ( and also probably in Oreo Go ) but does not work in Lollipop. The xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/activity_main"
tools:context=".MainActivity">
<EditText
android:id="#+id/mrinp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Number"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:onClick="generate"
/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/tbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_marginBottom="20dp" />
</HorizontalScrollView>
</LinearLayout>
Main activity
public class MainActivity extends AppCompatActivity {
EditText edr;
TableLayout tl;
LinearLayout myLayout;
int M;
int layoutWidth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edr = findViewById(R.id.mrinp);
tl = findViewById(R.id.tbl);
tl.setStretchAllColumns(true);
tl.setBackgroundColor(getResources().getColor(R.color._light_green));
}
public void generate(View view) {
tl.removeAllViews();
M = Integer.parseInt(edr.getText().toString());
int setWidth;
for (int i = 0; i < M; i++) {
TableRow tr = new TableRow(this);
tr.setBackgroundColor(getResources().getColor(R.color.cold));
for (int j = 0; j < M; j++) {
if(i==0){
tl.setColumnStretchable(j, true);
}
EditText ed = new EditText(this);
ed.setMinWidth(70);
tr.addView(ed, j);
}
tl.addView(tr);
}
}
}
Note that any one of android:stretchColumns="*" or tl.setStretchAllColumns(true); or tl.setColumnStretchable(j, true); works as desired in Pie. Here is the snapshot.
But the above code does not work (none of those three I mentioned) in Lollipop.
Here is the snapshot.
Note that if I replace HorizontalScrollView with LinearLayout in Lollipop it works fine.
Any suggestion?
As per this
Add android:fillViewport="true"
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<TableLayout
android:id="#+id/tbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_marginBottom="20dp" />
</HorizontalScrollView>

How to make equal size column width of two tables

This is the table header:
<android.support.v4.widget.NestedScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:id="#+id/roster_bat_stats_table_header_adv1"
android:scrollbarStyle="outsideInset"
android:fillViewport="true"
android:layout_gravity="top">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/divider_stand_table_list"
android:showDividers="middle"
android:isScrollContainer="true"
android:stretchColumns="9"
android:orientation="horizontal">
<TableRow android:layout_width="match_parent"
android:id="#id/header_table_row"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="BAT"
android:background="#drawable/mybutton"
android:textAlignment="center"
android:textColor="#android:color/white"
android:layout_weight="0.0652"
android:textSize="18sp" />
etc...
Here is the actual table without header:
<android.support.v4.widget.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/nestedScrollBatStatsAdv1"
android:scrollbars="vertical"
android:scrollbarStyle="outsideInset"
android:fillViewport="true"
android:layout_gravity="top">
<TableLayout
android:id="#+id/rosterBatStatsTableAdv1"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"
android:divider="#drawable/divider_stand_table_list"
android:showDividers="middle"
android:isScrollContainer="true"
android:stretchColumns="9"
android:orientation="horizontal">
</TableLayout>
</android.support.v4.widget.NestedScrollView>
I'm adding android:layout_weight="0.0652" in the header section above.
The actual table rows are added programmaticaly by creating TableRow and setting weights for each element inside of it:
double[] weigths = new double[]{0.0893, 0.0893, 0.2500, 0.0833, 0.1012, 0.0952, 0.0893, 0.1071, 0.0952};
creating table row:
TableRow tableRow = new TableRow(context);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
tableRow.setLayoutParams(lp);
tableRow.setOrientation(TableRow.HORIZONTAL);
tableRow.setBackgroundColor(resources.getColor(trainingPlayerSelectRowColor));
tableRow.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tableRow.setTag(player.getId());
Creating elements inside of tablerow:
for (int i = 0; i < weights.length; ++i) {
TextView h1 = new TextView(context);
h1.setText(values[i]);
TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[i]);
h1.setLayoutParams(params);
h1.setTextColor(resources.getColor(standingsTableHeadTXT));
if (i == 2) {
h1.setGravity(Gravity.LEFT);
} else {
h1.setGravity(Gravity.CENTER);
}
h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tableRow.addView(h1);
}
The values array is an array of Strings.
I made weights (layout_weight) in the header_table_row same as the rows added programatically in the actual table. But they do not match:
Is there any mistake or how can I make columns in both tables match?

my table dynamic layout keep appearing in the left side even the gravity is right

what i am trying to do is to make a dynamic table layout .. i mean the rows will be filled dynamically with text views and with scrolling horizontal and vertical but each time when the activity open my table layout start showing from the left side even i set the gravity to be right .. why this is happening?
this is my xml code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:layout_gravity="right" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_gravity="right">
</TableRow>
</TableLayout>
</ScrollView>
</HorizontalScrollView>
</LinearLayout>
and here is my java code :
TableLayout ll = (TableLayout) findViewById(R.id.table1);
int razan=0;
int c=-1;
for(int s=1; s<=fixed;s++){
if(razan<=count){
for (int i = 0; i <text.length; i++) {
if(i>0 && i%7==0){
c = c+1;
TableRow row1= new TableRow(this);
TableRow.LayoutParams tr1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row1.setLayoutParams(tr1);
TextView emptyrow = new TextView(this);
emptyrow.setText("");
// emptyrow.setBackground(getResources().getDrawable(R.drawable.back));
row1.addView(emptyrow);
ll.addView(row1,i);
}
TableRow row= new TableRow(this);
TableRow.LayoutParams tr = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(tr);
TextView product = new TextView(this);
TextView product_j = new TextView(this);
product_j.setText(prodlist.get(razan));
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.back);
product_j.setBackgroundDrawable(drawable);
product_j.setGravity(Gravity.RIGHT);
product_j.setTextSize(18);
product.setText(text[i]);
// product.setBackgroundDrawable(drawable);
product.setGravity(Gravity.RIGHT);
product.setPadding(3, 0, 3, 0);
product.setTextSize(18);
row.addView(product_j);
row.addView(product);
razan++;
ll.addView(row,i);
//here is the code for adding an empty row after each seven inner rows !!!
}//inner for
}
}//outer fors
}
can anyone help me?
Make your root LinearLayouts Height and width as match_parent and try gravity to right on your TableLayout

Dynamic TableLayout Android

I am creating a android application where i have to show contents of sqlite in Android app.
So i decide to use TableLayout here.But the main point here is that columns and rows from sqlite database is not previously decided,so i need to create that type of layout that deal with dynamic columns and rows.
So here what i am doing is that i have created a layouts sublayout.xml, cells.xml ,mainlayout.xml.
sublayout.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"
android:orientation="horizontal"
android:id="#+id/subLayout_" >
</LinearLayout>
In this layout i am dynamically adding cells using cells.xml.
cells.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"
android:orientation="horizontal" >
<TextView
android:id="#+id/cell_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cell"
android:textSize="25dp"/>
</LinearLayout>
And when my one row gets ready i simply added it to mainlayout.xml.
mainlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/tableLayout_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</TableRow>
</TableLayout>
But i am getting nullpointer exception here.
Here is my class code.
LinearLayout layout,subLayout_;
layout=(LinearLayout)findViewById(R.id.tableLayout_);
subLayout_=(LinearLayout)findViewById(R.id.subLayout_);
for (int i = 0; i < columns.length; i++) {
Log.e("Column name", ""+columns[i]);
View v=inflater.inflate(R.layout.cells, null);
TextView tv=(TextView)v.findViewById(R.id.cell_);
tv.setText(columns[i]);
subLayout_.addView(v);
}
layout.addView(subLayout_);
Please help me what i am doing wrong here ?
Try this
layout = (LinearLayout) findViewById(R.id.tableLayout_);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.sublayout, null);
subLayout_ = (LinearLayout) vi.findViewById(R.id.subLayout_);
for (int i = 0; i < columns.length; i++) {
Log.e("Column name", "" + columns[i]);
ViewGroup v = (ViewGroup) inflater.inflate(R.layout.cells, null);
TextView tv = (TextView) v.findViewById(R.id.cell_);
tv.setText(columns[i]);
subLayout_.addView(v);
}
layout.addView(subLayout_);
You could try this approach here.
Design your xml like this:
<TableLayout>
<Tablerow>
<Add your views here dynamically>
</TableRow>
</TableLayout>
Create the textviews dynamically and go on adding it to the TableRow and finally add the TableRow to the TableLayout.

Stretching columns on TableLayout has no effect

I am working on an android project and I am making use of the table layout but I am experiencing an issue.
As mentioned in the title, I am using a TableLayout and trying to stretch all of the columns to fit the screen but for some reason it has no affect, and all the columns are squished over to the left.
Below is the XML layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="#+id/resultTable"
android:stretchColumns="*"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
Below is how I am populating the TableLayout
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
try
{
Log.d("Returned in Result", String.valueOf(result.length()));
resultTable.removeAllViews();
resultTable.setStretchAllColumns(true);
for (int i = 0; i < result.length(); i++)
{
TableRow tr = new TableRow(getActivity());
JSONArray array = result.getJSONArray(i);
Log.d("Returned in Result", "Loaded Columns: " + String.valueOf(array.length()));
for (int j = 0; j < array.length(); j++)
{
//LinearLayout linearLayout = new LinearLayout(getActivity());
//LayoutParams parms = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//linearLayout.setLayoutParams(parms);
TextView textView = new TextView(getActivity());
textView.setText(array.getString(j));
textView.setSingleLine();
//linearLayout.addView(textView);
tr.addView(textView);
}
resultTable.addView(tr);
}
}
catch (Exception ex)
{
Log.e("Load Result", ex.toString());
CrashReporter.ReportCrash(ex, Severity.Critical);
}
}
});
Thanks for any help you can provide.
UPDATE
I've found it the HorizontalScrollView that is causing the columns to not stretch. If I remeove the horizontal scroll view it works fine but with the horizontal scroll view the columns don't stretch. How can I get this working as I need to have the horizontal scroll view.
I might be wrong but I had something similar , I didn't had any table layout but I wanted to have a scroll view inside a relative layout so I had to do something like ,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/theme"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="34dp" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
This mighy not be of any use but this is what made my "columns are squished over to the left." problem solved.
EDIT: Try having your Linear Layout inside Scroll view.

Categories

Resources