Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll my layout when setting android:windowSoftInputMode="adjustPan"?

My activity have a top bar and a bottom bar. the space between topbar and bottom bar i have a linearlayout with several edittext views inside. Because i don't want my layout resize every time the softkeyboard show up, so i set android:windowSoftInputMode="adjustPan" for my activity in manifest. But when the softkeyboard is openned, i want to scroll down to select another edittext to input, it's not allow me do that. Im only able to select the edittext at bottom when i close the softkeyboard. That's very annoying and inconvenient. How can i get both scrollview and ajustpan mode for softkeyboard work well together?

Please help me out. thanks you so much.

like image 627
MichaelP Avatar asked Apr 20 '12 04:04

MichaelP


People also ask

What is window soft input mode?

Posted on October 25, 2010 by Lars Vogel. Android has the so-called Input Method Framework to support different input methods, e.g. keyboard, soft-keyboard, handwriting etc. If the soft-keyboard is used it is diplayed at the bottom of the screen once the user select an text view to edit it.


1 Answers

At last, i find out a workaround for my problem, so i want to share for someone maybe get the same problem in future. A brief description of my layout as following:

<myRelativeLayout>
<topbar.../>
<myscrollView>
    <linearLayout>
        //all stuff controls:editview,textview,....
    </linearLayout>
</myscrollView>
<bottombar.../>

i create custom class myRelativeLayout extend RelativeLayout

public class myRelativeLayout extends RelativeLayout{

public interface OnRelativeLayoutChangeListener {
    void onLayoutPushUp();
    void onLayoutPushDown();
}

private OnRelativeLayoutChangeListener layoutChangeListener;
public myRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
    final int actualHeight = getHeight();

    if (actualHeight > proposedheight){
        // Keyboard is shown
        layoutChangeListener.onLayoutPushUp();
    } else if(actualHeight < proposedheight){
        // Keyboard is hidden
        layoutChangeListener.onLayoutPushDown();
    }       
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void setLayoutChangeListener(OnRelativeLayoutChangeListener layoutChangeListener) {
    this.layoutChangeListener = layoutChangeListener;
}

public OnRelativeLayoutChangeListener getLayoutChangeListener() {
    return layoutChangeListener;
}

}

And in my activity , i just set setLayoutChangeListener for myRelativeLayout to hide bottombar when softkeyboard show up and display bottombar when softkeyboard hide:

myRlayout.setLayoutChangeListener(new OnRelativeLayoutChangeListener() {

        @Override
        public void onLayoutPushUp() {
            // TODO Auto-generated method stub
            myBottombar.setVisibility(View.GONE);//in my case i need to setVisibility(View.GONE) to bottombar in order for this bar is not displayed when softkeyboard show up.
        }

        @Override
        public void onLayoutPushDown() {
            // TODO Auto-generated method stub
            myBottombar.setVisibility(View.VISIBLE);// redisplay myBottombar when keyboard is closed.

        }
    });

Dont forget set android:windowSoftInputMode="adjustResize" for activity. Hope this useful for someone got the same problem.

like image 191
MichaelP Avatar answered Sep 19 '22 15:09

MichaelP