I want to auto display a listview with same height of keyboard of device when user goes to that particular activity. For this, I am calling three methods that are, showKeyboard(), getKeyboardHeight() and then hideKeyboard() and then giving height to that listview and showing that listview. But the problem is once I call showKeyboard(), calculate height and then hideKeyboard(), the keyboard doesn't hide and remains visible. Also I'm getting height as 0. I cannot display that listView. Is there any another process to get height of keyboard or any correction in below code ? See the code below :
showKeyboard() method -
private void showKeyboard() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editChatBox, 0);
}
getKeyboardHeight() method -
public int getKeyboardHeight() {
final View rootview = this.getWindow().getDecorView();
linearChatLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect r = new Rect();
rootview.getWindowVisibleDisplayFrame(r);
int screenHeight = rootview.getRootView().getHeight();
int newHeight = screenHeight - (r.bottom - r.top);
if (newHeight > heightOfKeyboard) {
heightOfKeyboard = screenHeight
- (r.bottom - r.top);
// heightOfKeyboard = heightDiff;
}
Log.d("Keyboard Size", "Size: " + heightOfKeyboard);
}
});
return heightOfKeyboard;
}
hideKeyboard() method -
private void hidekeyBoard() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editChatBox.getWindowToken(), 0);
}
Inside onCreate() method -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_new_layout);
ArrayAdapter<String> chatQueAdapter = new ArrayAdapter<>(this,
R.layout.chat_que_row, R.id.textChatQue, queArrays);
myListView.setAdapter(chatQueAdapter);
showKeyboard();
heightOfKeyboard = getKeyboardHeight();
hidekeyBoard();
myListView.getLayoutParams().height = heightOfKeyboard;
myListView.setVisibility(View.VISIBLE);
}
Next, go to “Preferences.” In the “Layout” section, select “Keyboard Height.” There are a bunch of different heights to choose from. It's a good idea to start with one step up from where you're currently at.
To know about the keyboard height, you can just check for the bottom property of viewInsets , when the keyboard is onscreen, this will hold the height of keyboard else zero. Note: The bottom property may have value even if some other system ui obscures the flutter ui from bottom. Hope that helps!
Open Preferences. Tap the Keyboard Height option. You'll see seven different options ranging from "Extra-short" to "Extra-tall." The default is "Normal." Tap the option you prefer. Your selection takes effect immediately.
You can scroll up without hiding the keyboard. If you set android:windowSoftInputMode="adjustResize" , the top portion of the activity (Toolbar/Appbar) is maintained with EditText pushed to above the keyboard.
I added global layout on root view and I was able to resize list view to same size as Keyboard.
The Pink Box is the resized ListView . I have tested this sample app on 2 Samsung devices and it work fine on Both.
Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/edt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="text"
android:maxLines="1" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:visibility="gone" />
</LinearLayout>
Activity class
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.fet.minebeta.R;
public class ListActivity extends AppCompatActivity {
PagerAdapter adapterViewPager;
ViewPager viewPager;
private int heightDiff;
private ListView myListView;
private EditText editChatBox;
private boolean wasOpened;
private final int DefaultKeyboardDP = 100;
// Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff
private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
myListView = (ListView) findViewById(R.id.list);
editChatBox = (EditText) findViewById(R.id.edt);
//Listen for keyboard height change
setKeyboardListener();
// InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
// editChatBox.requestFocus();
// inputMethodManager.showSoftInput(editChatBox, 0);
//
// if (getCurrentFocus() != null) {
// inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
// inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
// }
//
// getWindow().setSoftInputMode(
// WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
// );
}
public final void setKeyboardListener() {
final View activityRootView = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private final Rect r = new Rect();
@Override
public void onGlobalLayout() {
// Convert the dp to pixels.
int estimatedKeyboardHeight = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics());
// Conclude whether the keyboard is shown or not.
activityRootView.getWindowVisibleDisplayFrame(r);
heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
boolean isShown = heightDiff >= estimatedKeyboardHeight;
if (isShown == wasOpened) {
Log.d("Keyboard state", "Ignoring global layout change...");
return;
}
wasOpened = isShown;
if (isShown) {
//Set listview height
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = heightDiff;
myListView.setLayoutParams(params);
myListView.requestLayout();
myListView.setVisibility(View.VISIBLE);
Toast.makeText(ListActivity.this, "KeyBoard Open with height " + heightDiff +
"\n List View Height " + myListView.getHeight(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
Again this is just quick demo you can enhance things according to your need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With