Saturday 15 January 2011

android - Prevent EditText from focussing after rotation -



android - Prevent EditText from focussing after rotation -

i've implemented simple edittext provides done button hide keyboard , upon rotating landscape not nowadays total screen dialog edittext. there's problem. when tap done dismiss keyboard rotate device landscape, keyboard appears. if dismiss 1 time again rotate portrait keyboard appears yet again.

how can preserve keyboard visibility state upon rotation - if hidden before rotation don't nowadays after rotation, if visible before rotation nowadays after rotation?

<edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/edittext1" android:inputtype="text" android:imeoptions="flagnofullscreen|actiondone" />

i tried setting android:descendantfocusability="beforedescendants" , android:focusableintouchmode="true" in parent container (relativelayout), didn't impact behavior.

there 2 options.. in oncreate() method seek these options

option 1.

this.getwindow().setsoftinputmode(windowmanager.layoutparams.soft_input_state_always_hidden);

option 2

edittext.clearfocus();

this alternative sets focus first focusable view in activity.

edit:

option 2 not work if edittext self first focusable view in activity clearfocus sets focus first focusable view in activity.

usage of alternative 1:

public class myactivity extends activity { edittext edittext1; boolean iskeyboardopen=false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my); edittext1 = (edittext) findviewbyid(r.id.edittext1); if(savedinstancestate!=null &&(savedinstancestate.getboolean("iskeyboardopen",false))) this.getwindow().setsoftinputmode(windowmanager.layoutparams.soft_input_state_always_visible); else this.getwindow().setsoftinputmode(windowmanager.layoutparams.soft_input_state_always_hidden); final view activityrootview = findviewbyid(r.id.root_layout); activityrootview.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.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... iskeyboardopen=true; }else iskeyboardopen=false; } }); } protected void onsaveinstancestate(final bundle bundle) { super.onsaveinstancestate(bundle); bundle.putboolean("iskeyboardopen",iskeyboardopen); } }

android rotation keyboard android-edittext

No comments:

Post a Comment