Desidero visualizzare automaticamente un listview con la stessa altezza della tastiera del dispositivo quando l'utente accede a quella particolare attività. Per questo, sto chiamando tre metodi che sono, showKeyboard(), getKeyboardHeight() e quindi hideKeyboard() e quindi dando l'altezza a tale listview e mostrando tale listview. Ma il problema è una volta che chiamo showKeyboard(), calcoli l'altezza e poi hideKeyboard(), la tastiera non si nasconde e rimane visibile. Inoltre sto ottenendo altezza come 0. Non riesco a visualizzare quel listView. C'è qualche altro processo per ottenere l'altezza della tastiera o qualsiasi correzione nel codice sottostante? Vedere il codice qui sotto:Come ottenere l'altezza della tastiera quando l'attività viene creata in Android
showKeyboard metodo() -
metodo getKeyboardHeight() - Metodo
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() -
private void hidekeyBoard() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editChatBox.getWindowToken(), 0);
}
Metodo
All'interno onCreate() -
@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);
}
Non è possibile ottenere altezza della tastiera prima della sua prima apertura. Per la prima volta è possibile utilizzare il valore predefinito (ad esempio 230 dpi) e quindi regolare il valore. E imposta windowSoftInputMode per adjustResize. –
@gabber ma sto aprendo la tastiera usando il metodo showKeyboard() e quindi calcolando l'altezza e nascondendola usando il metodo hideKeyboard(). Sto ottenendo altezza zero e la tastiera rimane aperta. – Ruchir
per essere in grado di calcolare la dimensione in modo tale, è necessario impostare windowSoftInputMode per regolare Ridimensiona –