0

enter image description hereI am using a horizontalscrollview with buttons. Now i want that only 3 buttons should be displayed at a time in each type of mobile screen . How is it possibble? i am using following code.

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp" >

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button4" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button5" />

    </LinearLayout>
</HorizontalScrollView>
M Martin
  • 68
  • 1
  • 6
  • `each type of mobile screen` means different screen size mobile ? is it – Sree Dec 23 '15 at 08:21
  • Means to say that always 3 buttons should be displayed before scrolling. – M Martin Dec 23 '15 at 08:25
  • you mean 3 button on the bottom/top of the page and scroll on background is it ? – Sree Dec 23 '15 at 08:26
  • http://stackoverflow.com/questions/9326299/android-scrollview-and-buttons-at-bottom-of-the-screen – Sree Dec 23 '15 at 08:28
  • can you post the screenshot that you have tried so far. and the screenshot you want – Linh Dec 23 '15 at 08:33
  • I want above output.but i am getting 7 number of button in a row – M Martin Dec 23 '15 at 08:35
  • I once wanted to achieve what you're trying to do now and I ended up finding out that the best way to do it is by using [`ViewPager`](http://developer.android.com/training/animation/screen-slide.html)... With this kind of view you can fit the screen with and limit the inner views to the amount needed and always view that amount in any eventual pager that you may have... – n1nsa1d00 Dec 23 '15 at 08:49

3 Answers3

0

its not possible in xml layout because you haven't the width of scroll view BUT... you can get device width pixels by following code and then divide it to 3:

DisplayMetrics display;
display = this.getResources().getDisplayMetrics();
int width = display.widthPixels; 

when you get the device width pixels devide it to 3 and set to buttons:

btn.setWidth (width / 3);

Hope its helpful ..

Mahdi Astanei
  • 1,905
  • 1
  • 17
  • 30
0

I think you are looking for something like this.

You can give the buttons a size of 1/3th of the screen (with an layout weight) and i think the rest of the buttons than are "out of the screen" so you can make them visible by scrolling. I can't tell you how the code exacly looks like becouse i'm not an android developer but i did it once and it was something like this.

I hope it helps you to find the exact solution!

CodeNinja
  • 836
  • 1
  • 15
  • 38
0

It is difficult because HorizontalScrollView will not let you do so.

if you take HorizontalScrollView and then give this buttons a weightSum , it wont be applied.

so i would say , it is dependent on screensize. if you fix it for one than it will differ for other.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142