Home How to make Dynamic one row 7 columns that has horizontal Scroll
Reply: 1

How to make Dynamic one row 7 columns that has horizontal Scroll

Scott Cooper
1#
Scott Cooper Published in 2017-11-15 00:35:27Z

So i have been working on my android project and the layout has gotten me and I cannot figure out what is the best to do something like this. Currently my XML looks like this. What i am trying to do is have 7 columns with one row that is filled with textviews. I was looking at Grid views but it seems like the entire point of the grid is to scroll with more than one row. How do I achieve this layout?

 <android.support.constraint.ConstraintLayout
    android:id="@+id/ConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/foodmenuexample"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/textView9"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="TextView" />

                        <TextView
                            android:id="@+id/textView10"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="TextView" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/textView8"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="TextView" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/textView11"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:ellipsize="end"
                            android:maxLines="1"
                            android:text="123456789123456789123456789123456789123456789123456789123456789" />
                    </LinearLayout>

                </LinearLayout>
            </HorizontalScrollView>

            <TextView
                android:id="@+id/textView12"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>
DWndrer
2#
DWndrer Reply to 2017-11-15 02:22:41Z

The answer is... it depends. For simplicity, you can use a GridLayout, which if you have all your data within the view, this would work well. However if you want the data to load from a database, or if there is an indeterminate amount of columns, then a gridview is your best bet inside a horizontal scrollview. You can just set the number of rows to 1, and the number of columns programmatically.

Example of that:

GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setNumColumns(numOfColumns);

Gridviews are more complicated because you will have to use an adapter, which you can learn about here:

https://developer.android.com/guide/topics/ui/layout/gridview.html

However, if you are looking for static simplicity, here is what you could do:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    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">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:rowCount="1"
            android:columnCount="7">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="Lots of text on this one" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="Some text on this one" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="Textview3" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="Textview4" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="Textview5" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="Textview6" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="Textview7" />


        </GridLayout>
    </HorizontalScrollView>


</android.support.constraint.ConstraintLayout>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.354502 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO