0

I want to display a horizontal list of scrollable buttons at top of the phone screen containing 30 items, for this purpose I am using a HorizontalScrollView with a LinearLayout with "horizontal" orientation as it's child but the linear layout is not taking up the entire phone width even on setting its width as "match-parent". Here's the code :

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_gravity="center">
        <GridView
            android:id="@+id/gridView_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:stretchMode="columnWidth" />
    </LinearLayout>
</HorizontalScrollView>

enter image description here

Here is shown in the image that linear layout is not taking up the entire space and LinearLayout is only covering some of the space. Also on changing the size to a fixed size, I noticed that the HorizontalScrollView was actually behaving like a vertical Scroll View only.

NOTE: Also if there is an alternative way to display a horizontal list of buttons with 30 items with numbers from 1 to 30 on it, please suggest it.

Sudhanshu Vohra
  • 1,345
  • 2
  • 14
  • 21
  • You cannot put a GridView inside a horizontal scroll view. And you can create a recycler view with horizontal scroll direction. mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true)); – mudit_sen Dec 28 '17 at 15:54

2 Answers2

1

To achive this easily you may use Recyclerview with Horizotanl Layout Manager.For Example

recycler_view.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
Munir
  • 2,548
  • 1
  • 11
  • 20
  • I was thinking the same to use a RecyclerView but since my requirement was simple a horizontal list of fixed items I thought using a RecylerView would not be a good option. Still, I will consider this and work this way too. – Sudhanshu Vohra Dec 28 '17 at 15:55
  • instaed of using gridview and horizontal Scrollview or putting static 30 button is not efficient way. you may do it with less code and effiecient way by using of recylerview – Munir Dec 28 '17 at 15:59
  • Okay, I will consider doing it with a RecyclerView instead. – Sudhanshu Vohra Dec 28 '17 at 16:01
  • @SudhanshuVohra if the answer will helped you.. you may also accept it. – Munir Dec 28 '17 at 16:07
  • I will try it and if this works I will surely accept it. My problem is not to achieve the end result only but to also find out why is it not working with HorizontalScrollView. If you could help me with that then please do so. – Sudhanshu Vohra Dec 28 '17 at 16:11
0

I don't think you need a HorizontalScrollView with a nested LinearLayout for what you are trying to achieve. It would be a better idea to simply use a horizontal RecyclerView if your button layouts are similar and the functionality of the buttons is similar too. Refer to this answer for help https://stackoverflow.com/a/40584425/9119277

Napster
  • 1,353
  • 1
  • 9
  • 11