Search code examples
androidscrollbackground-image

Background image resizes every time keyboard is opened


I have an activity and a fragment in my application.The activity consists of a RelativeLayout which is the holder for the fragment.I set a drawable image as background of the RelativeLayout.Upon clicking the EditText the keyboard opens which causes the background image of the relativelayout to resize.

Code: AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="stateVisible|adjustResize">

If I remove 'adjustResize', then I can't scroll the view.

holder_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="15dp"
        android:id="@+id/usr_name_img"
        android:src="@drawable/user"
        android:tint="#ffffff"
        android:layout_marginTop="@dimen/img_mrgn_top"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:id="@+id/name"
        android:textColor="#ffffff"
        android:layout_toRightOf="@+id/usr_name_img"
        android:background="@drawable/edittext_bg"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="@dimen/lyt_mrgn_top"
        android:hint="Your name"
        android:textColorHint="#ffffff"
        android:paddingLeft="30dp"
        android:inputType="textMultiLine" />
    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:layout_below="@+id/name"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:id="@+id/place_img"
        android:src="@drawable/location"
        android:tint="#ffffff"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:layout_below="@+id/name"
        android:id="@+id/locality"
        android:background="@drawable/edittext_bg"
        android:textColor="#ffffff"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:hint="Locality"
        android:textColorHint="#ffffff"
        android:paddingLeft="30dp"
        android:layout_alignLeft="@+id/name"
        android:layout_alignStart="@+id/name"
        android:inputType="textMultiLine" />
    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:id="@+id/email_img"
        android:src="@drawable/email"
        android:tint="#ffffff"
        android:layout_below="@+id/locality"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:layout_below="@+id/locality"
        android:id="@+id/email"
        android:textColor="#ffffff"
        android:background="@drawable/edittext_bg"
        android:inputType="textEmailAddress"
        android:layout_marginTop="10dp"
        android:hint="E-Mail"
        android:textColorHint="#ffffff"
        android:paddingLeft="30dp"
        android:layout_alignLeft="@+id/locality"
        android:layout_alignStart="@+id/locality"
        android:layout_alignRight="@+id/locality"
        android:layout_alignEnd="@+id/locality" />
    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:layout_below="@+id/email"
        android:src="@drawable/phone"
        android:tint="#ffffff"
        android:id="@+id/mobile_img"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:layout_below="@+id/email"
        android:id="@+id/phone"
        android:background="@drawable/edittext_bg"
        android:layout_marginTop="10dp"
        android:hint="Mobile No."
        android:textColor="#ffffff"
        android:inputType="phone"
        android:textColorHint="#ffffff"
        android:paddingLeft="30dp"
        android:layout_alignLeft="@+id/email"
        android:layout_alignStart="@+id/email"
        android:layout_alignRight="@+id/email"
        android:layout_alignEnd="@+id/email" />
    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:layout_below="@+id/phone"
        android:src="@drawable/event"
        android:tint="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:id="@+id/imageView2" />
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:gravity="center"
        android:overlapAnchor="false"
        android:background="@drawable/edittext_bg"
        android:popupBackground="#BB000000"
        android:layout_below="@+id/phone"
        android:spinnerMode="dropdown"
        android:id="@+id/eventspinner"
        android:layout_marginTop="10dp"
        android:layout_alignLeft="@+id/phone"
        android:layout_alignStart="@+id/phone"
        android:layout_alignRight="@+id/phone"
        android:layout_alignEnd="@+id/phone"></Spinner>
<EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:id="@+id/bdgt"
        android:inputType="numberDecimal"
        android:hint="Your Budget"
        android:textColorHint="#ffffff"
        android:paddingLeft="25dp"
        android:textColor="#ffffff"
        android:layout_below="@+id/eventspinner"
        android:background="@drawable/edittext_bg"
        android:layout_marginTop="10dp"
        android:layout_alignLeft="@+id/eventspinner"
        android:layout_alignStart="@+id/eventspinner"
        android:layout_alignRight="@+id/eventspinner"
        android:layout_alignEnd="@+id/eventspinner" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="@dimen/btn_height"
        android:id="@+id/book"
        android:background="@drawable/btn_bg"
        android:layout_centerHorizontal="true"
        android:text="Book Now"
        android:textColor="#ffffff"
        android:layout_marginLeft="90dp"
        android:layout_below="@+id/noOfPeople"
        android:layout_marginTop="40dp"/>
<ImageView
        android:layout_width="10dp"
        android:layout_height="@dimen/drp_dwn_arrw_hght"
        android:layout_below="@+id/phone"
        android:layout_alignParentRight="true"
        android:layout_marginRight="25dp"
        android:layout_marginTop="25dp"
        android:src="@drawable/downarrow"
        android:tint="#ffffff"
        android:id="@+id/imageView" />
<ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:id="@+id/bdgt_img"
        android:src="@drawable/budget"
        android:tint="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/eventspinner"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:id="@+id/noOfPeople"
        android:layout_marginTop="10dp"
        android:background="@drawable/edittext_bg"
        android:hint=" Number ofpeople"
        android:paddingLeft="25dp"
        android:textColorHint="#ffffff"
        android:textColor="#ffffff"
        android:inputType="number"
        android:layout_toRightOf="@+id/group"
        android:layout_below="@+id/bdgt"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:id="@+id/group"
        android:tint="#ffffff"
        android:src="@drawable/group"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/bdgt"
        android:layout_alignLeft="@+id/bdgt_img"
        android:layout_alignStart="@+id/bdgt_img" />
</RelativeLayout>
</ScrollView>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/cstm_toolbar"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:background="#AB47BC">
    </android.support.v7.widget.Toolbar>


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             android:orientation="vertical"
            android:background="@drawable/bg"
            android:layout_centerHorizontal="true"
            android:id="@+id/frag_holder"
            android:layout_below="@+id/cstm_toolbar"
        ></LinearLayout>
</RelativeLayout>

Solution

  • Use this in the Manifest file.

    android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
    

    and remove background image from Xml file and set it via Java code using below way

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