Yik Jin Yik Jin - 4 months ago 53
Android Question

Make LinearLayout scrollable and preserve title bar

I am new to Android development. I would like to make

LinearLayout
scrollable but after implementing the accepted solution here, the default dark blue
ActionBar
at the top of my app disappeared. I am using
Empty Activity
.

Below is my
activity_main.xml
:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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>


There were no errors when I used this code.

Any help would be appreciated!

Answer

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.