Background image gets shrink on keyboard popup

401 Views Asked by At

I have a parent layout with background image, so after adding scroll view when the keyboard pops up the background image gets shrink.

Like below images :

enter image description here

enter image description here

I checked all solutions on SO, adding windowSofInput as adjustResize and adjustPan but adjustResize dosent solve the problem and by using adjustPan cant scroll up the page, scroll view dosent work with adjust pan.

I tried adding some attributes of scroll view like fillViewPort, adjustViewBounds to true.

Also I tried the true With this scroll view dose not shrink but, I dont need the full screen, status bar cant be seen with full screen.

I tried to add the image view rather than setting the background image to the parent layout, and tried to set its scaleType, matrix scale type dosent shrink but my backgroung image dose not look like original image.

It looks like tihs :

enter image description here

enter image description here

layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.admin.SmartCabFuelCard.RegisterDriver">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="matrix"
        android:src="@drawable/page"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_color"
        android:alpha="0.5"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds = "true"
        android:isScrollContainer="true"
        android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context="com.example.admin.SmartCabFuelCard.RegisterDriver"><!--

  <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:src="@drawable/page"
      android:scaleType="fitXY"/>
-->


        <RelativeLayout
            android:id="@+id/rl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_marginEnd="@dimen/dimen_10dp"
            android:layout_marginStart="@dimen/dimen_10dp"
            android:layout_marginLeft="@dimen/dimen_10dp"
            android:layout_marginRight="@dimen/dimen_10dp">

            <ImageView
                android:id="@+id/logo"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/dimen_20dp"
                android:src="@drawable/petrolpump" />

            <TextView
                android:id="@+id/register_driver_welcome"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_below="@+id/logo"
                android:layout_marginTop="@dimen/dimen_15dp"
                android:padding="@dimen/dimen_10dp"
                android:text="@string/welcome_driver"
                android:textColor="@color/divider_color"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/edt_mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/register_driver_welcome"
                android:drawableLeft="@drawable/ic_local_phone"
                android:hint="Mobile No."
                android:inputType="number"
                android:maxLength="10"
                android:maxLines="1"
                android:padding="@dimen/dimen_15dp"
                android:textColor="@color/divider_color"
                android:textColorHint="@color/hint_color"
                android:textSize="@dimen/dimen_15dp" />

            <TextView
                android:id="@+id/register_driver_enter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_below="@+id/edt_mobile"
                android:padding="@dimen/dimen_10dp"
                android:text="@string/otp_send"
                android:textColor="@color/divider_color"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/register_driver_otp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/register_driver_enter"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/dimen_5dp"
                android:drawableLeft="@drawable/ic_lock_close"
                android:hint="OTP"
                android:inputType="number"
                android:maxLength="4"
                android:maxLines="1"
                android:padding="@dimen/dimen_15dp"
                android:textColor="@color/divider_color"
                android:textColorHint="@color/hint_color"
                android:textSize="@dimen/dimen_15dp" />


            <Button
                android:id="@+id/register_driver_register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/register_driver_otp"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="@dimen/dimen_20dp"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimary"
                android:text="Register"
                android:textColor="@android:color/white" />

            <Button
                android:id="@+id/register_driver_sendotp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edt_mobile"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="@dimen/dimen_40dp"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimary"
                android:text="Send OTP"
                android:textColor="@android:color/white" />

            <Button
                android:id="@+id/btn_resend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/register_driver_register"
                android:layout_below="@+id/register_driver_otp"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="@dimen/dimen_20dp"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimary"
                android:text="RESEND OTP"
                android:textColor="@android:color/white"
                android:visibility="gone" />
        </RelativeLayout>
    </RelativeLayout>
    </ScrollView>
</RelativeLayout>

Can anyone help me with this please? Thank you.

2

There are 2 best solutions below

0
On

If you want to set a background image, you should set it on your parent relativelayout instead of using an imageview.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/page"
tools:context="com.example.admin.SmartCabFuelCard.RegisterDriver">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_color"
    android:alpha="0.5"/>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds = "true"
    android:isScrollContainer="true"
    android:fillViewport="true">

.....
7
On

Don't set the background in xml. Set the image as window background in your activity as.

getWindow().setBackgroundDrawableResource(R.drawable.bg);

And manifest entry

<activity
        android:name=".ActivityName"
        android:configChanges="screenSize|keyboard|orientation|screenSize"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateHidden" />