I have a layout in which I have autoComplete edit text with some suggestions.
The image below is its normal behaviour until keyboard slides in.
after the keyboard comes the dropdown goes above the field which hides my TO: field. As shown below.
What should I do to get the dropdown below even when the keyboard slides up.
I want the result to be like this.
Thanks...
I resolved this by hiding the upper layout when key board is shown and focus is on this edit text. As soon as the keyboard is slided_out the upper layout is visible again which bring back the original screen.
For detecting the keyboard slide_in/slide_out i used something like this.
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its
// probably a keyboard...
if (searchText.isFocused()) {
//keyboard is shown
}
} else {
if (searchText.isFocused()) {
//Keyboard is hidden.
}
}
}
});
Related
I want to show a button when the virtual keyboard is open and hide this button if the virtual keyboard visibility is off.But I could not find any listeners to perform this activity.
Anybody knows how to do this?
As found here, you'll need to instantiate the SoftkeyBoard and add a listener.
/*
Somewhere else in your code
*/
RelativeLayout mainLayout = findViewById(R.layout.main_layout); // You must use your root layout
InputMethodManager im = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
/*
Instantiate and pass a callback
*/
SoftKeyboard softKeyboard;
softKeyboard = new SoftKeyboard(mainLayout, im);
softKeyboard.setSoftKeyboardCallback(new SoftKeyboard.SoftKeyboardChanged()
{
#Override
public void onSoftKeyboardHide()
{
// Code here
}
#Override
public void onSoftKeyboardShow()
{
// Code here
}
});
/*
Open or close the soft keyboard programatically
*/
softKeyboard.openSoftKeyboard();
softKeyboard.closeSoftKeyboard();
/*
SoftKeyboard can catch keyboard events when an EditText gains focus and keyboard appears
*/
/* Prevent memory leaks:
*/
#Override
public void onDestroy()
{
super.onDestroy();
softKeyboard.unRegisterSoftKeyboardCallback();
}
In his post, you will also find more information about bug fixes and possible problems.
add onGlobalLayoutListener to your parent view of activity/fragment and make your button visibility accordingly
final View parentView= findViewById(R.id.myrootview);
parentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = root.getRootView().getHeight() - root.getHeight();
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
if(heightDiff <= contentViewTop){
//Soft KeyBoard Hidden---button visible
}else{
//Soft KeyBoard Shown---button hide
}
}
});
There is no direct event for keyboard open and close. but you can create observer on your full layout and then display buttons or whatever you want to do.
For Observer code check this - Hide part of activity_main.xml if keyboard is open (Android)
I have an activity that is basically a long form of entry fields.
On each row, I want to show a TextView to serve as hint text just below each EditText and I want the TextView to remain visible at all times when the user is entering data. Unfortunately, the soft keyboard obscures the hint text and always positions itself immediately below the EditText. Is there any technique that will allow the TextView below the EditText to also be visible when the soft keyboard appears and the contents are adjusted (via windowSoftInputMode=adjustResize|adjustPan), without having the user scroll ?
Vishavjeet got me on the right track in suggesting I scrolldown to reveal the view that may be overlapped by the keyboard. Below is a function similar to what I used to solve the problem. It can be called when the EditText above the TextView receives focus:
// View targetView; // View that may be hidden by keyboard
// ScrollView scrollContainerView; // Scrollview containing hiddenView
//
void assureViewVisible (View targetView, ScrollView, scrollContainerView) {
Window rootWindow = activity.getWindow();
Rect rMyView = new Rect();
View rootview = rootWindow.getDecorView();
rootview.getWindowVisibleDisplayFrame(rMyView); // Area not taken up by keyboard
int subTextPos[] = new int[2];
targetView.getLocationInWindow(subTextPos); // Get position of targetView
int subTextHt = targetView.getHeight(); // Get bottom of target view
if ((subTextPos[1]+subTextHt) > rMyView.bottom) { // Is targetView at all obscured?
int scrollBy = (subTextPos[1]+subTextHt) - rMyView.bottom + 10; // add a small bottom margin
mMeasurementViewScrollView.smoothScrollBy(0, scrollBy); // Scroll to subtext
}
}
EDIT:
By understanding the problem more deeply, I think that you should add scroll programatically when user clicks on the Edittext. Here is the code to do that:
private final void focusOnView()
{
new Handler().post(new Runnable()
{
#Override
public void run()
{
your_scrollview.scrollTo(0, your_EditBox.getBottom());
}});
}
From my personal experience I think there is not such way to do that. The thing you can do is place the hint textview toRightOf the editext. Or Use modern Approach by using a Hint Placeholder on Edittext:
In XML, it's simply android:hint="someText"
Programatically you can use edittext.setHint(int);
pass R.string.somestring in above method.
Having Problem with Edittext and SoftKeyboard.
now if i click on edittext(hello text) to modify it (means when it gains focus bottom gridview is poping up see second image)
so how to change this behaviour of poping up the bottom views?
Any Help will be greately appreciated.
Thanks in advance.
you can open dialog and in dialog add EditText and on ok button you can change that text according to text added in dialog edittext.
android:windowSoftInputMode="adjustResize" property might be squeezing your background
use android:windowSoftInputMode="stateVisible|adjustPan" instead.
View activityRootView;
activityRootView = (LinearLayout) findViewById(R.id.root); // main layout id
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
if (heightDiff > 100) {
// if more than 100 pixels, its
// hide your below layout like layout1.setVisibility(View.GONE);
} else {
// visible your below layout like layout1.setVisibility(View.VISIBLE);
}
}
});
I have a View say anchor and a PopupWindow pop. pop is shown with showAsDropDown when I click a button on anchor.
What I want is to show pop automatically at the first time anchor comes to screen. So I override onAttachedToWindow and add a onGlobalLayoutListener and show pop in it. see below:
private boolean mFirstRun = true;
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if(mFirstRun) {
mFirstRun = false;
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
showPopup();
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
}
The result is, only the first time (see above) pop will stay right above anchor. I have confirmed the width and height of pop is correct. when the button click triggers, pop is shown at the proper place.
After spending some time I found the problem is in onGlobalLayoutListener anchor.getLocationOnScreen has a larger value than the final value so there's no enough space for pop below it.
How can I fix this? and why the screen location is incorrect in that listener?
I'd like to get notified when the virtual keyboard is shown / dismissed. This does not seem to be possible, other than by using some layout resizing listener tricks:
How to check visibility of software keyboard in Android?
My activity has a single EditText. I could make it not have focus at activity startup, then add a focuschangelistener to it. When it gains focus, I can do my onVirtualKeyboardShown() stuff. If I could just listen for the back key being pressed in the EditText, I could then interpret that as the virtual keyboard being hidden. Something like:
EditText et = ...;
et.setOnFocusChangedListener(new OnFocusChangedListener() {
public void onFocusChanged(boolean focused) {
if (focused) {
// virtual keyboard probably showing.
}
}
});
et.setKeyListener(new KeyListener() {
public void onKeyPressed(int code) {
if (code == BACK_KEY) [
if (et.isFocused()) {
// virtual keyboard probably hiding.
// lose focus to set up for next time.
et.setFocused(false);
}
}
}
});
Seems like any approach is fraught with problems given all the differences between virtual keyboards, then we also have to deal with physical keyboards too,
Thanks
This does not seem to be possible, other than by using some layout resizing listener tricks
Correct.
I want to be notified so I can show my own suggestions ribbon above the virtual keyboard.
Not all Android devices use virtual keyboards. Some have physical keyboards. Since you need to support both types of devices, you need to come up with a UI design that does not assume that everyone has a virtual keyboard.
// Catch the keyboard height
final LinearLayout masterView = (LinearLayout) findViewById(R.id.conversation_prent);
masterView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect r = new Rect();
masterView.getWindowVisibleDisplayFrame(r);
int result = 0;
int resourceId = getResources().getIdentifier(
"status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(
resourceId);
}
int heightDiff = masterView.getRootView().getHeight()
- masterView.getHeight();
heightDiff = heightDiff - (ab.getHeight() + result);
Log.e("Keyboard Size", "Size: " + heightDiff);
if (heightDiff > 200) {
// The keyboard is shown
} else {
// The keyboard is hidden
}
}
});
if your app is running on android < 3 (HoneyComb) delete the parts of the code that are related to the actionbar.