How get height of Android Keyboard?
I try:
KeyboardView keyboardView = new KeyboardView(_activity.getApplicationContext(), null);
Log.i("","xxx height " + keyboardCustom.mKeyboardView.getHeight());
Log.i("","xxx height " + keyboardCustom.mKeyboardView.getBottom());
But always get 0.
Use OnGlobalLayoutListener for getting Keyboard height or implement above code snippet
chatRootLayout is your xml root layout
pass this rootLayout as parentLayout parameter in checkKeyboardHeight
private void checkKeyboardHeight(final View parentLayout)
{
chatRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
chatRootLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = chatRootLayout.getRootView().getHeight();
int keyboardHeight = screenHeight - (r.bottom);
if (previousHeightDiffrence - keyboardHeight > 50)
{
// Do some stuff here
}
previousHeightDiffrence = keyboardHeight;
if (keyboardHeight> 100)
{
isKeyBoardVisible = true;
changeKeyboardHeight(keyboardHeight);
}
else
{
isKeyBoardVisible = false;
}
}
});
}
Here is changeKeyboardHeight() method
private void changeKeyboardHeight(int height)
{
if (height > 100)
{
keyboardHeight = height;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, keyboardHeight);
yourLayout.setLayoutParams(params);
}
}
Related
I am trying to get android keyboard height with following code.
The popupWindow solution doesn't work in some devices , is there another solution?
parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
parentLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = parentLayout.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom);
previousHeightDiffrence = heightDifference;
if (heightDifference > 100) {
isKeyBoardVisible = true;
changeKeyboardHeight(heightDifference);
} else {
if(emojiKeyboard.getVisibility()==View.INVISIBLE){
emojiKeyboard.setVisibility(View.GONE);
}
isKeyBoardVisible = false;
}
}
});
I would go with insets solution - where rootWindowInsets.systemWindowInsetBottom would include keyboard if present. Take a look at documentation
final Window mRootWindow = getWindow();
ll_main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int screenHeight = ll_main.getRootView().getHeight();
Rect r = new Rect();
View view = mRootWindow.getDecorView();
view.getWindowVisibleDisplayFrame(r);
int keyBoardHeight = screenHeight - r.bottom;
}
});
Full code on this link. this link also help for AdjustNothing.
I have been trying to make a gridview with drag and drop functionality along with one cell of different size. I have already made the the grid drag and drop and its working fine. you can check the code from here
but I want it to be like this and purely dynamic as I will be draging and dropping the other which will be replaced and resized automatically
Updated with new code that accommodates resizing of cells.
Your question refers to GridView but the code you supplied doesn't mention GridView but uses GridLayout instead, so I am assuming that GridLayout is the right layout.
I have put together a demo using a mocked-up layout with one 2x2 tile. I have modified the code that you have supplied to accommodate the 2x2 tile. Other than the code that I added to implement the 2x2 tile, the only other change to MainAcitivity was to the calculateNextIndex method that uses a different way of calculating the index at an (x, y) position. Layouts and the LongPressListener class were also mocked up since they were not supplied.
Here is a video of the demo:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final int ITEMS = 10;
private GridLayout mGrid;
private ScrollView mScrollView;
private ValueAnimator mAnimator;
private Boolean isScroll = false;
private GridLayout.Spec m1xSpec = GridLayout.spec(GridLayout.UNDEFINED, 1);
private GridLayout.Spec m2xSpec = GridLayout.spec(GridLayout.UNDEFINED, 2);
private int mBaseWidth;
private int mBaseHeight;
private int mBaseMargin;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScrollView = (ScrollView) findViewById(R.id.scrollView);
mScrollView.setSmoothScrollingEnabled(true);
mGrid = (GridLayout) findViewById(R.id.grid);
mGrid.setOnDragListener(new DragListener());
final LayoutInflater inflater = LayoutInflater.from(this);
GridLayout.LayoutParams lp;
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float dpiToPx = displayMetrics.density;
View view = inflater.inflate(R.layout.item, mGrid, false);
lp = (GridLayout.LayoutParams) view.getLayoutParams();
mBaseWidth = lp.width;
mBaseHeight = lp.height;
mBaseMargin = lp.rightMargin;
for (int i = 0; i < ITEMS; i++) {
final View itemView = inflater.inflate(R.layout.item, mGrid, false);
final TextView text = (TextView) itemView.findViewById(R.id.text);
text.setText(String.valueOf(i + 1));
itemView.setOnLongClickListener(new LongPressListener());
lp = (i == 0) ? make2x2LayoutParams(itemView) : make1x1LayoutParams(itemView);
mGrid.addView(itemView, lp);
}
}
private GridLayout.LayoutParams make2x2LayoutParams(View view) {
GridLayout.LayoutParams lp = (GridLayout.LayoutParams) view.getLayoutParams();
lp.width = mBaseWidth * 2 + 2 * mBaseMargin;
lp.height = mBaseHeight * 2 + 2 * mBaseMargin;
lp.rowSpec = m2xSpec;
lp.columnSpec = m2xSpec;
lp.setMargins(mBaseMargin, mBaseMargin, mBaseMargin, mBaseMargin);
return lp;
}
private GridLayout.LayoutParams make1x1LayoutParams(View view) {
GridLayout.LayoutParams lp = (GridLayout.LayoutParams) view.getLayoutParams();
lp.width = mBaseWidth;
lp.height = mBaseHeight;
lp.setMargins(mBaseMargin, mBaseMargin, mBaseMargin, mBaseMargin);
lp.rowSpec = m1xSpec;
lp.columnSpec = m1xSpec;
return lp;
}
private int mDraggedIndex;
class DragListener implements View.OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
final View view = (View) event.getLocalState();
int index = calculateNextIndex(event.getX(), event.getY());
View child;
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
mDraggedIndex = index;
break;
case DragEvent.ACTION_DRAG_LOCATION:
if (view == v) return true;
// get the new list index
final Rect rect = new Rect();
mScrollView.getHitRect(rect);
final int scrollY = mScrollView.getScrollY();
if (event.getY() - scrollY > mScrollView.getBottom() - 250) {
startScrolling(scrollY, mGrid.getHeight());
} else if (event.getY() - scrollY < mScrollView.getTop() + 250) {
startScrolling(scrollY, 0);
} else {
stopScrolling();
}
child = mGrid.getChildAt(0);
if (index == 0) {
child.setLayoutParams(make1x1LayoutParams(child));
view.setLayoutParams(make2x2LayoutParams(view));
} else if (mDraggedIndex == 0) {
view.setLayoutParams(make1x1LayoutParams(view));
child.setLayoutParams(make2x2LayoutParams(child));
} else {
child.setLayoutParams(make2x2LayoutParams(child));
view.setLayoutParams(make1x1LayoutParams(view));
}
mGrid.removeView(view);
mGrid.addView(view, index);
break;
case DragEvent.ACTION_DROP:
for (int i = 0; i < mGrid.getChildCount(); i++) {
child = mGrid.getChildAt(i);
child.setLayoutParams(make1x1LayoutParams(child));
}
mGrid.removeView(view);
if (index == 0) {
view.setLayoutParams(make2x2LayoutParams(view));
}
mGrid.addView(view, index);
view.setVisibility(View.VISIBLE);
mGrid.getChildAt(0).setLayoutParams(make2x2LayoutParams(mGrid.getChildAt(0)));
break;
case DragEvent.ACTION_DRAG_ENDED:
if (!event.getResult()) {
view.setVisibility(View.VISIBLE);
}
break;
}
return true;
}
}
private void startScrolling(int from, int to) {
if (from != to && mAnimator == null) {
isScroll = true;
mAnimator = new ValueAnimator();
mAnimator.setInterpolator(new OvershootInterpolator());
mAnimator.setDuration(Math.abs(to - from));
mAnimator.setIntValues(from, to);
mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
mScrollView.smoothScrollTo(0, (int) valueAnimator.getAnimatedValue());
}
});
mAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
isScroll = false;
mAnimator = null;
}
});
mAnimator.start();
}
}
private void stopScrolling() {
if (mAnimator != null) {
mAnimator.cancel();
}
}
private int calculateNextIndexOld(float x, float y) {
// calculate which column to move to
final float cellWidth = mGrid.getWidth() / mGrid.getColumnCount();
final int column = (int) (x / cellWidth);
final float cellHeight = mGrid.getHeight() / mGrid.getRowCount();
final int row = (int) Math.floor(y / cellHeight);
int index = row * mGrid.getColumnCount() + column;
if (index >= mGrid.getChildCount()) {
index = mGrid.getChildCount() - 1;
}
Log.d("MainActivity", "<<<<index=" + index);
return index;
}
private int calculateNextIndex(float x, float y) {
// calculate which column to move to
int index;
for (index = 0; index < mGrid.getChildCount(); index++) {
View child = mGrid.getChildAt(index);
Rect rect = new Rect();
child.getHitRect(rect);
if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
break;
}
}
if (index >= mGrid.getChildCount()) {
// Move into empty cell? Calculate based upon uniform cell sizes.
index = calculateNextIndexOld(x, y);
}
if (index >= mGrid.getChildCount()) {
// Can't determine where to put it? Add it to the end.
index = mGrid.getChildCount() - 1;
}
return index;
}
}
If you work with the demo a little, you will see that it is possible to move tiles such that a 1x1 tile gap is opened up. This may be OK, but the code may need to be reworked a little if not.
You can try :
https://github.com/askerov/DynamicGrid
I hope it can help your problem!
I have a WebView, and load iframe in loadData. When a softkeyboard appears - the Webview change height, but when the softkeyboard disappears - the height of the WebView doesn't change. How to change height of the WebView back to fullscreen?
My Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.wbv);
webView.getSettings().setJavaScriptEnabled(true);
String html = "<html><body><iframe src=\"https://api.motion.ai/webchat/85714?color=EFB426&sendBtn=%D0%9E%D1%82%D0%B2%D0%B5%D1%82%D0%B8%D1%82%D1%8C&inputBox=%D0%9D%D0%B0%D0%BF%D0%B8%D1%88%D0%B8%D1%82%D0%B5%20%D0%B2%D0%B0%D1%88%20%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2F%D0%BE%D1%82%D0%B2%D0%B5%D1%82&token=9b32ca23536d9816b448d2e4c53434dc\" width="100%" height="100%"></iframe></body></html>";
webView.loadData(html,"text/html","UTF-8");
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="app.hr_bot.MainActivity">
<WebView
android:id="#+id/wbv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Screenshots:
EDIT:
So, Now i have something like this:
final EKRAN myEkran = new EKRAN(MainActivity.this);
MAIN = findViewById(R.id.rootLayout);
MAIN.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
MAIN.getWindowVisibleDisplayFrame(r);
int screenHeight = MAIN.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
Log.d( String.valueOf(1) , "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.10) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
Toast.makeText( MainActivity.this , "COOL1 " , Toast.LENGTH_SHORT).show();
ViewGroup.LayoutParams params1 = webView.getLayoutParams();
params1.width = myEkran.W(100); // IN %
params1.height = myEkran.H(45); // IN %
webView.setLayoutParams(params1);
webView.requestLayout();
}
else {
Toast.makeText( MainActivity.this , "COOL2 " , Toast.LENGTH_SHORT).show();
// keyboard is closed
ViewGroup.LayoutParams params1 = webView.getLayoutParams();
params1.width = myEkran.W(100); // IN %
params1.height = myEkran.H(92); // IN %
webView.setLayoutParams(params1);
webView.requestLayout();
}
}
});
webView = (WebView) findViewById(R.id.wbv);
webView.getSettings().setJavaScriptEnabled(true);
String html = "<html><body><iframe src=\"https://api.motion.ai/webchat/85714?color=EFB426&sendBtn=%D0%9E%D1%82%D0%B2%D0%B5%D1%82%D0%B8%D1%82%D1%8C&inputBox=%D0%9D%D0%B0%D0%BF%D0%B8%D1%88%D0%B8%D1%82%D0%B5%20%D0%B2%D0%B0%D1%88%20%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2F%D0%BE%D1%82%D0%B2%D0%B5%D1%82&token=9b32ca23536d9816b448d2e4c53434dc\" width=\"100%\" height=\"100%\"></iframe></body></html>";
webView.loadData(html,"text/html","UTF-8");
Detect keyboard isHihhen or visible :
// MAIN is root view see in design window (also enter id for root view ):
View MAIN;
...
//########################
// Put this intro OnCreate
//########################
MAIN = findViewById(R.id.main);
MAIN.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
MAIN.getWindowVisibleDisplayFrame(r);
int screenHeight = MAIN.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
Log.d( String.valueOf(1) , "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
Toast.makeText( context_pass , "COOL1 " , 1);
}
else {
Toast.makeText( context_pass , "COOL2 " , 1);
// keyboard is closed
}
}
});
If your webview is deformed use this code after keyboard hides:
ViewGroup.LayoutParams params1 = YOUR_CONTROL.getLayoutParams();
params1.width = MY_EKRAN.W(100); // IN %
params1.height = MY_EKRAN.H(100); // IN %
YOUR_CONTROL.setLayoutParams(params1);
OR
runOnUiThread(new Runnable() {
public void run() {
// runs on UI thread
YOUR_CONTROL.getLayoutParams().height = ( MY_EKRAN.HEIGHT()/100*10);
}
});
DEF :
//global
EKRAN MY_EKRAN;
// in onCreate
MY_EKRAN = new EKRAN( context_pass );
Class for diametric dimensions and positions (add like new file - new java class) :
import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.TypedValue;
/**
* Created by nikola on 10/17/16.
*/
//##############################################
// SCREEN - EKRAN CLASS
//##############################################
public class EKRAN {
DisplayMetrics dm = new DisplayMetrics();
Point size_ = new Point();
static int width;
static int height;
EKRAN(Context CONTEXT_) {
dm = CONTEXT_.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
height = dm.heightPixels;
width = dm.widthPixels;
}
public static int WIDTH() {
return width;
}
public static int HEIGHT(){
return height;
}
public int W( int PER_ ){
return width/100*PER_;
}
public int H( int PER_ ){
return height/100*PER_;
}
//////////////////
//extras
/////////////////
public int GET_PIX_FROM_DP ( float DP_VALUE )
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP_VALUE , dm );
}
public int GET_PIX_FROM_DP2 ( float DP_VALUE )
{
float res = DP_VALUE * ( dm.ydpi / 160f);
return (int) res;
}
}
I have used the below code for getting the keyboard height.
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect rect = new Rect();
view.getWindowVisibleDisplayFrame(rect);
int screenHeight = view.getRootView().getHeight();
int keyboardHeight = screenHeight - rect.bottom;
if(keyboardHeight != 0){
if(orientation == Configuration.ORIENTATION_LANDSCAPE)
AppConfig.landscapeKeyboardHeight = keyboardHeight;
else if(orientation == Configuration.ORIENTATION_PORTRAIT)
AppConfig.portraitKeyboardHeight = keyboardHeight;
}
}
});
But this gives height only when the app opens keyboard for the first time at least. I want the height of keyboard even before the it gets open for the first time. Is there any way of doing this? Thanks in advance...
I had the same problem before for getting the height of keyboard for showing some dialog boxes. You can use the method below to solve it.
rootView = getWindow().getDecorView().getRootView();
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
int screenHeight = rootView.getHeight();
int keyboardHeight = screenHeight - (rect.bottom - rect.top);
if(keyboardHeight > screenHeight / 3){
keyboardActive = true;
Log.d("Keyboard", "Active");
}
else{
keyboardActive = false;
Log.d("Keyboard", "Not Active");
}
}
});
Just came across this app RetailMeNot Coupons. The list view used is custom and looks pretty cool. Initially, the list show smaller image of a child-item. As the user scrolls up, the child item expands fully to reveal a bigger image.
Out of curiosity, I plan to build a similar list-view in a test app.
I am sure this is a custom list-view, has anyone seen a library or implementation of a similar list-view?
Look at this library
https://github.com/ksoichiro/Android-ObservableScrollView
May be some customization on this library can help you.
I have tried directly working with listviewscroll.
Is not perfect but is a start
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
for (int j=0; j<visibleItemCount; j++) {
if(absListView.getChildAt(j).getClass() == FrameLayout.class) {
FrameLayout item = (FrameLayout) absListView.getChildAt(j);
String result = "";
if (item != null) {
RelativeLayout relative1 = (RelativeLayout) item.getChildAt(0);
ImageView promoImage = (ImageView) relative1.getChildAt(0);
View promoOverlay = (View) relative1.getChildAt(1);
RelativeLayout relative2 = (RelativeLayout) relative1.getChildAt(2);
ImageView brandImage = (ImageView) relative2.getChildAt(0);
TextView promoText = (TextView) relative2.getChildAt(1);
item.setLayoutParams(new AbsListView.LayoutParams(item.getWidth(), (int) minHeight));
if (item.getTop() < maxHeight) {
if (item.getTop() >= 0) {
float topPercent = item.getTop() / maxHeight;
float newHeight = minHeight + ((maxHeight - minHeight) * (1 - topPercent));
if (newHeight <= maxHeight) {
item.setLayoutParams(new AbsListView.LayoutParams(item.getWidth(), (int) newHeight));
promoOverlay.setAlpha(topPercent / 2);
} else {
item.setLayoutParams(new AbsListView.LayoutParams(item.getWidth(), (int) maxHeight));
if (item.getTop() > 0) {
promoOverlay.setAlpha(0.5f);
} else {
promoOverlay.setAlpha(0f);
}
}
} else {
float topPercent = (-item.getTop()) / maxHeight;
float newHeight = maxHeight - ((maxHeight - minHeight) * (topPercent));
if (newHeight >= minHeight) {
item.setLayoutParams(new AbsListView.LayoutParams(item.getWidth(), (int) newHeight));
promoOverlay.setAlpha(topPercent / 2);
} else {
item.setLayoutParams(new AbsListView.LayoutParams(item.getWidth(), (int) minHeight));
}
promoOverlay.setAlpha(0);
}
} else {
if (item.getTop() > 0) {
item.setLayoutParams(new AbsListView.LayoutParams(item.getWidth(), (int) minHeight));
promoOverlay.setAlpha(0.5f);
}
}
}
}
}
}
});
}