Skip to content
Advertisement

Scaling an ImageView when the soft keyboard opens

I’m creating a tabbed activity. As long as the first tab is selected, the soft keyboard should be visible. I achieved that by adding this line to my manifest file in the activity tag:

android:windowSoftInputMode="stateVisible|adjustResize"

As the keyboard opens, the space for the layout shrinks. The most space is occupated by an ImageView. I want it to shrink with the layout size to allow the other 2 views (which should remain the same size) to fit on the screen. However, although the soft input mode is set to adjustResize, the ImageView keeps its size after the keyboard opens. Here’s a comparison of the current layout and the one I want to achieve (the ImageView is the red rectangle): comparison

My fragment’s layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".DataInputFragment"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitStart"
        android:src="@drawable/image"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/input_label_text"/>

        <namespace.InputEditText
            android:id="@+id/input_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            <requestFocus/>
        </namespace.InputEditText>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/submit"
    </LinearLayout>
</RelativeLayout>

My activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TabLayout">

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_1" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_text_2" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>

How to force the ImageView to resize according to the screen size to make all of the views fit on the screen?

[EDIT]: My attempt to create a ConstraintLayout, which didn’t solve the problem (the ImageView still keeps its original size):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".DataInputFragment">

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitStart"
        android:src="@drawable/image"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/input_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/input_label_text"
        app:layout_constraintTop_toBottomOf="@id/img"/>

    <namespace.InputEditText
        android:id="@+id/input_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/input_label">
        <requestFocus />
    </namespace.InputEditText>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/submit"
        app:layout_constraintTop_toBottomOf="@id/input_edit_text"/>
</android.support.constraint.ConstraintLayout>

Advertisement

Answer

I’ve created an Android Project from scratch on Android Studio 4.0, using the “Tabbed Activity” template and uploaded it to GitHub.

I’ve had no issues with the resize taking place when the activity is resized.

What Did I change?

  1. In the manifest.xml I added android:windowSoftInputMode="adjustResize"
  2. I modified the default layout for the fragment in the tab to include an ImageView (called imageToShrink) and an EditText (called editLayout).
  3. I left the existing TextView untouched, except that since it’s at the top, I made it the HEAD of the vertical Chain, and gave it a 0.0 BIAS to be aligned at the top, rather at the center.

When you tap on the cyan EditText at the bottom, the keyboard pops (I made it number only so it’s even taller) and you can see how the image is re-drawn after its new size is recomputed.

This is how it looks when it opens: (beautiful color palette!)

image before the keyboard

And this is how it looks when I tap the “edit field” to pop the keyboard:

image after the keyboard is open

Advertisement