Search code examples
androidandroid-appbarlayout

appBarLayout is closed immedaitely when starting the activity


I am new in android. one of my activites has an appbarLayout, the problem is when starting that activity the app barLayout closed immediately and I have to scroll down to open it again. I also set app:expaned to true, but it doesnt't work for me. my code:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mygray">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/appbar_h"
    app:expanded="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
        android:foregroundGravity="bottom|right"
        android:foregroundTintMode="add"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:expandedTitleTextAppearance="@style/CustomToolbar">


        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/graph"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar"
            android:background="@color/lime300"
            app:layout_collapseMode="parallax"></com.github.mikephil.charting.charts.LineChart>

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:elevation="@dimen/elevation"
            app:layout_collapseMode="pin"
            app:theme="@style/Toolbar">
        </android.support.v7.widget.Toolbar>

        <TextView
            android:id="@+id/Mn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="70dp"
            android:rotation="-90"
            android:text="بار محوری (KN)"
            android:textSize="@dimen/textsize_checkBox"
            android:textColor="@color/gray_heavy"/>
        <TextView
            android:id="@+id/Pn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin4"
            android:layout_marginStart="@dimen/margin4"
            android:layout_gravity="bottom"
            android:layout_marginBottom="@dimen/margin_columnAnalysis"
            android:text="لنگر خمشی (KN.m)"
            android:textSize="@dimen/textsize_checkBox"
            android:textColor="@color/gray_heavy"/>


    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<include layout="@layout/nested_scroll_view"/>

 </android.support.design.widget.CoordinatorLayout>

when start the activity

after scroll sown the appBarLyout


Solution

  • I have the same Problem. The problem is that the stuff under the AppBarLayout (in my App the Fragment) pushes the AppBarLayout up and closes it because its content fills more than the visible space. But I found no solution to prevent this...

    When I hide the content of my Fragment the AppbarLayout stayed open.

    android:visibility="invisible"
    

    UPDATE:

    I found the solution:

    One element in your View is getting focus (probably EditText).

    To prevent the Collapsing in my app I had to put this statement in the XML in the EditText

    android:focusable="false"
    

    If you want to use the EditText, use in onCreate:

    EditText text = findViewById(R.id.textEdit);
    text.setFocusable(true);
    

    UPDATE 2:

    additionally maybe you need to get the editability back:

    text.setFocusableInTouchMode(true);