Android: How to leave the softkeyboard visible after pressing the button - android

I have a screen with edittext and one button.
Edittext has properties
android:focusable="true"
android:focusedByDefault="true"
android:focusableInTouchMode="true"
Button has the same properties with false values.
On pressing the button there is a change edittext gravity. The edittext does not lose focus when button clicked. But keyboard is hidding.
How to make the keyboard not to hide and stay visible in this case?

You can do this programatically. Call this when you click Button.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
or
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

You can do this also with a tag like so:
button.setTag(R.id.DONT_HIDE_KEYBOARD, "true");
and under values/ids.xml, add:
<item name="DONT_HIDE_KEYBOARD" type="id"/>

try following.code,here focus is your EditText.
fun closeInput(focus: View?) {
val inputMethodManager = mActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(focus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}

Related

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)

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()

Android Onclicking textView the editText comes without numpad.How to get the numpad on single click on textView?

I have a textview.On clicking the texstview the edit text comes up with focus. But to get the numpad another click is required.
I want that when i click on TextView,the edit text should come up with the numpad togeter withoutanother click on the editText.Not through xml.But manually
In the xml i have already given it as android:inputType="number"
(the text view is made invisible on click)
The on click method----
case R.id.txtPhoneNo:
hintPhoneNum.setVisibility(View.VISIBLE);
phoneNum.setVisibility(View.GONE);
edtPhoneNum.setVisibility(View.VISIBLE);
edtPhoneNum.requestFocus();
edtPhoneNum.setText("");
WHen the editText gets focus how to get the numpad forcefully?
The correct answer is
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
for your EditText add the following XML:
android:inputType="number"
EDIT
Try requesting focus just after your textView was set invisible:
yourTextView.setVisibility(View.INVISIBLE); //or View.GONE
yourEditText.requestFocus();
This should display the keypad immediately
when TextView is shown or touched first it gets focus and when you click on it again it performs onClick() so you have to handle Foucus of TextView to achieve what you want.

Avoid edittext getting focus when text changes

I have a numberpicker with a plus and minus button and a edittext.
The user can click on the plus and minus button to increase / decrease or manually input when he clicks on the edittext.
But each time the user clicks a +/- button the text changes and the edittext receives focus.
This means the border changes color and the cursor shows up.
I want to avoid that, how can I do this?
I tried adding clearFocus() to the onTextChanged listener, and this works but the border still changes color for a moment.
In your XML file where you have your editText, you can set the focusable to false:
android:focusable="false"
To show the keyboard manually, use:
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
try{
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.SHOW_FORCED);
}
catch (Exception e)
{}
And put this on a listener.

Removing autofocus from EditText and regaining focus ?

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.

Categories

Resources