[Android] IME表示時にアクティビティーを伸縮させる

| トラックバック(0) |

Input Method Engine(IME)表示時に、アクティビティーを伸縮させる方法について。

 

例えば、下図のような(テキスト、エディット、ボタンの順に配置された)レイアウトを例に説明します。

IME_OFF.png

 

画面中央のエディットボックスにフォーカスをあてると、IME(ここでは Simeji) が表示されるのですが、

通常では、下図のように画面下半分が隠れてしまい、アクティビティー全体が表示されません。

この結果、画面下部に配置したボタンが操作できなくなってしまいます。

IME_ON_NG.png

 

そこで、IME 表示時にアクティビティーを伸縮させるには、

AndroidManifest.xml ファイルの対象のアクティビティに「android:windowSoftInputMode="adjustResize"」を指定します。

すると、下図のように IME 以外の残りの領域が、アクティビティー全体にレイアウトされます。

この結果、アクティビティー画面下部に配置していたボタンを操作することができます。

IME_ON_OK.png

 

(上記説明で使用した)サンプルソースコード

  • AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...>
    <application android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity android:name=".UpdateStatusActivity"
            android:windowSoftInputMode="adjustResize" />
    </application>
    ...
</manifest>

 

  • res/layout/update_status.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/textview_remain_status_length"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right" />
    <EditText android:id="@+id/edittext_status_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/whats_happening" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button android:id="@+id/button_update"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/update" />
        <Button android:id="@+id/button_cancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/cancel" />
    </LinearLayout>
</LinearLayout>

関連記事

トラックバック(0)

トラックバックURL: http://mt.adakoda.com/mt-tb.cgi/482

Android Advent Calendar 2011

2012年2月

      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29