KeyboardVisibilityEvent

Links & Dependenciesactivity_main.xmlMainActivity.java

Library on GitHub with dependency + instructions:

github.com/yshrsmz/KeyboardVisibilityEvent

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="com.codinginflow.keyboardvisibilityeventexample.MainActivity">

    <TextView
        android:id="@+id/text_view_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Hello World!"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/edit_text_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="setText"
        android:text="Set Text" />

</LinearLayout>
package com.codinginflow.keyboardvisibilityeventexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent;
import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEventListener;
import net.yslibrary.android.keyboardvisibilityevent.util.UIUtil;

public class MainActivity extends AppCompatActivity {
    private TextView textViewResult;
    private EditText editTextInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textViewResult = findViewById(R.id.text_view_result);
        editTextInput = findViewById(R.id.edit_text_input);

        KeyboardVisibilityEvent.setEventListener(this, new KeyboardVisibilityEventListener() {
            @Override
            public void onVisibilityChanged(boolean isOpen) {
                if (isOpen) {
                    Toast.makeText(MainActivity.this, "keyboard opened", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "keyboard hidden", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setText(View v) {
        String newText = editTextInput.getText().toString();
        textViewResult.setText(newText);

        UIUtil.hideKeyboard(this);
    }
}