aeroxr1
aeroxr1

Reputation: 1074

How can I correctly resize the layout when the keyboard appears?

In Android I have reproduced the following layout :

enter image description here

In this way :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#94F734"
    android:orientation="vertical">

    <LinearLayout  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:layout_marginLeft="48dp"
        android:padding="0dp">

        <include layout="@layout/header"/>

    </LinearLayout>

    <RelativeLayout 
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="48dp"
        android:layout_marginRight="48dp"
        android:layout_marginBottom="48dp"
        android:paddingTop="96dp"
        android:background="#463779B3"
        android:visibility="visible">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28dp"
            android:text="title"
            android:layout_above="@+id/code"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="48dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/code"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Insert here "
            android:background="@drawable/back_item_edit"
            android:textColor="#FFFFFF"
            android:layout_centerVertical="true"
            android:layout_marginLeft="36dp"
            android:layout_marginRight="36dp">
            <requestFocus />

        </EditText>

        <Button
            android:id="@+id/prova"
            android:layout_below="@id/code"
            style="@style/initialButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="26dp"
            android:text="Connect"
            android:textColor="#000000"
            android:layout_alignLeft="@id/code"
            android:layout_alignRight="@id/code"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:visibility="visible"/>

    </RelativeLayout>

</LinearLayout>

But when I click on "insert here" one problem appears : the layout is not correctly resized and doesn't fit in the little screen.

I thought to use a scrollview, but I don't know how to use it.

When the keyboard appears I would to show :

Is there a way to resolve my problem ?

Upvotes: 0

Views: 563

Answers (2)

Jim
Jim

Reputation: 10278

First, you specify a lot of parameters using "dpi", like:

android:layout_marginTop="48dp"

This does not work for dynamically sizing much of anything. You are requiring the heights to be a specific height regardless of the size of other elements on the screen.

To solve this problem you first need to move away from absolute measurements, and start using runtime measurements. Normally, you could simply use the "hdpi", "xhdpi", etc. folders to manage this, but the layout with a keyboard is a dynamic measure (at runtime).

You could use XML hints like a weighted LinearLayout to adjust to the screen based on the available size. You could also use java code to change the size of elements. But you cannot expect that the screen will display elements that are larger than the available space (which is the problem it is having now). Also, you will need to determine how the elements will be "squeezed" together.

EDIT:

To make different layouts have different margins, etc. based on the screen size/density, you should look at having folders for each screen density (i.e. a folder for "layout-xhdpi" for example).

Here are the docs: http://developer.android.com/guide/practices/screens_support.html

Including using "dimens" folders like here:

Different values folders in android

So you have a lot of options for handling dynamic screens sizes. But maybe the "easiest" for many situations (like yours, probably) is to use the displayMetrics and adjust your elements accordingly:

getting the screen density programmatically in android?

You can measure, resize, etc. Each element has different LayoutParams or other attributes that allows you to change them programmatically. You probably need to do searches for the particular things that you want to change.

Upvotes: 2

Ranjithkumar
Ranjithkumar

Reputation: 707

You can use this property in your manifest file for your activity windowSoftInputMode.

Upvotes: 1

Related Questions