0

I would like to have a scrollable Table Layout in Android. For that I use the following XML layout file:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".Softdrinks">

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">



        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar_mainActivity"
            android:layout_width="432dp"
            android:layout_height="135dp"
            android:background="#435cb53f"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleTextColor="@android:color/holo_green_light">

            <TextView
                android:id="@+id/textView_ToolBar_CocktailSelectionActivity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                android:text="Softdrinks" />
        </androidx.appcompat.widget.Toolbar>

        <ScrollView
            app:layout_constraintTop_toBottomOf="@id/toolbar_mainActivity"
            app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_width="0dp"
            android:layout_height="0dp">
            <TableLayout
                android:id="@+id/tableLayout1"
                android:name="tableLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="0,1">



            </TableLayout>
        </ScrollView>





        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            app:labelVisibilityMode="labeled"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorGreen"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/bottom_navigation"
            app:itemIconTint="@color/colorPrimaryDark"
            app:itemTextColor="@color/colorAccent"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

And I have the following method for creating the table and adding ImageButtons into it within a Fragment.

 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        /**
         * Create TableLayout and add ImageButtons into them
         */

        TableLayout table = binding.tableLayout1;
        int numberOfColumns = 2;
        int numberOfRows = 2;
        for (int i = 0; i < numberOfRows; i++) {

            TableRow tr = new TableRow(getContext());
            tr.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

            for (int j = 0; j < numberOfColumns; j++) {

                ImageButton button1 = new ImageButton(getContext());
                button1.setImageResource(R.drawable.test_dish_1);
                button1.setBackgroundColor(Color.TRANSPARENT);
                tr.addView(button1);
            }
            table.addView(tr);
        }

    }

Here is the resulting screenshot: As you can see, the ImageButtons not fitting the screen and there is no margin between them horizontally. I do not want to have this. I played around with the layoutParams in the line:

tr.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

and used "Match_Parents" and "FILL_PARENT" and any combinations of them. Still the result does not change. Would you mind helping me on that issue? I'd really appreciate every comment and will be quite thankful for your help.

enter image description here

VanessaF
  • 515
  • 11
  • 36
  • would you like to set table row size, a part of the the width or height of the screen? for example half or one third of the screen? – justArandomUser Aug 29 '20 at 09:08
  • Thanks RandomUser for your comment. Basically I would like to have two columns always in each row. The number of rows can be arbitrarily high but I use a ScrollLayout to scroll vertically. So there should be two ImageButtons in one row. They should fit to the LayoutSize and there should be a small margin between them. – VanessaF Aug 29 '20 at 09:17
  • `TableLayout` is used very seldomly in Android. You would be much better off using a `RecyclerView` in this case. – Gavin Wright Aug 29 '20 at 09:49
  • Thanks Gavin for your comment. How can I use a RecyclerView in this case? Does a RecyclerView not increase the complexity and difficulty tremendously having to implement an Adapter and a ViewHolder? – VanessaF Aug 29 '20 at 09:55
  • @Gavin:Further I can imagine that embedding the RecyclerView into a Layout is way more difficult than embedding a TableLayout into a given Layout (which is already causing problems to me) – VanessaF Aug 29 '20 at 10:01
  • That's what I thought when I was first starting out. You'll actually end up doing a lot more work trying to do everything manually. Populating all those `Views` with data, adding `OnClickListeners`, keeping references to all the items you're adding manually. Not to mention the `Views` won't be recycled as the user scrolls. [RecyclerView does all this for you.](https://stackoverflow.com/questions/40587168/simple-android-grid-example-using-recyclerview-with-gridlayoutmanager-like-the) You just define a layout for a single item and tell it you want a grid with 2 columns, and you're good. – Gavin Wright Aug 29 '20 at 10:06
  • I linked one example above, but here's another simple `RecyclerView` with `GridLayout` example to get you started: https://www.tutorialspoint.com/simple-android-grid-example-using-recyclerview-with-gridlayoutmanager – Gavin Wright Aug 29 '20 at 10:12
  • Thanks Gavin for your comment and the link. I do not understand the advantages of RecyclerView that you mentioned (or I do not see them honestly). Adding on ClickListeners is just one line of code for ImageButtoms and TableViews. What do you mean by 'Populating all those Views with data' and 'keeping references to all the items you're adding manually'? Also adding an Image is just one line of code for ImageButtons. – VanessaF Aug 29 '20 at 10:18
  • @Gavin: I once tried to make a RecyclerView and I was surpriesed how ridiciously complex it is and I have not yet seen the advantages of it but many disadvantages (especially the hight complexity and the many lines of codes). – VanessaF Aug 29 '20 at 10:20
  • Fetching data and displaying it in a `RecyclerView` is basically the single most important skill for an Android developer to have. I'd suggest giving it another shot. – Gavin Wright Aug 29 '20 at 10:39
  • If you are fetching data and populating those records, use recyclerView with Paging library. It will reduce complexity and improve performance. – Sourav Bagchi Aug 29 '20 at 11:16
  • And what is the need of FrameLayout in your layout file? – Sourav Bagchi Aug 29 '20 at 11:18
  • @GavinWright: Thanks for your answers and effot. I think I do not have to fetch any data. I just have some ImageButtons and depending on the context they have different Images on them. Why is it so important for Android developers to use the Recylcler View. As far as I know there are good alternatives with less complexity – VanessaF Aug 29 '20 at 14:34
  • Thanks Sourav for your answer. What do you mean by recylcler View with Paging Library? If it reduces the complexity (opposed to the 'conventional' RecyclerView which increases the complexity) I would like to use it. – VanessaF Aug 29 '20 at 14:35
  • @SouravBagchi: I use the one Activity multiple Fragments approach. This is why I define the FrameLayout for the Fragment. – VanessaF Aug 29 '20 at 14:38
  • 1
    The complexity isn't really a problem. It's a pattern that you learn once and then re-use. Basically any app where you see a scrolling list is using a `RecyclerView`. The alternative is a `ListView`, which is slightly less complicated but less efficient and less versatile. – Gavin Wright Aug 29 '20 at 14:39
  • You can use ConstraintLayout as parent layout. There is no need for using FrameLaout for Fragments. – Sourav Bagchi Aug 29 '20 at 16:12
  • And @GavinWright is right. Currently, there is no better alternative for RecyclerView in Android. You can do a lot of things with RecyclerView. And Paging library is used for on-demand feeding data to RecyclerView. https://developer.android.com/topic/libraries/architecture/paging – Sourav Bagchi Aug 29 '20 at 16:15
  • Suppose you have a long list. But the mobile screen is small. So it can show a few rows at a time. So drawing the entire list is a waste of resources. But when you are scrolling down the RecyclerView, it recycles the top viewholder and sends to the bottom. – Sourav Bagchi Aug 29 '20 at 16:24

0 Answers0