Android ScrollView Example

Scrollview is one of important view class in android that always you to scroll the content of the layout ,when the content of the layout goes bigger than the physical device size  

In fact the scrollview is a special kind of framelayout that contains a single child view , and the childview that is used in it is a linear layout in  vertical orientation which can be scrollable is vertical direction 

Lets See An Example
Project Detail
Project Name SampleScrollView
Package com.tutorialsbuzz.samplescrollview
Minimum SDK API 15
Target SDK API 26
Theme Theme.AppCompat.Light.DarkActionBar

1. XML Layout


  file:-activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView"
    tools:context=".MainActivity">

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

        <include layout="@layout/sample" />
        <include layout="@layout/sample" />
        .....
  ...
  ..

    </LinearLayout>

</ScrollView>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:layout_marginLeft="16dp"
        android:text="@string/scroll_text"
        android:textSize="28sp" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="4dp"
        android:layout_marginTop="20dp"
        android:background="#cccccc" />
</LinearLayout>

2. Activity


  file:-MainActivity.java
package com.tutorialsbuzz.samplescrollview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

    }
}


3. RUN



No comments:

Post a comment