Search code examples
androidandroid-layoutimageviewandroid-linearlayoutandroid-layout-weight

if I set linearlayout weight when keyboard pops it resizes its continent


I folowed this example and manage to put fab button in right place

enter image description here

but when I press on edit text keyboard pops up and it messes up layout weight

enter image description here

How can I make that ImageView would be static and when keyboard opens it would stay on same size?

XML:

<android.support.design.widget.CoordinatorLayout    
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:focusableInTouchMode="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/ViewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@color/colorPrimary"
android:orientation="horizontal">

<ImageView
    android:id="@+id/imgview"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@color/light_blue"
    android:scaleType="centerCrop"
    android:src="@android:drawable/ic_menu_camera"
    android:layout_gravity="top|center_horizontal"
    />

</LinearLayout>

<LinearLayout
    android:id="@+id/ViewB"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.3"
    android:orientation="horizontal">
</LinearLayout>
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/ViewA"
    android:clickable="true"
    app:layout_anchorGravity="bottom|right"
    app:elevation="5dp"
    app:pressedTranslationZ="0dp"
    android:src="@android:drawable/ic_menu_camera"/>


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

Solution

  • This is because of android default behaviour that view will resize to make room for keyboard. you can update this in your manifest file posible values for android:windowSoftInputMode --> adjustResize, adjustPan, adjustUnspecified

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

    More info