Software keyboard open/close watcher for Android.

Overview

AndroidKeyboardWatcher

Software keyboard open/close watcher for Android.

Android SDK doesn't provide direct way to track open/close events from software keyboard. This small library does it for you.

AndroidKeyboardWatcher

Usage

Make sure that you use adjustResize windowSoftInputMode in your Acitivty configuration in AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="adjustResize" />

Bind KeyboardWatcher in your Activity.onCreate() method. To prevent memory leaks make sure to unbind it in onDestroy() method.

public class MainActivity extends Activity implements KeyboardWatcher.OnKeyboardToggleListener {
    private KeyboardWatcher keyboardWatcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...
        keyboardWatcher = new KeyboardWatcher(this);
        keyboardWatcher.setListener(this);
    }

    @Override
    protected void onDestroy() {
        keyboardWatcher.destroy();
        super.onDestroy();
    }

    @Override
    public void onKeyboardShown(int keyboardSize) {
        
    }

    @Override
    public void onKeyboardClosed() {
        
    }
}

Download

Java code

If you don't want to add another dependency to your project, just copy KeyboardWatcher.java to your source directory.

Library dependency

dependencies {
  compile 'com.azimolabs.keyboardwatcher:keyboardwatcher:0.1.3'
}

License

Copyright (C) 2016 Azimo

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.

Towards financial services available to all

We’re working throughout the company to create faster, cheaper, and more available financial services all over the world, and here are some of the techniques that we’re utilizing. There’s still a long way ahead of us, and if you’d like to be part of that journey, check out our careers page.

Comments
  • Not static, leak free

    Not static, leak free

    Hello, I like where this is going. I hope these suggestions help you building a robust tool people can use:

    1)

    Ditch ìnitWith as it does nothing. Have a public KeyboardWatcher(Activity activity) constructor instead. The client activity will hold a reference to an instance. So you can have multiple activities using keyboard watcher at the same time. Activity will create an instance in onCreate and keep it.

    2)

    Use a more robust way to get the root view. android.R.id.content can change by calling Activity.setContentView(...). Activity.getWindow().getDecorView().getRootView() will not. See http://stackoverflow.com/a/11553582/2444099. Store the reference right after you get the activity in constructor.

    3)

    Define private final ViewTreeObserver.OnGlobalLayoutListener viewTreeObserverListener = new GlobalLayoutListener(); You can do this because rootView is resolved at the point of execution.

    4)

    Use weak references for activity, rootView and onKeyboardToggleListener. As soon as the activity is destroyed, you lose the references and prevent leaks.

    5)

    Rename bindKeyboardWatcher to setListener. Remove unbindKeyboardWatcher because setListener(null).

    6)

    Consider having addListener and removeListener instead of setListener.

    enhancement 
    opened by consp1racy 5
  • Problem when changing orientation

    Problem when changing orientation

    This problem happen when activity attribut: android:configChanges="orientation" is used in the manifest. With this attribute set, the activity is not destroyed/recreated when we change orientation. That mean that KeyboardWatcher is not destroyed/recreated neither. So the initialValue is now wrong and since the detection of the presence of the keyboard is base on that, onKeyboardClosed() and onKeyboardShown() are thrown wrongly. So it would be nice to add support for that use case, for example add a resetInitialValue method or something like that.

    For now the solution I found is to destroy/recreate the KeyboardWatcher in the onConfigurationChanged method:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        if (mKeyboardWatcher != null) {
            mKeyboardWatcher.destroy();
            mKeyboardWatcher = new KeyboardWatcher(this);
            mKeyboardWatcher.setListener(this);
        }
    }
    
    opened by dgenest65 0
  • when the Activity Fullscreen,it doesn't work

    when the Activity Fullscreen,it doesn't work

    when ths theam of activity like this "android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen",it is not work,i feel that "AndroidBug5497Workaround" will be helpful for this problem,but i failed !

    opened by wenzhihao123 0
  • Wrong keyboard height on device rotate

    Wrong keyboard height on device rotate

    I have found issue in keyboard height calculation after device rotate. The solution is to calculate keyboard height initialValue - rootViewRef.get().getHeight() in KeyboardWatcher.GlobalLayoutListener:onGlobalLayout() after keyboard has already finished slide from bottom animation. I need hotfix for my project right now so I used new Handler().postDelayed(() -> {//onGlobalLayout method content//}, 1000); but there should be better solution

    opened by erva 1
  • the initialValue is always == rootView.get().getHeight()

    the initialValue is always == rootView.get().getHeight()

    if (initialValue == 0) { initialValue = rootViewRef.get().getHeight(); } else { if (initialValue > rootViewRef.get().getHeight()) { if (onKeyboardToggleListenerRef.get() != null) { if (!hasSentInitialAction || !isKeyboardShown) { isKeyboardShown = true; onKeyboardToggleListenerRef.get().onKeyboardShown(initialValue - rootViewRef.get().getHeight()); } } } else { if (!hasSentInitialAction || isKeyboardShown) { isKeyboardShown = false; rootViewRef.get().post(new Runnable() { @Override public void run() { if (onKeyboardToggleListenerRef.get() != null) { onKeyboardToggleListenerRef.get().onKeyboardClosed(); } } }); } } hasSentInitialAction = true; }

    opened by anjiao 2
Owner
Azimo Labs
We're the tech team behind Azimo - the faster, cheaper way to send money internationally.
Azimo Labs
A 2020s compatible React Native keyboard avoiding view for Android and iOS that just works.

react-native-keyboard-shift Example Snack coming soon Until then: Clone this repo: git clone https://github.com/FullStackCraft/react-native-keyboard-s

Full Stack Craft 66 Aug 16, 2022
OSGeo4A is a build environment to cross-compile opensource GIS software for android devices

OSGeo4A This provides a set of scripts to build opensource geo tools for Android. This is Experimental Dependencies instructions you need a JDK v8 or

OPENGIS.ch 31 Aug 5, 2022
This software was developed using an Agile and TDD methodology

VM-EmpDirectory Software Development Approach This software was developed using

null 0 Dec 22, 2021
Copylefted libre software (GPLv3+) card management app

Catima Copylefted libre software (GPLv3+) card management app. Logo by Rose (TangentFoxy) Stores your store loyalty and membership cards on your devic

Catima 402 Jan 4, 2023
Game project in TDT4240 Software Architecture

Star Battle NTNU Intergalactic space warfare - Game project in TDT4240 Software Architecture View Demo Table of Contents About the Project Tech Stack

William H. Le 3 Sep 28, 2022
📱 Andriller - is software utility with a collection of forensic tools for smartphones

Andriller - is software utility with a collection of forensic tools for smartphones. It performs read-only, forensically sound, non-destructive acquisition from Android devices. It has features, such as powerful Lockscreen cracking for Pattern, PIN code, or Password; custom decoders for Apps data from Android (some Apple iOS & Windows) databases for decoding communications. Extraction and decoders produce reports in HTML and Excel formats.

Denis Sazonov 980 Dec 27, 2022
Design patterns are typical solutions to common problems in software design

Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you can customize to solve a particular design problem in your code.

hamid 4 Aug 30, 2022
Open Super dApp - Your gateway to the new digital commons. Integrated mobile messenger, Ethereum wallet, and Web 3.0 browser built on open, decentralized, and encrypted protocols.

A fully open source, open standard, decentralized "super app" including a secure, encrypted Matrix compatible messenger based off of the Element Messenger, and an Ethereum crypto wallet and web3 browser based off of Alpha Wallet.

2Gather 6 Jul 25, 2022
Ride-Sharing Uber Lyft Android App - Learn to build a ride-sharing Android Taxi Clone App like Uber, Lyft - Open-Source Project By MindOrks

Ride-Sharing Uber Lyft Android App - Learn to build a ride-sharing Android Taxi Clone App like Uber, Lyft - Open-Source Project By MindOrks

MindOrks 1.2k Dec 29, 2022
An Open-Source repository that contains all the Android Dev and Kotlin concepts and several projects on android ranging in difficulty making this repo completely beginner-friendly.

An Open-Source repository that contains all the Android Dev and Kotlin concepts and several projects on android ranging in difficulty making this repo completely beginner-friendly. The main aim of this repository is to help students who are learning Android Development or preparing for an Android Developer role-based job.

Mimo Patra 15 Dec 29, 2022
Open-source modular Android App IDE for Android

Blokkok Blokkok is an open-source modular Android App IDE for Android. Every components of the IDE are separated from each other by modules, every mod

null 14 Dec 16, 2022
An open source Hacker News client for Android.

Hacker News Come chat with us on Gitter.im! An open source Hacker News client for Android phones & tablets. Available on the Google Play Store How to

Matthew Bishop 227 Dec 11, 2022
An open source clone of a famous flappy bird game for Android using AndEngine

OpenFlappyBird An open source clone of a famous flappy bird game for Android using the amazing AndEngine History When the original Flappy Bird game wa

Dean Wild 299 Dec 26, 2022
A Free, Fully Fledged, Open-Source Music Player for Android

Turtle Player Free, Fully Fledged & Open-Source. The Music Player for Android. Turtle Player is designed to be a fun and unique app for playing your m

Edd Turtle 178 Nov 18, 2022
Freegemas libGDX is an Android and Java desktop port of Freegemas, which in turn is an open source version of the well known Bejeweled.

freegemas-gdx Freegemas libGDX is an Android, HTML 5 and Java desktop port of Freegemas, which in turn is an open source version of the well known Bej

David Saltares 144 Jun 21, 2022
Open source Imgur Android App

Opengur NO LONGER UNDER ACTIVE DEVELOPMENT Open source Imgur Android App Want to become a beta tester? Click Here! #Features Built in native java Nati

Kenny 324 Nov 22, 2022
Android open source calendar

Etar Calendar Etar (from Arabic: إِيتَار) is an open source material designed calendar made for everyone! Why? Well, I wanted a simple, material desig

null 1.5k Dec 30, 2022
Open source Imgur Android App

Opengur NO LONGER UNDER ACTIVE DEVELOPMENT Open source Imgur Android App Want to become a beta tester? Click Here! #Features Built in native java Nati

Kenny 324 Nov 22, 2022