1

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!

Community
  • 1
  • 1
  • What is this 'Empty Activity'? And do you mean actionbar is not visible? – Harin Jan 23 '16 at 13:17
  • `Empty Activity` is the activity I chose to use when I first start a new project in Android Studio. Basically just a pre-coded action bar (yes I think that's it, will update post now) for you. By action bar not visible please refer to my screenshot above. –  Jan 23 '16 at 13:44
  • Post your style.xml here - which was generated by default. – Harin Jan 25 '16 at 09:20

4 Answers4

2

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.

0

Open your styles.xml file under res/values/styles.xml and make sure that the AppTheme parent is Theme.AppCompat.Light.DarkActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

and one thing, LinearLayout's attribute

android:layout_height="match_parent"

should be

android:layout_height="wrap_content"

Hope it helps... Tell me if it works.

arvicxyz
  • 381
  • 3
  • 13
  • can you post a screenshot of the screen you have? and if you are referring to the title bar as actionbar/toolbar? right? – arvicxyz Jan 23 '16 at 13:09
  • Yes and I had just posted the screenshot above –  Jan 23 '16 at 13:21
  • I'm sorry, both links can't be viewed. and I don't know why. Maybe country restrictions... – arvicxyz Jan 23 '16 at 13:32
  • Try again now, I've set security to the minimum. Should be working :) –  Jan 23 '16 at 13:33
  • still nothing bro... webpage is not available... If my answer is still not working. you can try to change the MainActivity to extend Activity instead of AppCompatActivity but not a good solution and just a workaround, there are also only certain versions of Android that shows the ActionBar. The best one is to implement your own toolbar as what @FranMowinckel said. – arvicxyz Jan 23 '16 at 13:46
  • Ok, thanks for your time though. Link is `yikjin.ga/Capture.PNG`, if it is not working means its a country restriction. I will try and implement my own toolbar –  Jan 23 '16 at 13:50
  • 1
    Good thing. Because that's the standard way to do it now... Goodluck and have fun bro. :) – arvicxyz Jan 23 '16 at 13:51
0

You should use the Toolbar support class instead as described in Setting Up the App Bar. The reason is (as described in the link) the ActionBar behaves differently depending on what version of Android is used.

Apart from that, @arvicx is correct, the layout height of the LinearLayout should be android:layout_height="wrap_content" because it doesn't make sense to have a scrollable element the same size as its container.

And you also might want to add android:fillViewport="true" for the reasons described here

FranMowinckel
  • 4,233
  • 1
  • 30
  • 26
0

Buddy,

Use LinearLayout vertically to put your title bar and scrollview respectively.

stdout
  • 2,471
  • 2
  • 31
  • 40
  • I assume it means in `LinearLayout` I have to use `android:orientation="vertical"`? If that's so, I've already done it in the code above. –  Jan 23 '16 at 13:40
  • You need to move your titlebar out of scrollview. And packed them together into another LinearLayout which had its orientation set to vertical. – stdout Jan 24 '16 at 14:05