26

I have a simple activity with a text view followed by two edit texts. When I start typing, the soft keyboard pushes layout up and hides the action bar, and I won't be able to select/copy/paste text.

My activity looks like this:

Normal activity view

When I start typing, the action bar gets hidden, like this - and more importantly, note that the normal copy paste bar is missing even though a text is highlighted:

Missing action bar and the copy/paste bar

What can I do to make sure the action bar does not hide?

  • I have tried some other tips from SO (such as using scroll view) but none worked.
  • The app is created using Android Studio 1.4.1 wizard with the Drawer Activity.
  • In my activity manifest I am using adjustPan. I do not want to use adjustResize.

    android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

My activity is here:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/navigation_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/app_theme.app_bar_overlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/app_theme.popup_overlay" />

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

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/activity_margin"
            android:fillViewport="true"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:background="@android:color/darker_gray"
                android:text="My holiday story here." />

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/activity_margin" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/gap_normal"
                android:gravity="top"
                android:text="My Holiday in Europe" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:gravity="top"
                android:text="My holiday story is blah blah blah blah blah blah blah blah blah..." />

        </LinearLayout>


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

    <com.magicparcel.app.mysampleapp.ui.CustomNavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/navigation_drawer_header"
        app:itemIconTint="@color/app_grey_500"
        app:menu="@menu/drawer_main" />

</android.support.v4.widget.DrawerLayout>
Ayan
  • 1,952
  • 2
  • 21
  • 58
henrykodev
  • 2,234
  • 2
  • 22
  • 34

3 Answers3

6

Try by adding app:layout_collapseMode="pin" in toolbar

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/app_theme.popup_overlay" />

and for enable copy/paste in editText Set android:textIsSelectable="true" look more on https://developer.android.com/guide/topics/text/copy-paste.html

Uttam Panchasara
  • 5,010
  • 4
  • 23
  • 38
  • Hi, Just for some clarification. What we should do in case of any other View, which located above EditText fields. For example. we have several layouts above the EditText and we want to use adjustPan. In this case, while we selecting EditText, all views above moving up, and some of them (like ActionBar) hides. What I should do, to save some layouts above EditText in selecting, and let other moving up? – GensaGames Sep 21 '16 at 08:14
  • I didn't get you what you mean by this **What I should do, to save some layouts above EditText in selecting, and let other moving up?** – Uttam Panchasara Sep 21 '16 at 08:34
2

Step-1 : Remove fitsSystemWindow attribute from each view and add it in the activity tag in the manifest.

Step-2 : app:layout_behavior="@string/appbar_scrolling_view_behavior" Remove this line from your linear layout. This is the code which is actually pushing the actionbar above.

These two steps solved my problem, Hope it helps you too.

Mohammed Atif
  • 3,892
  • 6
  • 24
  • 49
0

Add you Toolbar inside AppBarLayout and add AppBarOverLay and PopOverlay themes.

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

styles.xml

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Naveen Kumar M
  • 6,868
  • 6
  • 55
  • 66