Thursday, May 2, 2013

Simple Scroll-View example in Android | Vertical Scroll View Demo in Android | Scroll View in Android

Hello Friends, On a specific demand I am going to share very simple code for vertical scroll view in android. Hope it will help you-

1)MainActivity.java


package com.example.scrollviewdemo;

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

public class MainActivity extends Activity {

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

}

2)activity_main.xml


<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/imageView1" >

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

            <Button
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button7"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button8"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button9"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button10"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button11"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button12"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button13"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button14"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

Thanks,

37 comments :

  1. no iwint to do two scrollview in one activity

    ReplyDelete
    Replies
    1. Hi!
      If you want one scroll-view work in both orientation(horizontal+vertical) then it is very tuff.
      Therefore two way only-
      1)Divide screen in two part and use separate scroll view for both.
      2)Use custom web-view.

      Delete
    2. will you please tell me exactly what you want? On same page in detail?

      Delete
    3. Hey manish dear
      package in.wptrafficanalyzer.listviewwithimagesandtext;

      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;

      import android.app.Activity;
      import android.os.Bundle;
      import android.widget.ListView;
      import android.widget.SimpleAdapter;

      public class MainActivity extends Activity {

      // Array of strings storing country names
      String[] countries = new String[] {
      "India",
      "Pakistan",
      "Sri Lanka",
      "China",
      "Bangladesh",
      "Nepal",
      "Afghanistan",
      "North Korea",
      "South Korea",
      "Japan"
      };

      // Array of integers points to images stored in /res/drawable-ldpi/
      int[] flags = new int[]{
      R.drawable.india,
      R.drawable.pakistan,
      R.drawable.srilanka,
      R.drawable.china,
      R.drawable.bangladesh,
      R.drawable.nepal,
      R.drawable.afghanistan,
      R.drawable.nkorea,
      R.drawable.skorea,
      R.drawable.japan
      };

      // Array of strings to store currencies
      String[] currency = new String[]{
      "Indian Rupee",
      "Pakistani Rupee",
      "Sri Lankan Rupee",
      "Renminbi",
      "Bangladeshi Taka",
      "Nepalese Rupee",
      "Afghani",
      "North Korean Won",
      "South Korean Won",
      "Japanese Yen"
      };


      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      // Each row in the list stores country name, currency and flag
      List> aList = new ArrayList>();

      for(int i=0;i<10;i++){
      HashMap hm = new HashMap();
      hm.put("txt", "Country : " + countries[i]);
      hm.put("cur","Currency : " + currency[i]);
      hm.put("flag", Integer.toString(flags[i]) );
      aList.add(hm);
      }

      // Keys used in Hashmap
      String[] from = { "flag","txt","cur" };

      // Ids of views in listview_layout
      int[] to = { R.id.flag,R.id.txt,R.id.cur};

      // Instantiating an adapter to store each items
      // R.layout.listview_layout defines the layout of each item
      SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);

      // Getting a reference to listview of main.xml layout file
      ListView listView = ( ListView ) findViewById(R.id.listview);

      // Setting the adapter to the listView
      listView.setAdapter(adapter);
      }
      }
      how to put hyperlink/URl in above code so please tell me and replay on my email id amit.potent@gmail.com please.......

      Delete
  2. can i use scrollview in absolutelayout

    ReplyDelete
  3. Hey how is the scrollTo(x, y)method working? could you please provide me an example?

    ReplyDelete
    Replies
    1. What you mean alex? I did not got you, can you explain detail please?
      If you are looking for horizonatal scroll view you can try my this blog-

      http://www.androidhub4you.com/2012/09/horizontal-scroll-view-in-android.html

      Thanks,

      Delete
  4. Hello. Can you add the code for downloading a file in the app and showing its progress

    ReplyDelete
    Replies
    1. Sure I will upload soon! Please keep in touch..

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  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. Thank You.

    If i scroll fastly last contact or last button should be displayed.
    How can i do that?

    ReplyDelete
    Replies
    1. No idea dear..
      How can we deduct user scrolling fast? If it is possible then only get swipe speed and set condition like if speed>80 then lastPage :)
      Something like that dear. I am not sure..

      Delete
  8. hello sir, can you pls. help me on how to implement fisher-yates shuffling algorithm in android? i want to shuffle a set of questions using fisher-yates algorithm.

    ReplyDelete
    Replies
    1. in java here Random method available here for this task. if you want use your own alog please implement it yourself.

      Delete
  9. What is The Java Code For Scroll View.....? I want to scroll my Text Which is Long Enough. and I putted it below image View. Now If i want to scoll My Text Then what i should do ? And If i m takin only Text View then Text Is Not Coming Whol....

    ReplyDelete
    Replies
    1. Just put your textview inside scrollview below of your imageview-


      Delete
  10. Hi Manish,

    Really helpful for beginners ...can you please help in creating an app which has scroll view nd when we select an item in scroll it will go to its information texts...

    ReplyDelete
  11. using vertical orientation n divide screen in three parts , i have just scroll second part ,what can i do ?? i m a beginner ...

    ReplyDelete
  12. how i write text in scroll view

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. I try your code without Imageview but a message " This ScrollView layout or its RelativeLayout parent is useless" appear in xml file. And when i run the code it will suddenly stop...please tell me why?..i 'm beginner...thanks

    ReplyDelete
    Replies
    1. Did you declared your project in manifest file? please check or share your error log cat..

      Delete
  15. Hi, I have a scroll view with 1000 views in it, and it takes time to display on the screen. any suggestions for this issue?

    ReplyDelete