1

I have a login screen with scroll view and I'm setting the layout the full screen like so

if (VERSION.SDK_INT >= 21) {
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

The problem is I'm not able to scroll when the keyboard pops up even though I have a scroll view. Here's my layout

<ScrollView
android:id="@+id/scrollView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ooru"
android:fitsSystemWindows="false"
tools:context=".android.LoginActivity">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/branding_login"
        android:layout_width="170dp"
        android:layout_height="35dp"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="55dp"
        android:scaleType="centerInside"
        android:src="@drawable/branding"/>

    <ImageView
        android:id="@+id/logged_in_icon"
        android:layout_width="54dp"
        android:layout_height="54dp"
        android:layout_below="@id/branding_login"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="42dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_fan_icon"/>

    .... //More views

</RelativeLayout>

</ScrollView>

I tried searching about the problem and some suggested adding android:windowSoftInputMode="adjustResize", it doesn't work for me.

P.S: The parent theme for this activity is Theme.AppCompat.Light.NoActionBar

Shashank Udupa
  • 2,173
  • 1
  • 19
  • 26
  • add "android:fillViewport="true" " to your scroll view and set relative layout height as match_parent – Sachchidanand Jun 07 '16 at 12:27
  • look this answer - http://stackoverflow.com/questions/10599451/how-to-make-an-android-view-scrollable-when-the-keyboard-appears/10599512#10599512 – Sunil Sharma Jun 07 '16 at 12:29
  • Setting fillViewPort and adding match_parent did not work, and @SunilSharma I had already tried doing it as shown in that answer – Shashank Udupa Jun 07 '16 at 12:32
  • If problem is in above lollipop then try `android:fitsSystemWindows="true"` – Sunil Sharma Jun 07 '16 at 12:36
  • Did that as well, actually I tried removing View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) flag, the scroll works fine then, but it will show the status bar and that's not what I want – Shashank Udupa Jun 07 '16 at 12:38
  • If nothing is working then try using view tree observer and scrollY parameter in scrollview – Sunil Sharma Jun 07 '16 at 12:55

3 Answers3

1

Try

1. Add KeyboardUtil class to your code.

2. Add following line to onCreate method of activity

new KeyboardUtil(this, contentView) //inflate layout and assign it to contentView object

KeyboardUtil.java

/*
 * Copyright 2015 Mike Penz All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.materialdrawer.util;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by mikepenz on 14.03.15.
 * This class implements a hack to change the layout padding on bottom if the keyboard is shown
 * to allow long lists with editTextViews
 * Basic idea for this solution found here: http://stackoverflow.com/a/9108219/325479
 */
public class KeyboardUtil {
    private View decorView;
    private View contentView;

    public KeyboardUtil(Activity act, View contentView) {
        this.decorView = act.getWindow().getDecorView();
        this.contentView = contentView;

        //only required on newer android versions. it was working on API level 19
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void enable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void disable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }


    //a small helper to allow showing the editText focus
    ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            decorView.getWindowVisibleDisplayFrame(r);

            //get screen height and calculate the difference with the useable area from the r
            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;

            //if it could be a keyboard add the padding to the view
            if (diff != 0) {
                // if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
                //check if the padding is 0 (if yes set the padding for the keyboard)
                if (contentView.getPaddingBottom() != diff) {
                    //set the padding of the contentView for the keyboard
                    contentView.setPadding(0, 0, 0, diff);
                }
            } else {
                //check if the padding is != 0 (if yes reset the padding)
                if (contentView.getPaddingBottom() != 0) {
                    //reset the padding of the contentView
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };


    /**
     * Helper to hide the keyboard
     *
     * @param act
     */
    public static void hideKeyboard(Activity act) {
        if (act != null && act.getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

References:

  1. https://github.com/mikepenz/MaterialDrawer/issues/95

  2. KeyboardUtil

Community
  • 1
  • 1
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
0

The main problem is that you set the screen to fullscreen mode.

View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

if you set fullscreen as

View.SYSTEM_UI_FLAG_FULLSCREEN 

with

android:windowSoftInputMode="adjustResize".

However, one problem is that it will show up actionbar.

Sam Willis
  • 4,001
  • 7
  • 40
  • 59
Bipin
  • 31
  • 3
  • the layout you made is not perfect, we need scroll there, where view is large and your view is two small and not perfect that's why isn't scrolling,you can hide you keyboard when login launch and on edit ext click keyboard will show. – rachna Oct 23 '19 at 12:27
0

For those who came here expecting a solution.

If you want a login screen without SupportActionBar, hide it dynamically, no need for FullScreen theme.

@Override
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_yours);
    if (getSupportActionBar() != null)
        getSupportAction().hide();

    ...
        your onCreate code here
    ...
}

Hope it helps.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
phsa
  • 61
  • 7