Thursday, September 27, 2012

Horizontal Scroll View in Android | Horizontalscrollview example in Android | ScrollView Demo in Android

Hello Friends,
It's a simple demo for Horizontal Scroll View in Android.

1-Print Screen:


2-HorizontalScrollView.java


package com.example.horizontalscrollview;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
}

3-activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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" />

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

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

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

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

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button10" />
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

Thank you so much!

28 comments:

  1. how to do it with the ScrollView?
    1-ImageView(not move from screen)
    2-Button (move up from screen)
    3-world (move with the Button)
    4-ImageView(move with the Botton)

    1-ImageView(not move from screen)
    2-Button (move up from screen)
    3-world (move with the Button)
    4-ImageView(move with the Botton)
    like this one by one

    ReplyDelete
    Replies
    1. Means you want vertical scroll view? with below requirements-

      1-ImageView(not move from screen)
      2-Button (move up from screen)
      3-world (move with the Button)
      4-ImageView(move with the Botton)

      Delete
    2. follow below link-
      http://www.androidhub4you.com/2013/05/simple-scroll-view-example-in-android.html

      Delete
  2. is it possible to rotate it in circular order.

    ReplyDelete
    Replies
    1. circular means?
      Try in your layout android:rotaion="270"

      Delete
  3. how can we add animations to it while horizontal scrolling.

    ReplyDelete
    Replies
    1. apply some effect on touch gesture if it is needed..

      Delete
  4. i have problem whit this i cant run that.
    error:This HorizontalScrollView layout or its LinearLayout parent is useless.
    whats problem

    ReplyDelete
    Replies
    1. That was not error warning only just ignore it...
      And code should work as you can see screen shot are there..

      Thanks,

      Delete
  5. hello, do you know how can I use an Horizontal scroll view , but filled by a simple adapter?(like a grid view)
    Any ideas?
    By the way, what a nice job you have done with this blog and the tutorials!

    ReplyDelete
    Replies
    1. Thanks & your welcome!
      and for gridview inside Horizontal scroll you should use dynamic layout. it will help you.

      Thanks!

      Delete
  6. hi

    Instead of ScrollView i want to use SimpleGesture and onFling() method.
    So that based on the velocity or speed of swipe i can scroll the buttons.
    Can you tell me how can we do this?

    Thanks

    ReplyDelete
    Replies
    1. Please use swipe gesture then...
      Try this link-
      http://www.androidhub4you.com/2012/07/dynamic-swipe-view-demo-in-android.html

      Delete
  7. i am trying for text to scrol... how can i get tat ? could you help me ??

    ReplyDelete
    Replies
    1. I think it is same just create textview instead of button. or any other issue?

      Delete
  8. Hie Manish
    I want to know how use viewflipper inside scrollview?
    i did it but when i use scrollview with that at that time image can not flip right to left or vise versa.

    any solution???
    please let me now.

    ReplyDelete
    Replies
    1. Hi Dhaval, I don't think it will work because view-flipper is touch gesture and scroll-view and list-view. so you can't put two touch gesture on same place.

      Delete
  9. Such a nice job man !! Appreciate you !! :-)

    ReplyDelete
  10. Can I replace button with list view? Will it work the same? I want list view with horizontal scrolling.

    ReplyDelete
  11. Hi ! How can i set space between two views in horizontalscrollview?

    ReplyDelete
    Replies
    1. Just add some space into your button-

      android:layout_width="wrap_content"
      android:layout_marginLeft="10dip"
      android:layout_marginRight="10dip"
      android:layout_height="wrap_content"
      android:text="Button1"

      Delete
  12. horizontalscrollview works on Samsung Galaxy S3 neo but doesn't on htc neither huawei. I don't understand why.Can you help me please?

    ReplyDelete
  13. can you please tell me how to implement multiple pages in a single layout under fragment??

    ReplyDelete