Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
siebeprojects committed Sep 14, 2016
1 parent 87bb389 commit 3fbfd22
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@

package com.siebeprojects.samples.keyboardheight;

import android.content.Context;
import android.content.SharedPreferences;
import android.app.Activity;

import android.util.Log;
import android.content.res.Configuration;
import android.content.res.Resources;

import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;

import android.app.Activity;

import android.util.Log;

import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
Expand All @@ -45,9 +43,14 @@
*/
public class KeyboardHeightProvider extends PopupWindow {

/** The keyboard height observer */
private final static String TAG = "sample_KeyboardHeightProvider";

/** The keyboard height observer */
private KeyboardHeightObserver observer;

/** The minimum height of the navigation bar */
private final static int NAVIGATION_BAR_MIN_HEIGHT = 100;

/** The cached landscape height of the keyboard */
private int keyboardLandscapeHeight;

Expand Down Expand Up @@ -92,7 +95,7 @@ public KeyboardHeightProvider(Activity activity, View parentView, int keyboardPo
this.keyboardLandscapeHeight = keyboardLandscapeHeight;

LayoutInflater li = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
popupView = li.inflate(R.layout.keyboardheight_popupwindow, null, false);
this.popupView = li.inflate(R.layout.popupwindow, null, false);
setContentView(popupView);

setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Expand Down Expand Up @@ -170,17 +173,14 @@ public int getLandscapeHeight() {
*/
public int getScreenOrientation() {

Display getOrient = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(size);
int orientation = Configuration.ORIENTATION_UNDEFINED;

if (getOrient.getWidth() == getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_SQUARE;
if (size.x > size.y) {
orientation = Configuration.ORIENTATION_LANDSCAPE;
} else {
if (getOrient.getWidth() < getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_PORTRAIT;
} else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
orientation = Configuration.ORIENTATION_PORTRAIT;
}
return orientation;
}
Expand Down Expand Up @@ -246,8 +246,7 @@ else if (rect.bottom + navigationBarHeight == screenHeight) {
this.navigationBarVisible = true;
handleKeyboardClosed();
}
else if ((keyboardHeight = calculateKeyboardHeight(rect, statusBarHeight, navigationBarHeight, screenHeight)) < 100) {
// navigation bar must be larger than 100 pixels.
else if ((keyboardHeight = calculateKeyboardHeight(rect, statusBarHeight, navigationBarHeight, screenHeight)) < NAVIGATION_BAR_MIN_HEIGHT) {
this.navigationBarVisible = false;
handleKeyboardClosed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

package com.siebeprojects.samples.keyboardheight;

import android.widget.TextView;
import android.view.View;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

Expand All @@ -40,17 +43,28 @@ public final class MainActivity extends AppCompatActivity implements KeyboardHei
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keyboardheight_activity);
setContentView(R.layout.activity);

View parentView = findViewById(R.id.parentview);
keyboardHeightProvider = new KeyboardHeightProvider(this, parentView, 0, 0);

keyboardHeightProvider = new KeyboardHeightProvider(this, findViewById(R.id.parentview), 0, 0);
// start the keyboard height provider after
// the view is initialized and after onResume of this Activity
parentView.post(new Runnable() {
public void run() {
keyboardHeightProvider.start();
}
});
}


/**
* {@inheritDoc}
*/
@Override
public void onPause() {
super.onPause();
keyboardHeightProvider.setKeyboardHeightObserver(null);
}

/**
Expand All @@ -59,15 +73,16 @@ public void onPause() {
@Override
public void onResume() {
super.onResume();
keyboardHeightProvider.setKeyboardHeightObserver(this);
}

/**
* {@inheritDoc}
*/
@Override
public void onPostResume() {
super.onPostResume();
keyboardHeightProvider.start();
public void onDestroy() {
super.onDestroy();
keyboardHeightProvider.close();
}

/**
Expand All @@ -76,5 +91,7 @@ public void onPostResume() {
@Override
public void onKeyboardHeightChanged(int height) {
Log.i(TAG, "onKeyboardHeightChanged: " + height);
TextView tv = (TextView)findViewById(R.id.height_text);
tv.setText(Integer.toString(height));
}
}
60 changes: 60 additions & 0 deletions app/src/main/res/layout/activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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"
android:id="@+id/parentview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
android:id="@+id/info_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="@android:color/black"
android:textSize="16dp"
android:text="@string/info">

</TextView>

<EditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
>
</EditText>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">

<TextView
android:id="@+id/height_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textColor="@android:color/black"
android:textSize="16dp"
android:text="@string/height">

</TextView>

<TextView
android:id="@+id/height_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16dp"
android:text="0">

</TextView>

</LinearLayout>

</LinearLayout>
15 changes: 0 additions & 15 deletions app/src/main/res/layout/keyboardheight_activity.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popuplayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

<resources>
<string name="app_name">KeyboardHeight</string>
<string name="info">CLick the EditText to open keyboard</string>
<string name="height">Height (pixels):</string>
</resources>
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ allprojects {
repositories {
jcenter()
}

gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}

0 comments on commit 3fbfd22

Please sign in to comment.