Removing autofocus from EditText and regaining focus ? - android

I have an EditText and a set of Buttons in my layout.
To remove autofocus from EditText i am using a dummy LinearLayout as told in some answers at this site.
I want the edit text to get focus on button press and the virtual keyboard being shown.
but on 1st button press the edit text gets focus but virtual keyboard is shown only after button is pressed again.well the problem is bit different but any idea? This is what i m doing on button click:
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mgr.showSoftInput(main_edt,InputMethodManager.SHOW_FORCED);
main_edt.requestFocus();
}
});
This is the dummy linear layout:
<LinearLayout
android:focusable="true"
android:id="#+id/dummyll"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px" />
If i dont write this dummy linear layout or make the focusable tags as false the keyboard shows on 1st click only.

Let's try to set the below properties in your xml itself for EditText
android:focusableInTouchMode="true"
android:cursorVisible="false".
I hope it may solve your problem,if you suppose want to hide the softkeypad itself at launching activity means use the below code in your activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Add attribute android:windowSoftInputMode="stateHidden" to the activity in your manifest. This way, the soft keyboard will no be shown, when you start the activity.

Related

Why when I use setClickable(true) it's not show the action of click

Actually, I use a ListView and when I use setClickable(false) I have the animation as if I clicked on a button you see? The animation that shows you click. Which is not normal, I think, basic.
And when I use setClickable(true) I no longer have the animation, as well as if I use
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
And i would like to use the OnClickListener but I think it would be better for the user to see that he can click, so to have the animation when you click.
So, I'd like to see when the user clicks on an item in the list, it does the action I want (I'll add that later) but let's imagine a Toast but it displays the effect as if you click on a button. The effect i got if i use setClickable(false) (the default setting).
That's the ripple effect !
In the row's layout of the ListView just add:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
This will add the Ripple effect. If you want to show it on top of the other views, use the forground attribute:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
add this foreground:?attr/selectableItemBackground to your view attribute, it should work

clearFocus() on empty editText is not working in Android

I tried to remove focus from empty editText but it isn't working correctly.
I called clearFocus() on edittext ,and then I placed break point at my onFocusChanged() function call.
Here is what happened:
onFocusChanged() called 4 times with the focused parameters values false,true,false,true.
What I thought was that onFocusChanged() must be called only once (with focused = false)
Sorry for my bad English. Any help would be appreciated.
Thanks
In xml, make parent layout
android:focusable="true"
android:focusableInTouchMode="true"
and then call clearFocus on edit text and then call parent request focus
mFragmentBase.editText.clearFocus();
mFragmentBase.parentLayout.requestFocus();
This is happening because your EditText is the first focusable view.
From the docs,
Note: When a View clears focus the framework is trying to give focus
to the first focusable View from the top. Hence, if this View is the
first from the top that can take focus, then all callbacks related to
clearing focus will be invoked after which the framework will give
focus to this view.
You can try setting a dummy focusable view above the EditText to clear the focus from it.
Instead of clearing the focus from EditText create an empty view at the top of the layout
<LinearLayout
android:id="#+id/focusableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"/>
and declare a utility function to hide the keyboard like this
fun hideKeyboard(activity: Activity) {
val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity.currentFocus?.windowToken, 0)
}
Finally when you need to clear focus just call
focusableLayout.requestFocus()
hideKeyboard(currentActivity)

Android remove focus from all Views

This is a fairly simple question but I can't believe all answers I found were not working.
I have a layout with two EditText and the bottom of the window is just some remaining blank space. I'd like each EditText to loose focus when the user is clicking in the blank part of the window. So my guess is that I have to put something in the OnTouchListener of the root View:
rootView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Put something here
}
});
I have tried several things:
Requesting focus from the layout View as suggested somewhere was in fact giving focus to one of the EditTexts.
I also tried setting setFocusable for both EditTexts to false but I was unable to put them back to true afterwards.
I also tried the simple rootView.clearFocus(), but it was also giving back the focus to one EditText.
EDIT: My EditText boxes are multilines so I can't use a 'Done' keyboard button since I need the return one to be present.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
LinearLayout llRootView = findViewBindId(R.id.ll_root_view);
llRootView.clearFocus();
I use this when already finished update profile info and remove all focus from EditText in my layout
====> Update: In parent layout content my EditText add line:
android:focusableInTouchMode="true"
In Kotlin:
view?.clearFocus()
In Java:
getView().clearFocus();

Prevent Softkeyboard from opening on EditText Focus

I need to prevent softkeyboard from showing up when Edit Text gains focus or if user taps on the edit text because user has to use a barcode scanner as an input method. But I need is open the keyboard when user clicks/taps a specific button.
I have tried focusableInTouchMode property and it works fine in preventing the softkeyboard from popping but it won't let the edit text gain focus.
Any hint on how to achieve this?
myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// showMyDialog();
}
});
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// showMyDialog();
}
}
});
Try this.... Just return null or anything on onclicklistner..
Njoy.. :)
And m using it so don't tell me its not working.. LOL..
I have one condition that if user clicks on edittext then it ll pops DatePicker Dialog and its working.
So njoy dude...: ;)
you can hide the keyboard using this code
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You should use stateAlwaysHidded. Either in runtime as in the first answer, or directly in your project Manifest:
android:windowSoftInputMode="stateAlwaysHidden"
This will prevent keyboard from appearing during onCreate/onResume.
To prevent keyboard appearing after clicking EditText, you can place some blank view with transparent backround right on top of your EditText:
<View
android:id="#+id/view"
android:layout_width="editTextWidth"
android:layout_height="editTextWidth"
android:background="#android:color/transparent"
android:clickable="true"/>
The key attribute here is android:clickable="true". It will prevent from gaining focus on click (actually users will click on this blank view, not EditText).
Then, when user clicks special button you've mentioned, you just remove this blank view and call editText.requestFocus()

requestFocus doesn't work properly for EditText

A lot of time was spent to solve the problem, and it looks easy, but I'm really tired and couldn't find the solution.
I have an Activity, activity has 4 EditText components, 2 of them has popup menu (AlertDialog) which contain the list, next one - is disabled for edit, and last one - is editable, and should show the soft keyboard, when user is tapping on it.
Also, my root LinearLayout has LinearLayout which contain inside RelativeLayout. The last one is need for AdvBanner. Last LinearLayout(RelativeLayout) is aligned to the bottom of root layout.
The part of XML that describes it:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="bottom">
<RelativeLayout
android:id="#+id/AdvLayoutReserveArea"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="bottom" />
</LinearLayout>
When activity is start, editable EditText has focus with GREEN border and cursor is blinking. After few seconds left, the AdvBanner is loaded and shown. When it happens, editable EditText lost focus.. from this moment, my life be like a nightmare.
Let's look step by step.
Problem 1.
If in THIS MOMENT (when Adv loaded and appears) user is editing an EditText field via the soft keyboard, focus is lost, border take a GRAY color, and if user continue to typing a text is have no result - symbols are not printed (CURSOR in EditText is too lost).
I THINK any user will be annoyed - when you typing text, and cursor is inactive, because in background some adv is loaded and it take focus for self.
To solve this, in method when Adv is loaded (is shown) I try to back focus manually to EditText by requestFocus method.
public void onAdLoaded()
{
// TODO Auto-generated method stub
// add app specific code for this event here...
// called when an ad is successfully displayed on device
CountEdit1.requestFocus();
}
Yes, the cursor is returned to EditText field, and if soft keyboard is active, user can still typing text, but border of EditText field stay GRAY...
NOTE: actually I'm not sure about the difference between GREEN and GRAY border of focused EditText.. GREEN is usually when user is tapping on it, and GRAY, probably, when we want to request a focus manually (using requestFocus() etc)
Problem 2. (As result of solvation Problem #1).
After soft keyboard was closed, if user tap on editable EditText field, it take focus and cursor appears inside, but no reaction for showing soft keyboard again! Tapping of it do not show soft keyboard, but looks like the edit field in copy mode - when user can select a text and cut/copy it to clipboard.
My goal is easy for a first look. I just want to SAVE the cursor and focus to editable EditText field (CountEdit1) while soft keyboard is shown and user typing some text.
And normal reaction when user tapping EditText - as usually, just show me the soft keyboard!
I read all issues here, I combined different methods (clearFocus, requestFocusFromTouch etc), just not enough of time and space to describe all that I tried to do to solve this. The main problems are described above.
Hope for help and solving the problem...
Thanks in advance..
The goal is solved, the workaround is an easier than I thought. Problem #2 is fixed by using onClick () method. Sufficient condition for appearing of soft keyboard that use of both clearFocus() and requestFocus() methods.
CountEdit1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
CountEdit1.clearFocus();
CountEdit1.requestFocus();
}
});
The soft keyboard appears when user is tapping on the EditText field.
Fixed.
Works for me:
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
editField.requestFocus();
mgr.showSoftInput(editField, InputMethodManager.SHOW_IMPLICIT);
userInput.post(new Runnable() {
public void run() {
userIdInput.requestFocus();
}
});
Have a go at this and tell if your problem is still unsolved.
You should request focus after view is created in fragment or activity:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
CountEdit1.requestFocus();
}
try this:
public void onAdLoaded()
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
CountEdit1.requestFocus();
InputMethodManager mgr = (InputMethodManager) base.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(CountEdit1, InputMethodManager.SHOW_IMPLICIT);
CountEdit1.setSelection(CountEdit1.getText().length());
}
},0);
}

Categories

Resources