Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soft Keyboard Hides Toolbar

I have used Toolbar in my layout. Whenever I open the activity, and focus on the edit-text, it hides the toolbar. Please see below pictures

Before Soft Keyboard opens up

After Soft Keyboard opens up

Below is my layout file:

<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"
android:orientation="vertical"
tools:context="com.example.sampleactivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    android:background="@color/color_theme"
    android:minHeight="?attr/actionBarSize">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="App"
        android:textColor="@color/white"
        android:textSize="24sp" />

    <Button
        android:id="@+id/past_locations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Next"
        android:textColor="@color/white"
        style="?android:attr/borderlessButtonStyle"/>

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

<LinearLayout
    android:id="@+id/mapLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>


    <EditText
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Title"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:singleLine="true"
        />

    <AutoCompleteTextView
        android:id="@+id/tags"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:maxLines="3"
        />

</LinearLayout>

Using android:windowSoftInputMode="adjustPan" didn't help either.

How can I avoid hiding my toolbar when keyboard opens up.

like image 878
satish123 Avatar asked Oct 25 '15 19:10

satish123


People also ask

How to Show and hide soft keyboard in Android?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

What is soft keyboard in Android?

The Android system shows an on-screen keyboard—known as a soft input method—when a text field in your UI receives focus.


2 Answers

Adding android:windowSoftInputMode="adjustResize" in AndroidManifest.xml solved my issue, like:

 <activity
        android:name=".MainActivity"
        android:label="@string/activity_main"
        android:windowSoftInputMode="adjustResize">
 </activity>
like image 127
satish123 Avatar answered Oct 01 '22 16:10

satish123


i suggest you create a different layout for toolbar and include it in your activity

Create toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    android:background="@color/color_theme"
    android:minHeight="?attr/actionBarSize">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="App"
        android:textColor="@color/white"
        android:textSize="24sp" />

    <Button
        android:id="@+id/past_locations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Next"
        android:textColor="@color/white"
        style="?android:attr/borderlessButtonStyle"/>

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

Your activity(assuming activity_main.xml)

<?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"
android:orientation="vertical"
tools:context="com.example.sampleactivity">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
</LinearLayout>

<LinearLayout
    android:id="@+id/mapLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>


    <EditText
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Title"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:singleLine="true"
        />

    <AutoCompleteTextView
        android:id="@+id/tags"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:maxLines="3"
        />

</LinearLayout>

try this if it works !!!

like image 40
Danish Ansari Avatar answered Oct 01 '22 15:10

Danish Ansari