How to Make a Linearlayout Scrollable

How do I make a LinearLayout scrollable?

Place your layout in a ScrollView.

How do you make a LinearLayout scrollable?

Wrap the linear layout with a <ScrollView>

See here for an example:

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

Note: fill_parent is deprecated and renamed to match_parent in API Level 8
and higher.

Is it possible to make a linear layout scrollable?

You should wrap your LinearLayout in a ScrollView:

Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

How to make a linear layout scrollable?

A LinearLayout is a subclass of View, so anything you can do with a View you can with a Linear Layout.

So just use ScrollView with a single LinearLayout as a child

making a linear layout horizontally scrollable

check this link http://android.okhelp.cz/horizontalscrollview-scrollview-horizontal-vertical-android-xml-example/

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class HorizontalscroolActivity extends Activity {

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

ImageButton btn11 = (ImageButton) findViewById(R.id.imageButton1);

ImageButton btn2 = (ImageButton) findViewById(R.id.imageButton2);

ImageButton btn3 = (ImageButton) findViewById(R.id.imageButton3);

ImageButton btn4 = (ImageButton) findViewById(R.id.imageButton4);

}

}

In XMLFILE

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="@drawable/pic7"
android:text="image1" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Textview1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ImageButton
android:text="image2"
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="@drawable/pic2" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Textview2" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ImageButton
android:text="image3"
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="@drawable/pic7" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Textview3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ImageButton
android:text="image4"
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="@drawable/pic5" />
<TextView
android:id="@+id/text4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Textview4" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>

How to make only part of the layout scrollable with ScrollView and Linear Layout?

Have a look at how you can do it... Forget about FrameLayout, use the Linear as the main Layout

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

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/avli_horse"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/bar"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/kitchen"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/avli_logo"/>

</GridLayout>

</HorizontalScrollView>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:layout_weight="3">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/avli_horse" />

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/bar" />

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/kitchen" />

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/avli_logo" />

</LinearLayout>

</ScrollView>

</LinearLayout>

How to make middle LinearLayout scrollable

I made only small change in your code just paste it and try

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:weightSum="1">

<LinearLayout
android:id="@+id/profileBlock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageViewPatientPicture"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="0"
android:scaleType="fitXY"
app:srcCompat="@drawable/profile_pic" />

<TextView
android:id="@+id/textViewPatientName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:text="Anakin Skywalker" />

</LinearLayout>

<ScrollView
android:id="@+id/infoBlock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/graphInfoBlock"
android:layout_below="@id/profileBlock"
android:layout_marginLeft="10dp"
android:fillViewport="true"
android:orientation="vertical"
android:layout_weight="1">

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

<TextView
android:id="@+id/textViewDOB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Date of Birth: 26/08/1963" />

<TextView
android:id="@+id/textViewGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="5dp"
android:text="Gender: Male" />

<TextView
android:id="@+id/textViewMedication"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="5dp"
android:text="Current Medication: - " />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:paddingTop="5dp">

<TextView
android:id="@+id/textViewObservation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Observations:" />

<TextView
android:id="@+id/textViewObservatonsInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mollis nulla eu scelerisque ultricies. Etiam varius iaculis purus, et ullamcorper lectus tincidunt et. Nullam vel felis eu dolor condimentum dignissim. Vestibulum in tristique nulla. Mauris elit nibh, ullamcorper in imperdiet sed, luctus vitae nibh. Aliquam nisi nisi, mattis vitae cursus id, dignissim sollicitudin risus. Nam sed egestas mauris, sed scelerisque quam. Morbi sapien turpis, vestibulum at lacus vel, lacinia vehicula elit. Maecenas et lobortis felis. Vivamus elementum ut tortor efficitur iaculis. Vestibulum vestibulum dolor ut urna facilisis eleifend. Nulla gravida mattis eros sit amet molestie. Cras pellentesque sodales tortor, id efficitur neque ultrices at. Pellentesque lacus quam, euismod a dui et, ullamcorper luctus magna. Mauris semper sem vel neque hendrerit, hendrerit elementum nibh sodales." />
</LinearLayout>

</LinearLayout>
</ScrollView>

<LinearLayout
android:id="@+id/graphInfoBlock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">

<com.jjoe64.graphview.GraphView
android:id="@+id/graphPressure"
android:layout_width="275dp"
android:layout_height="275dp" />

<ListView
android:id="@+id/listViewGraphValues"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

</LinearLayout>

Make LinearLayout scrollable and preserve title bar

I had finally figured out my problem. The reason why Actionbar is missing is because ScrollView was placed outside LinearLayout. To fix this, use another LinearLayout to wrap the ScrollView, as shown below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context="com.example.android.MainActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<!-- Blah blah blah -->

</LinearLayout>
</ScrollView>
</LinearLayout>

Thanks to everyone, especially @arvicx, for their time! :)

I hope everyone who had this problem find this useful.

Make Linearlayout scrollable without using Scrollview

Actually after doing some research, I come up with a solution for this problem:

At first I want to explain the problem in a very simple way.

  1. LinearLayout will be scrollable. To do this we can use ScrollView but sometimes we need to use ListView inside LinearLayout.
  2. We know that inside ScrollView we cannot use another scrollview like ListView

How to solve that?

ListView is scrollable inherently so we can add header and footer in the ListView. As a conclusion:

  1. Create Layout header.xml and footer.xml and list.xml
  2. Find the ListView reference from list.xml in the main activity and dynamically add header and footer in the ListView reference.


Related Topics



Leave a reply



Submit