John Pettersson
John Pettersson

Reputation: 41

Prevent softkeyboard from pushing content up outside of the screen

I have this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:weightSum="10" >

<TextView
    android:id="@+id/textViewTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="10dp"
    android:gravity="center_horizontal"
    android:text="@string/fraga"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="40sp" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/layout_input"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textViewTitle"
    android:layout_margin="10dp"
    android:background="#DDD"
    android:isScrollContainer="false"
    android:padding="10dp" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textViewQuestion"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="18"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" android:contentDescription="@string/image"/>
    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/buttonPrev"
    style="@style/white_button"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="@string/tillbaka" />

<Button
    android:id="@+id/buttonNext"
    style="@style/white_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_toRightOf="@+id/buttonPrev"
    android:text="@string/nasta" />

<LinearLayout
    android:id="@id/layout_input"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/buttonPrev"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:digits="0123456789(),.*-+/=:"
        android:ems="10"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="@string/svar"
        android:inputType="number|numberSigned|numberDecimal"
        android:singleLine="true"
        android:textSize="28sp"
         >

        <requestFocus />
    </EditText>

    <Spinner
        android:id="@+id/spinner"
        style="@android:style/Widget.Spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:visibility="gone" />
</LinearLayout>

</RelativeLayout>

When i click the edittext the softkeyboard pops up and pushes the edittext to be just above the keyboard as expected. The only problem is that it also pushes the content above the edittext up. Is there any way to prevent this from happening?

Upvotes: 1

Views: 488

Answers (2)

Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Use this piece of code in your manifest.xml, inside the activity tag.

android:windowSoftInputMode="adjustPan"

Like this:

<activity
   android:label="activity_name"
   android:name=".MainActivity" android:windowSoftInputMode="adjustPan">
</activity>

Upvotes: 3

P Varga
P Varga

Reputation: 20229

See here for an explanation of android:imeOptions.

Upvotes: 0

Related Questions