0

I want to place a simple, single, grid at the bottom of my screen. Here is a screen shot of the desired result

simple grid I am looking for

simple grid I am looking

It would be cool if I could make the "Window" that shows the matrix only 3 rows high and allow the user to scroll/fling through it. With this, when the screen is rotated, the list/matrix would not consume the whole screen in horizontal mode.

I suspect I should use table layout? Below is my activity_main.xml. I want to replace the bottom-most / 3rd textview with a table layout that contains text in 3 columns with multiple rows.

Obviously I can drag/drop the widget but how do I populate the rows and columns?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context="com.example.ftonyan.gps7.MainActivity">

    <TextView
        android:text="abc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textAlignment="center" />

    <Chronometer
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/def"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:gravity="bottom"
        android:text="Log:"
        android:maxLines="8"
        android:layout_marginTop="31dp"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:text="ghi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="19dp"
        android:id="@+id/textView3"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
Frank Zappa
  • 451
  • 2
  • 11
  • 23

1 Answers1

3

You can take a look at this. So in your case, you can add the following at the end:

<TableLayout
    android:id="table"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"/>

In your code, you can then get the reference of the TableLayout, then inflate the row with three columns then add the rows to your TableLayout:

TableLayout tableLayout = (TableLayout) findViewById(R.id.table);

//TODO: inflate some rows

tableLayout.addView(row1);
tableLayout.addView(row2);

etc...

Your inflated row layout would look something like the following:

<TableRow>
        <TextView
            android:id="@+id/first"
            android:text="first"
            android:padding="3dip" />
        <TextView
            android:id="@+id/second"
            android:text="second"
            android:gravity="center"
            android:padding="3dip" />
        <TextView
            android:id="@+id/third"
            android:text="third"
            android:gravity="right"
            android:padding="3dip" />
</TableRow>

You can give each textview an id, so that when you inflate each row, you can reference the textview to set your data.

I think another important thing to note is that, depending on your data or data size, you can consider using a RecyclerView with a GridLayoutManager to more efficiently achieve what you're trying to do.

D Ta
  • 864
  • 6
  • 17
  • Thank you very much. My data will be no more than 20 rows deep and 4 columns wide. It's not huge but a grid with lines will make it easier to read. I want it to fit on the bottom of my screen while I keep the textviews already in my app...on the top. Should I use tableLayout or recyclerView? Since I have not used either they will both be new to me. – Frank Zappa Feb 20 '17 at 22:43
  • For the sake of not having to write repeated code, I think that the RecyclerView might be the better option. There's quite a few tutorials online on how to get started with it. But in a nutshell, you're feeding data into an adapter that will map the data with the view it's supposed to show. If you find the RecyclerView too complicated, you can take a look at GridView as well. However, it'd be good practice to try to use the RecyclerView since it's more efficient (ViewHolder pattern). – D Ta Feb 20 '17 at 22:55
  • You can take a look at this answer: http://stackoverflow.com/questions/40587168/simple-android-grid-example-using-recyclerview-with-gridlayoutmanager-like-the – D Ta Feb 20 '17 at 22:55
  • thanks again for your help. I implemented the simple RecyclerView tutorial with the red blocks. It worked! I reduced the number of columns to 3 and adjusted the background color. I would like to modify it so the column width is automatically set based on the text I load in. In my original post above I added an image of the simple format I am looking for. I appreciate any help. The text is an array of objects (list) today. I would just like to assign the test in each object row to the recyclerview. thanks – Frank Zappa Feb 23 '17 at 02:17
  • So instead of the GridLayoutManager that I suggested earlier, I think it'll be better if you use a LinearLayoutManager instead with your RecyclerView. You can define a layout with three textviews positioned horizontally. Assuming that your model is time/instructions (you can use the position to define your "item" column), for each of your rows, just set the textviews accordingly with the list of your model that you're passing into the adapter. let me know if you need further clarification. – D Ta Feb 23 '17 at 18:45
  • Thanks I will try this. By the way. Any idea why I am getting this warning with just simple text views? http://stackoverflow.com/questions/42419302/android-w-displaylistcanvas-displaylistcanvas-is-started-on-unbinded-rendernode – Frank Zappa Feb 24 '17 at 02:36