sharath
sharath

Reputation: 3616

Android : PopupWindow fill space above keyboard

I have a button inside a scrollView.When the button is clicked, I need a PopupWindow to show up with the softkeyboard up as soon as the button is clicked. This is how my code looks today :

final Button b = (Button) findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.popup, null);
                final PopupWindow popupWindow = new PopupWindow(popupView,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT,true);
                popupWindow.showAtLocation(v.getRootView(), Gravity.FILL, 0, 0);

                //Bring soft keyboard up : NOT WORKING
                final InputMethodManager mInputMethodManager = (InputMethodManager) getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                EditText editText = (EditText) popupView.findViewById(R.id.editText);
                mInputMethodManager.showSoftInput(editText, 0);

            }
        });

and my Layout XML looks like this :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/yourViewsEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:hint="Your Views"
        >
    </EditText>



</RelativeLayout>

When I try a fill_parent instead of the wrap_content, it starts off by filling the whole screen. I am trying to achieve something like this :enter image description here

Upvotes: 1

Views: 2815

Answers (2)

waqaslam
waqaslam

Reputation: 68177

Perhaps try using the following xml layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:isScrollContainer="true" >


    <EditText
        android:id="@+id/yourViewsEditText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:hint="Your Views" />

</RelativeLayout>

It should automatically set the height of your layout till the start of your soft-keyboard.

Upvotes: 3

בועז יערי
בועז יערי

Reputation: 455

you can get the hight of the keyboard like this:

myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    // TODO Auto-generated method stub
                    Rect r = new Rect();
                    parent.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parent.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    loadDialog(heightDifference);

                }
            });

and then you can load the popupwindow with the height of the keyboard and the width of the device:

public void loadDialog(int height)
{
Display mDisplay = activity.getWindowManager().getDefaultDisplay();
int width  = mDisplay.getWidth();

        //load the popup window with the height and width...

}

Upvotes: 1

Related Questions