Unable to disable soft keyboard in my android app - android

I have in my setting the option which should disable the app soft keyboard as some users could have a device with physical keyboard.
The issue is that everything i'm trying is not working and the soft keyboard is shown anyway,
I've tryed:
adding android:windowSoftInputMode="stateAlwaysHidden" in my manifest, i've tryed the following code in my fragment:
fun hideKeyboard(activity: Activity) {
val imm: InputMethodManager =
activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//Find the currently focused view, so we can grab the correct window token from it.
var view = activity.currentFocus
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
which i called in onViewCreated like:
hideKeyboard(requireActivity())
I've set to all EditText showSoftInputOnFocus:
val keyboard = prefs.getBoolean("keyboard", true)
txtBarcode.showSoftInputOnFocus = keyboard
txtQta.showSoftInputOnFocus = keyboard
And when showSoftInputOnFocus is set the keyboard doen't appear, but once i type something with the physical keyobard the softkeyboard is shown...
So based on my setting keyboard value (true,false) how can i disable the softkeyboard for the whole applicaiton??

Related

Weird issue with Samsung keyboard on Android with Libgdx

In my game, when a textfield is touched, the view moves up along with the keyboard.
Here's the code in AndroidLauncher:
onCreate(){
//other codes...
setListenerToRootView()
}
private fun setListenerToRootView() {
val activityRootView: View = window.decorView.findViewById(content)
activityRootView.viewTreeObserver.addOnGlobalLayoutListener(keyboardLayoutListener)
}
private var keyboardLayoutListener: OnGlobalLayoutListener? = OnGlobalLayoutListener {
val visibleDisplayFrame = Rect()
window.decorView.getWindowVisibleDisplayFrame(visibleDisplayFrame)
sizeChanged(visibleDisplayFrame.width(), visibleDisplayFrame.height())
}
override fun sizeChanged(width: Int, height: Int) {
val heightRatio = Gdx.graphics.height / main.worldHeight
val worldHeightChanged = height / heightRatio
val keyboardStatus = if (height == Gdx.graphics.height) KeyboardStatus.HIDE else KeyboardStatus.SHOW
main.platformsObservable.notifyObservers(Triple(ObservableKeys.SCREEN_SIZE_CHANGED, keyboardStatus, worldHeightChanged))
log.error("SCREEN_SIZE_CHANGED, status = $keyboardStatus")
}
The above code gets the keyboard height and status to send to my libgdx game class for the Camera to move the screen up/down.
With a normal keyboard it would send something like this for when the keyboard is shown:
SCREEN_SIZE_CHANGED, status = SHOW
and when the keyboard is hidden:
SCREEN_SIZE_CHANGED, status = HIDE
But on Samsung devices with the keyboard id of "com.samsung.android.honeyboard/.service.HoneyBoardService" then it does all this when the keyboard is shown once:
SCREEN_SIZE_CHANGED, status = HIDE
SCREEN_SIZE_CHANGED, status = SHOW
SCREEN_SIZE_CHANGED, status = SHOW
SCREEN_SIZE_CHANGED, status = SHOW
And this is making the keyboard blocking the textfield in my game because the view doesn't move up.
My gdxVersion is 1.11.0
How can I fix this?
Hi This isn't a libGDX issue. You need to provide parameters to your activity specifically saying what you want for this value (being the screen keyboard as opposed to a hardware one)
android:windowSoftInputMode
as described here
https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
and consider what you want for the parameters
adjustResize
adjustPan

programically click EditText

I would like to programmatically click an editText once a button has been pressed.
When a radio button "pitot_area" is selected, I want the editText associated with this selection to be programmatically clicked, so the user has one less "click" on the screen and it happens automatically.
So far I have tried
performClick()
callOnClick()
requestFocus()
the area i would like this code to be within is below:
root.pitot_area.setOnClickListener {
setAreaLabels(root)
evNodeItem.kvParams = false
evNodeItem.PitotRectArea = false
evNodeItem.PitotRoundArea = false
evNodeItem.areaParams = true
root.area_edit_text.requestFocus()
another method I have tried is
root.area_edit_text.setFocusableInTouchMode(true);
root.area_edit_text.setFocusable(true);
root.area_edit_text.requestFocus();
for the above lines of code, I have in the .xml file for the editText
android:focusable="false"
android:focusedByDefault="false"
An error does not show and the code builds, it seems this code is not read in kotlin? Does anyone have any ideas on how to do this? Thanks!
edit
ive looked at-> Focus Edit Text Programmatically (Kotlin) question sugggested below and was unable to implement it for my code.
To set direct focus to the EditText you have to open keyboard after setting focus to the EditText.
write this lines into your button click:
For Activity:
button.setOnClickListener {
editText.isFocusableInTouchMode = true
editText.requestFocus()
val inputMethodManager: InputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInputFromWindow(llMainView.applicationWindowToken, InputMethodManager.SHOW_FORCED, 0)
}
For Fragment:
button.setOnClickListener {
editText.isFocusableInTouchMode = true
editText.requestFocus()
val inputMethodManager: InputMethodManager = activity!!.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInputFromWindow(llMainView.applicationWindowToken, InputMethodManager.SHOW_FORCED, 0)
}

Xamarin.Forms View does not show keboard keyboard when we focus the EditText in Android

In Forms inherited from View and I have added EditText in Android project for that forms view by using custom render. And I have manually focused the view in button click dynamically. And now EditText has been focused but keyboard does not show.
Please find the Sample here for your reference
So to resolve the above issue by push to show the keyboard in EditText OnFocusChanged() by using the following code snippet. But when I have added the view inside of TableView in pcl, keyboard gitches when we tap the on that view.
InputMethodManager inputManager = (InputMethodManager)this.Context.GetSystemService(Context.InputMethodService);
if (gainFocus)
{
if (inputManager != null)
{
inputManager.ShowSoftInput(this, ShowFlags.Forced);
}
}
else
{
if (inputManager != null)
{
inputManager.HideSoftInputFromWindow(this.WindowToken, 0);
}
}

Input Text into Invisible EditText

My question is on invoking the soft-keyboard in Android.
I am trying out the FingerPaint (API Demos), which is essentially a touch-drawing app. Attempting to give it a touch text-input interface, I added EditText inside the CustomView class, like the following:
private void inputText() {
EditText newText = new EditText(getContext());
//getContext needed as we're in a class extending View, not activity
newText.setText("Text Text Text");
//'addView' is deliberately left out for invisibility
//'requestFocus' also left out
// Show soft keyboard for the user to enter the value.
InputMethodManager imm = (InputMethodManager) ConfigKey.context.getSystemService("input_method");
imm.showSoftInput(newText, InputMethodManager.SHOW_IMPLICIT);
String newTextString = newText.getText().toString();
System.out.println(newTextString);
}
The EditText was added alright (as seen from the Logcat, "Text Text Text") but there was just no keyboard to be seen for inputing.
Any way to fix it?
Any thoughts would be appreciated!

EditText request focus not working

I have an EditText and a Button. On click of the button i want to open the EditText keyboard and at the same time request focus on the EditText, So that the user directly start typing in the keyboard and text appears in the EditText.
But in my case when i click the button it open the keyboard, but it won't set focus on the EditText, because of which user has to click the EditText again to write on it. What is the issue. Any help or suggestion.
Code On click of button
m_SearchEditText.requestFocus();
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(m_SearchEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
ensure that the edittext is focusable in touch mode. You can do it two way.
In xml:
android:focusableInTouchMode="true"
in Java:
view.setFocusableInTouchMode(true);
Personally I don't trust the XML definition of this param. I always request focus by these two lines of code:
view.setFocusableInTouchMode(true);
view.requestFocus();
The keyboard shoul appear on itself without the need to call InputMethodManager.
It works in most of the cases. Once it did not work for me because I have lost the reference to the object due to quite heavy processing - ensure that this is not your issue.
In my case it worked by adding a handler after you clicked to button and focus set in another view the focus can get back to your needed view.
just put this in your code:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
lastview.getEditText().clearFocus();
m_SearchEditText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);
}
}, 100);
I hope it was helpful
Following saleh sereshki's answer and pauminku's comment, I'm using:
m_SearchEditText.post(new Runnable() {
#Override
public void run() {
m_SearchEditText.requestFocus();
}
});
which in Kotlin is the equivalent to:
m_SearchEditText.post { m_SearchEditText.requestFocus() }
The following works for me and should help:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
In your manifest.xml write:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />
And call m_SearchEditText.requestfocus() in oncreate().
OR,
Try:
if(m_SearchEditText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
As an extension to this answer (I didn't add it as a comment because of reputation...).
If you want to reduce the delayed time to zero, use handler.post() instead.
Full code:
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
lastview.getEditText().clearFocus();
m_SearchEditText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);
}
});
Minimalist Kotlin extension version because we should pretend these sorts of obtuse calls into system services are not necessary:
fun EditText.requestKeyboardFocus() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
U need two xml attributes also to achieve this:
android:focusable="true"
android:focusableInTouchMode="true"
Add them to the EditText as well as the parent layouts(Any layout inside which these views are). By default these are false, so the focus is not given to the requested view.
Source: https://developer.android.com/reference/android/view/View.html#attr_android:focusable
After u show the EditText based on the checkbox selection, add the next and previous focus points dynamically in code.
Hope this helps.
to focus you can use this function
editText.requestFocus()
you can have two different problems with EditText. one is EditText will be focused but the keyboard is not going to open, so as other answers said, you have to open the keyboard manually. other problem is EditText not gonna focused at all means nothing happened.
for opening keyboard you can do this after requestFocus :
val manager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
manager?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
if EditText not focused at all check for this properties. EditText has to be focusable, visible, enable, focusableInTouchMode. enable all of them before calling requestFocus.
this.isFocusable = true
this.isFocusableInTouchMode = true
this.visibility = View.VISIBLE
this.isEnabled = true
and at the end you can use this kotlin extention to do all of this for you :
fun EditText.focus() {
this.isFocusable = true
this.isFocusableInTouchMode = true
this.visibility = View.VISIBLE
this.isEnabled = true
this.isCursorVisible = true
this.post {
(this.context as? Activity)?.runOnUiThread {
this.requestFocus()
val manager =
this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
manager?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
}
}
In my case none of the answers above worked (they don't work in Nox, for example). To set focus to EditText, you could trick it into thinking that the user actually clicked it.
So I use MotionEvent, like this:
// simulate a click, which consists of ACTION_DOWN and ACTION_UP
MotionEvent eventDown = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0);
editText.dispatchTouchEvent(eventDown);
eventDown.recycle();
MotionEvent eventUp = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0);
editText.dispatchTouchEvent(eventUp);
eventUp.recycle();
// To be on the safe side, also use another method
editText.setFocusableInTouchMode(true);
editText.requestFocus();
editText.requestFocusFromTouch();
The above solutions will work if the EditText is enabled.
In my code, I have disabled the view at one point and trying to request focus later without enabling.
If none of the above worked, enable the EditText and then proceed with the above solutions.
In my case it is like,
etpalletId.isEnabled = true
etpalletId.text!!.clear()
etpalletId.requestFocus()
etpalletId.isCursorVisible = true
I was combining some answers and found the following solution:
fun Fragment.focusEditText(editText: EditText) {
Timer("Timer", false).schedule(50) {
requireActivity().runOnUiThread(java.lang.Runnable {
editText.isFocusableInTouchMode = true
editText.requestFocus()
val manager =
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
})
}
The timer delay makes sure that the focus request is working and the keyboard is opened manually because it did not work implicitely for me.
For some special cases in my code I do this:
later {
showKeyboard()
titleEdit.requestFocus()
}
In normal code it should look like this:
view.post {
(view.context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager)
.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_IMPLICIT_ONLY)
titleEdit.requestFocus()
}
Explanation: Looks like in some cases toggleSoftInput is the only way but somehow it steal focus and it works in some case just in code tun after things gets initialized so has to be post for later execution.
Make sure that your view is focusable if not then set it to focusable:
yourView.setFocusable(true);
yourView.setFocusableInTouchMode(true);
After that set focus as follows:
yourView.requestFocus()
If that doesn't work then use this:
yourView.post(new Runnable() {
#Override
public void run() {
yourView.requestFocus();
}
});
This will work, the reason behind this is the whole UI isn't loaded in order to set the focus on a specific UI element. In this cases, the focus can be overwritten by the system that might ignore your explicit request.
So if you request the focus using a background thread (yes, I’m meaning using Thread or Runnable class) it should do the trick.
For more info visit this link:
https://medium.com/#saishaddai/til-requestfocus-not-working-22760b417cf9
It's working for request focus when sometime request focus not working
fun EditText.focus(activity: Activity) {
this.isFocusable = true
this.isFocusableInTouchMode = true
this.visibility = View.VISIBLE
this.isEnabled = true
this.isCursorVisible = true
this.post {
activity?.runOnUiThread {
this.requestFocus()
val manager =
this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
manager?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
}

Categories

Resources