android - Regain Focus on EditText when User Clicks It -
i have activity wiht edittext, , want following behaviour 1 of edittext: on activity starting-up, edittext populated text. should not show cursor, has no focus , not show keyboard (so user can read text , scroll , down). when user clicks/touches edittext, gain focus, show cursor , show keyboard (so user can start editing).
the behaviour @ starting working ok, cannot "start editing" behaviour work when user clicks on edittext.
here code:
activity_main.xml: ...
... <edittext android:id="@+id/notepad" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.5" android:background="@android:color/transparent" android:textsize="@dimen/font_large" android:textstyle="normal" android:textcolor="@color/appprimarytext" android:textcolorhint="@color/appaccentcolour" android:gravity="top" android:scrollbars="vertical" /> ... <view android:layout_width="match_parent" android:layout_height="@dimen/activity_margin"></view> </linearlayout>
mainactivity.java:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mnoteedittext = (edittext) findviewbyid(r.id.notepad); final mainactivity mainactivity = this; mnoteedittext.setfocusable(false); mnoteedittext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { showtoastmessage("here", false); mnoteedittext.setfocusable(true); mnoteedittext.requestfocus(); mnoteedittext.setcursorvisible(true); showkeyboard(mnoteedittext); } }); } public void showkeyboard(edittext edittext) { inputmethodmanager inputmethodmanager = (inputmethodmanager) getsystemservice(activity.input_method_service); inputmethodmanager.showsoftinput(edittext, inputmethodmanager.show_implicit); }
the activity's manifest entry is:
<activity android:name=".ui.mainactivity" android:label="@string/app_name" android:launchmode="singletask" android:windowsoftinputmode="statealwayshidden|adjustpan" > <!--main launcher.--> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity>
the problem is, onclick method firing (i can see toast), other lines have no effect on edittext (mnoteedittext).
thanks.
so here best solution of problem. declare manifest file this-
<activity android:name=".ui.mainactivity" android:label="@string/app_name" android:launchmode="singletask" android:windowsoftinputmode="statehidden|adjustpan" > <!--main launcher.--> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity>
in xml define edittext this
<edittext android:id="@+id/notepad" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.5" android:background="@android:color/transparent" android:textsize="@dimen/font_large" android:textstyle="normal" android:textcolor="@color/appprimarytext" android:textcolorhint="@color/appaccentcolour" android:gravity="top" android:cursorvisible="false" android:scrollbars="vertical" />
in mainactivity edit protected void oncreate(bundle savedinstancestate) method this-
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mnoteedittext = (edittext) findviewbyid(r.id.notepad); mnoteedittext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (view.getid() == mnoteedittext.getid()) { mnoteedittext.setcursorvisible(true); } } }); }
that's :)
Comments
Post a Comment