Coder Social home page Coder Social logo

anthonycas / softkeyboardadjust Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yatw/softkeyboardadjust

0.0 1.0 0.0 1.15 MB

Example of how to pan the page up more with or without full screen mode (works with either adjustPan or adjustResize)

Java 100.00%

softkeyboardadjust's Introduction

SoftKeyboardAdjust

Example of how to pan the page up more with or without full screen mode (works with either adjustPan or adjustResize)

Custom pan up Default adjustPan

If you have a layout that is resizeable, and you actually want to resize. Use the original AndroidBug5497Workaround.

If you have a layout that is not resizeable, this example shows you how to create the pan up effect (with or without full screen mode)

  • As of why it work with or without full screen mode. Because now we are always listening to the layout height changed and applying our own pan up effect.
  • As of why it work with either adjustPan or adjustResize. Because our layout is not resizeable, so our own pan up effect always take effect.

Note: This is not a library, this is merely an example. You must understand how to create these affect to apply them into your own project.

Note: Only work on portrait mode In order to work in landscape mode we need to obtain the screen height differently.

Set Up

In MainActivity, there is a flag USE_IMMERSIVE_MODE to control full screen mode or not.

Experiment with resizeable and non-resizeable layout.
Experiment with adjustPan and adjustResize input mode.
Experiment with different devices.

The current activity_main.xml is non resizeable, all margin are fixed distance. A normal adjustResize will not have any affect.

When to execute the shift up shift down

In order for it to shift up when clicking on another editText while keyboard is open. We need to obtain that touch event from editText onTouch or onClick method. And call shift up from there.

Consecutive click

We always shift the page down when the keyboard is closed.

How to create the pan up effect

We can manually shift up and down our layout view to create the pan up effect.

On any view, we can use the scrollTo method to to shift the page up.

Now is just to calculate how much distance to shift the page up. Let say we want to bring the current focused editText up to 1/4 of the screen height, then a large space below the editText will be showing.

So the distance to scroll is:
distance.png

After we scroll the distance, “Where I currently am” will be at the position of “Where I want to be”.

When we want to shift up, (may be detected from keyboard open or edittext gain focus) we call this handleShiftUpmethod.

We can call this method anywhere, just make sure to pass in current focusedView and mChildOfContent is the root layout

absY is “Where I currently am”

If absY is already above 1/4 of the screen, there is no need to shift up anymore. But if it is below 1/4 of the screen, we call scrollTo on the root layout to shift it up to 1/4.

To shift up, scroll the root view to the calculated distance
To shift down, scroll the root view back to 0

Refer to the handleShiftUp() and handleShiftDown() method here.
If you don’t want 1/4, change it accordingly.

Modified AndroidBug5497Workaround2

What I changed from the original AndroidBug5497Workaround.

My AndroidBug5497Workaround2

  1. Changed how to detect keyboard close. By comparing the previous available height with available height now. If keyboard is closed, then previous height (with keyboard) is less than available height now. And that difference need to be large enough. (1/4 of the screen, change as you like)
    detectClose.png

Some phone, like the motorola XT1097, will disable the full screen mode after opening the soft keyboard. This mean the bottom navigation bar will appear and cause our height to change. It does not reach 1/4 of the screen and our listener think the keyboard just closed. (which it is not)

  1. onKeyboardOpen, I do not requestLayout. Calling requestLayout() here cause a blank white space in between the keyboard and content. I suspect it is because it is trying to resize after the pan up.
    blankWhiteSpace.jpg

  2. Rely on the EditText onTouch event to trigger handleShiftUp() method

Modified EditText

Contain a setOnTouchListener to call the shift up method. Link here.

You might have to come up with your own way to call your own shift up method.

Listener

Having a dedicated listener for keyboard open and close is useful. Like returning back to immersive mode again after keyboard close.

Listener is in MainActivity onCreate()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.