mirror of
https://github.com/gsantner/dandelion
synced 2024-11-22 04:12:08 +01:00
SoftKeyboardStateWatcher
This commit is contained in:
parent
cad0ce9939
commit
2ffaa3f1f9
2 changed files with 112 additions and 0 deletions
|
@ -81,6 +81,7 @@ import java.util.Date;
|
|||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import de.baumann.diaspora.utils.Helpers;
|
||||
import de.baumann.diaspora.utils.SoftKeyboardStateWatcher;
|
||||
|
||||
public class MainActivity extends AppCompatActivity
|
||||
implements NavigationView.OnNavigationItemSelectedListener, WebUserProfileChangedListener {
|
||||
|
@ -154,6 +155,22 @@ public class MainActivity extends AppCompatActivity
|
|||
});
|
||||
|
||||
|
||||
// Keyboard State Watcher
|
||||
final SoftKeyboardStateWatcher softKeyboardStateWatcher
|
||||
= new SoftKeyboardStateWatcher(findViewById(R.id.swipe));
|
||||
|
||||
softKeyboardStateWatcher.addSoftKeyboardStateListener(new SoftKeyboardStateWatcher.SoftKeyboardStateListener() {
|
||||
@Override
|
||||
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
|
||||
fab.setVisibility(View.GONE);
|
||||
}
|
||||
@Override
|
||||
public void onSoftKeyboardClosed() {
|
||||
fab.setVisibility(View.VISIBLE);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Load app settings
|
||||
setupNavigationSlider();
|
||||
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package de.baumann.diaspora.utils;
|
||||
|
||||
/**
|
||||
* Created by juergen on 25.03.16. Part of Diaspora WebApp.
|
||||
* solution found on: http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
|
||||
*/
|
||||
|
||||
import android.graphics.Rect;
|
||||
import android.view.View;
|
||||
import android.view.ViewTreeObserver;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class SoftKeyboardStateWatcher implements ViewTreeObserver.OnGlobalLayoutListener {
|
||||
|
||||
public interface SoftKeyboardStateListener {
|
||||
void onSoftKeyboardOpened(int keyboardHeightInPx);
|
||||
void onSoftKeyboardClosed();
|
||||
}
|
||||
|
||||
private final List<SoftKeyboardStateListener> listeners = new LinkedList<>();
|
||||
private final View activityRootView;
|
||||
private int lastSoftKeyboardHeightInPx;
|
||||
private boolean isSoftKeyboardOpened;
|
||||
|
||||
public SoftKeyboardStateWatcher(View activityRootView) {
|
||||
this(activityRootView, false);
|
||||
}
|
||||
|
||||
public SoftKeyboardStateWatcher(View activityRootView, boolean isSoftKeyboardOpened) {
|
||||
this.activityRootView = activityRootView;
|
||||
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
|
||||
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
final Rect r = new Rect();
|
||||
//r will be populated with the coordinates of your view that area still visible.
|
||||
activityRootView.getWindowVisibleDisplayFrame(r);
|
||||
|
||||
final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
|
||||
if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
|
||||
isSoftKeyboardOpened = true;
|
||||
notifyOnSoftKeyboardOpened(heightDiff);
|
||||
} else if (isSoftKeyboardOpened && heightDiff < 100) {
|
||||
isSoftKeyboardOpened = false;
|
||||
notifyOnSoftKeyboardClosed();
|
||||
}
|
||||
}
|
||||
|
||||
public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
|
||||
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
|
||||
}
|
||||
|
||||
public boolean isSoftKeyboardOpened() {
|
||||
return isSoftKeyboardOpened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default value is zero {@code 0}.
|
||||
*
|
||||
* @return last saved keyboard height in px
|
||||
*/
|
||||
public int getLastSoftKeyboardHeightInPx() {
|
||||
return lastSoftKeyboardHeightInPx;
|
||||
}
|
||||
|
||||
public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
|
||||
this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;
|
||||
|
||||
for (SoftKeyboardStateListener listener : listeners) {
|
||||
if (listener != null) {
|
||||
listener.onSoftKeyboardOpened(keyboardHeightInPx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyOnSoftKeyboardClosed() {
|
||||
for (SoftKeyboardStateListener listener : listeners) {
|
||||
if (listener != null) {
|
||||
listener.onSoftKeyboardClosed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue