maanick90
maanick90

Reputation: 133

android - show keyboard fix bottom framelayout

I am developing an android application in which my mainActivity has 5 tabs at the bottom represented in FrameLayout. Each tab points to a particular fragment which gets loaded based on the tab clicked.

In my 1st tab, I've an EditText. When the edit text is focused, I need to show the keyboard by just moving the layout of ('EditText and Send button) "up" without pushing up the other components including the bottom tab frame layout. Similarly when hiding the keyboard, the 'EditText and Send btn' layout should sit just above the tab frame layout. Can someone please suggest me the solution for this?

(Assume the situation same as in Facebook comment. When we go inside a post to comment something, the layout ('Write Comment' ET and POST btn) will be pushed up without moving any other components including the bottom tabs ('News Feed', 'Requests', 'Messenger', 'Notifications', 'More'))

I don't want to make the frame layout Visibility.Gone/Visible as it ends up in an ugly animation when the keyboard slides up/down.

Upvotes: 3

Views: 582

Answers (2)

Vette
Vette

Reputation: 541

I have the exact same issue. The best workaround I could find (as alluded to in the original question) is below. But this is a marginal solution, and not a great one.

The problem with setting the weightSum is that if the scrollview containing the edit boxes is longer, the button will disappear. The best solution would allow the buttons to stay at bottom of page on normal view, but to be hidden during keyboard onscreen.

 public boolean dispatchTouchEvent(MotionEvent event) {

    View v = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (v instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];
        if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
            layButton.setVisibility(View.VISIBLE);
        }
        else
        {
            layButton.setVisibility(View.GONE);
        }
    }
    return ret;
}

Upvotes: 0

feridok
feridok

Reputation: 658

Use a LinearLayout whit no weightSum, and give your main View in your layout 0dp height, and weight 1

Upvotes: 1

Related Questions