android - Force EditText to lose focus when back pressed -


i'm trying force edittext control lose focus when user presses button hide keyboard. there many questions similar already, after several hours, haven't been able make work.

first, little bit of context. have listview custom items. each item has several textviews , 1 edittext. have aftertextchanged() method saving edited values. have style set highlight field if has focus. unfortunately, more obvious edittext doesn't lose focus when hide (soft) keyboard, , think it's confusing. edittext not focused if there's no keyboard.

the solution seemed reasonable override onbackpressed() in activity described here. unfortunately, doesn't appear method being called. i.e. field still focused, , breakpoint in function doesn't fire.

similarly, onkeyup() listener on activity doesn't fire, , xamarin doesn't appear support onkeyup handler edittext control.

i'm not trying suppress keyboard on creation, or anything, using of invisible control tricks don't either.

it's obvious lot of people have problem. i'm sure 1 of has solved it! can please share solution?

thank much! -karen

p.s. not need know how hide keyboard. need take action when user hides keyboard button. :)

in experience onbackpressed() (at least default @override 1 in activity) not fire when pushing button close keyboard. far know fire when press initiate finish() on current activity.

below kind of "hacky" way know when keyboard shown/hidden monitoring change in view size. must set activity android:windowsoftinputmode="adjustresize" in androidmanifest.xml.

final view activityrootview = findviewbyid("your main view");     activityrootview.getviewtreeobserver().addongloballayoutlistener(new ongloballayoutlistener() {     @override     public void ongloballayout() {         rect r = new rect();         //r populated coordinates of view area still visible.         activityrootview.getwindowvisibledisplayframe(r);          int heightdiff = activityrootview.getrootview().getheight() - (r.bottom - r.top);         if (heightdiff > 100) { // if more 100 pixels, keyboard...           //keyboard shown           }         if(heightdiff <= 100) {             //keybaord not shown         }      }     }); 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -