From ccaf9f59c48c01d95fe313ea83e717b8b238ad56 Mon Sep 17 00:00:00 2001 From: Steven Luo Date: Thu, 30 Jun 2011 06:51:15 -0700 Subject: [PATCH] Resize the fullscreen conversation window when IME appears instead of scrolling When an activity sets FLAG_FULLSCREEN on its window, Android assumes that the window size will always be the same as the screen size. This causes the window to scroll instead of resizing when the soft keyboard comes up, which (according to a quick Google search) isn't the behavior most developers are expecting. This patch implements an ugly workaround: extend the root element of the layout (in our case, a LinearLayout) to hook into the onMeasure() callback, which is called when the window size changes, so that we can resize ourselves when the window size changes. --- application/res/layout/conversations.xml | 4 +- .../yaaic/activity/ConversationActivity.java | 2 + .../org/yaaic/view/ConversationLayout.java | 135 ++++++++++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 application/src/org/yaaic/view/ConversationLayout.java diff --git a/application/res/layout/conversations.xml b/application/res/layout/conversations.xml index b235430..fea7b56 100644 --- a/application/res/layout/conversations.xml +++ b/application/res/layout/conversations.xml @@ -19,7 +19,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Yaaic. If not, see . --> -. android:drawableLeft="@android:drawable/ic_btn_speak_now" android:visibility="gone" /> - + diff --git a/application/src/org/yaaic/activity/ConversationActivity.java b/application/src/org/yaaic/activity/ConversationActivity.java index 24a2bc9..d2ff4d3 100644 --- a/application/src/org/yaaic/activity/ConversationActivity.java +++ b/application/src/org/yaaic/activity/ConversationActivity.java @@ -65,6 +65,7 @@ import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.Configuration; +import android.graphics.Rect; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -84,6 +85,7 @@ import android.widget.Button; import android.widget.EditText; import android.widget.Gallery; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewSwitcher; diff --git a/application/src/org/yaaic/view/ConversationLayout.java b/application/src/org/yaaic/view/ConversationLayout.java new file mode 100644 index 0000000..6f9eae3 --- /dev/null +++ b/application/src/org/yaaic/view/ConversationLayout.java @@ -0,0 +1,135 @@ +/* +Yaaic - Yet Another Android IRC Client + +Copyright 2009-2011 Sebastian Kaspari + +This file is part of Yaaic. + +Yaaic is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Yaaic is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Yaaic. If not, see . + */ +package org.yaaic.view; + +import org.yaaic.model.Settings; + +import android.app.Activity; +import android.content.Context; +import android.content.res.Configuration; +import android.graphics.Canvas; +import android.graphics.Rect; +import android.util.AttributeSet; +import android.view.Gravity; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.LinearLayout; + +/** + * ConversationLayout: LinearLayout that resizes correctly when an IME + * comes up in fullscreen mode + * + * @author Steven Luo + */ +public class ConversationLayout extends LinearLayout +{ + Activity activity; + int curHeight = 0; + boolean fullscreen = false, isLandscape = false; + boolean redoLayout = false; + + /** + * Create a new conversation view switcher + * + * @param context + */ + public ConversationLayout(Context context) + { + super(context); + doInit(context); + } + + /** + * Create a new conversation view switcher + * + * @param context + * @param attrs + */ + public ConversationLayout(Context context, AttributeSet attrs) + { + super(context, attrs); + doInit(context); + } + + /** + * Initialize the ConversationLayout + */ + private void doInit(Context context) + { + activity = (Activity) context; + fullscreen = (new Settings(context)).fullscreenConversations(); + isLandscape = (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE); + } + + /** + * Get the height of the window's visible area + */ + private int getWindowHeight() + { + Rect visible = new Rect(); + getWindowVisibleDisplayFrame(visible); + return visible.height(); + } + + /** + * onMeasure (ask the view how much space it wants) + * This is called when the window size changes, so we can hook into it to + * resize ourselves when the IME comes up + */ + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) + { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + /* XXX: We should probably use some heuristic of how many pixels are + available for deciding whether to scroll instead of resize, instead + of refusing to resize in landscape */ + if (!fullscreen || isLandscape) { + return; + } + + int height = getWindowHeight(); + if (curHeight != height) { + curHeight = height; + FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( + FrameLayout.LayoutParams.FILL_PARENT, + height + ); + params.gravity = Gravity.BOTTOM | Gravity.CLIP_VERTICAL; + setLayoutParams(params); + redoLayout = true; + } + } + + /** + * onDraw (draw the view) + */ + @Override + protected void onDraw(Canvas canvas) + { + if (redoLayout) { + // Layout params have changed -- force a layout update + requestLayout(); + redoLayout = false; + } + super.onDraw(canvas); + } +}