Still visible soft keyboard - android

I have curious case of still visible soft-keyboard.
Simple flow.
Activity without any edittexts, where soft keyboard is hidden.
From manifest:
<activity
android:name=".activities.MainActivity"
android:theme="#style/MainActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
...and also checked from code
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
In this activity I have a fragment which contains webview. I have added touchlistener to this webview which send 'space' key to it.
protected View.OnTouchListener onTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_SPACE);
}
return false;
}
};
It handles start/pause effect, where I have vimeo video-content of that webview. Previously I tried to communicate with vimeo video player by javascript handler, but it wasn't work. Sending key event is really simple solution and works great on all devices except one. On dell t01c when touchlistener is invoked, software keyboard appears. I can hide it with simple
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
but it ruins immersive mode of activity - navigation bar shows and disappears. How to block keyboard permanently? Has anyone had similar problem?

Try this
put this static method in your Utils class
public static void hideKeyboard(Activity activity) {
try {
InputMethodManager input = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
Call this method whereever you want to hide the keyboard by Utils.hideKeyboard(YOURACTIVITY.this);

Related

Custom keyboard set visibility gone when touch event dispatch from editText

I want to mimic android keyboard behavior when handling touch event. The android keyboard always hide every time I click somewhere else other the edit text and the keyboard. So far, I could mimic that's behavior using dispatchTouchEvent
Since it will detect for any dispatch touch even, whenever I click my keyboard button then it will close the keyboard it self. I wanna check if I click somewhere else other than my keyboard then hide the keyboard, else keep the keyboard on. I think I just missed piece of code and I can't find it what i missed here. It just need one single step to accomplish this. Please help.
Here my keyboard looks like
and here what i did so far
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getCurrentFocus() != null) {
View view = getCurrentFocus();
//im not sure this condition would help
if (view == null) {
keyboard.setVisibility(View.GONE);
}
}
return super.dispatchTouchEvent(ev);
}
I can get the view object but i cant determine which view that i need to keep my keyboard on.
The other code (variable declaration)
PhoneKeyboard pk;
InputConnection ic;
LinearLayout keyboard;
pk = findViewById(R.id.phone_keyboard);
ic = phone_no.onCreateInputConnection(new EditorInfo());
pk.setInputConnection(ic);
Handling back pressed (mimic the android keyboard behavior)
#Override
public void onBackPressed(){
if(keyboard.getVisibility() == View.GONE)
super.onBackPressed();
else
keyboard.setVisibility(View.GONE);
}
How I handle request on the editText to make my keyboard visible
phone_no.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
android.content.Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(view.equals(phone_no) && keyboard.getVisibility() == View.GONE) {
phone_no.requestFocus();
keyboard.setVisibility(View.VISIBLE);
}
return false;
}
});
How I handle submit / enter event at my custom keyboard (for who need the solution for custom keyboard, maybe this post can help other:) )
phone_no.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
android.content.Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(view.equals(phone_no) && keyboard.getVisibility() == View.GONE) {
phone_no.requestFocus();
keyboard.setVisibility(View.VISIBLE);
}
return false;
}
});
Here is my snippet code for the keyboard layout view
<include
layout="#layout/layout_keyboard_phone_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Ps. I also have base activity and it extends to all my activity (if your solution required the base activity)
Update 1
I have try this method
phone_no.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
keyboard.setVisibility(View.GONE);
}
}
});
Is doing the same thing with the onDispatch event, whenever I click number on my custom keyboard, it also hide the keyboard. I need to check if the view others than the edit text and my custom keyboard then hide the keyboard else keep the keyboard on. Thank you

How to prevent focus on Activity start [duplicate]

This question already has answers here:
How to stop EditText from gaining focus when an activity starts in Android?
(54 answers)
Closed 5 years ago.
I have an Activity with only one EdtiText. When that Activity starts, the EditText is focused and the soft keyboard is shown. This seem to happen after onResume, because when I programmatically hide the keyboard in onResume it doesn't work. When I do this:
#Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
//If no view currently has focus, create a new one, just so we can grab a window token from it
imm.hideSoftInputFromWindow(etBarcode.getWindowToken(), 0);
}
}, 500);
}
it hides it (after popping up shortly).
Is there an event on an EditText I can use to preven the keyboard popping up? Or some other way of preventing it to show?
Update focusableInTouchMode does not do what I want, because when set to true the keyboard pops up, when set to false it is not focusable at all.
// Add following code in activity onCreate
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
For parent layout android:focusableInTouchMode="true"
You can set property of Layout like
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
The problem is very complicated, as it's about views getting the focus, and how it's handled by all the layout, about the touch mode being focusable, and last but not least about how the soft keyboard handles that.
But this works for me:
In manifest:
android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
In layout:
android:focusable="true"
android:focusableInTouchMode="true"
and last but not least, touch listener set to the EditText to prevent the soft keyboard to show up once touched:
mMyText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// forward the touch event to the view (for instance edit text will update the cursor to the touched position), then
// prevent the soft keyboard from popping up and consume the event
v.onTouchEvent(event);
disableSoftKeyboard(MyActivity.this);
return true;
}
});
whereas the method does more or less what you're doing already:
public void disableSoftKeyboard(#NonNull Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} else {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
Hope it helps, and also that I didn't forget anything :)

Hide keyboard when user touches fragment inside activity

Inside the .xml layout I have 3 components in following order:
A Textview
A fragment (extends ListFragment)
A LinearLayout storing 2 buttons
Whenever user uses the search bar to query, the keyboard appears. Content of the fragment will be updated depending on the query.
What I'm trying to do is whenever user taps the screen (outside the keyboard), the keyboard needs to be hidden. So far, I could only do so if I tap anywhere but the fragment region.
I tried setOnTouchListener inside onActivityCreated() inside the fragment but it doesnt seem to work.
I got tapping outside the fragment = hidding keyboard to work by using the following inside onCreate():
layout.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View view, MotionEvent ev)
{
hideKeyboard(view);
return false;
}
});
hideKeyboard(View view)
public void hideKeyboard(View view)
{
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
But not inside the fragment...
I made a silly mistake....
The following code would solve the issue:
Inside onActivityCreated()
getActivity().findViewById(R.id.mid).setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View dview, MotionEvent ev)
{
f_hideKeyboard(dview);
return false;
}
});
Edited: Nevermind.... The above works only if the query shows zero result. If it shows at least 1 result, touching the fragment region still will not hide keyboard
I know that it's a bit late. From what I have understand from your question, here is the solution below. Use the following code inside onCreateView
//hides keyboard when tap on the screen
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_MOVE){
//do something
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
return true;
}
});

Having Trouble with Soft Keyboard

Hi Everyone iam new to Android and stuck in really silly problem in my project i have one EditText which is defined in Header View of a List View whenever users touches the EditText soft keyboard will be displayed. i have something called clear button which clears the EditText After clearing soft keyboard is not displaying in 2.3 devices.Whenever i press lock/power button lock the device and unlock then soft keyboard is displaying
<activity
android:name=".pl.Mobile.AddJobNew"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"
>
Here is My Activity Declaration in Android for android:windowSoftInputMode : 'adjustResize' option working perfectly for Android 2.3 devices but failed in 4.0 devices where soft keyboard overlapping over edit text . option 'adjustPan' is workin good in 4.0 devices but failed in 2.3 devices
Please help me get out of this problem.
Thanks in Advance
Try this in the activity with the edit text:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// Do something for 4.0 and above versions
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
else{
// do something for phones running an SDK before 4.0
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
Also remove,
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"
From your activity in the manifest file.
Build.Version
I think that the problem is that when you click the clear button, the editText looses its focus so the keyboard hides itself.
I would try 2 things to solve it;
after you clean the text, do:
yourEditTextField.requestFocus()
manually control the keyboard visibility:
public void showKeyboard(){
View view = getWindow().getCurrentFocus();
if (view==null)
return;
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
public void hideKeyboard(){
View view = getWindow().getCurrentFocus();
if (view==null)
return;
IBinder binder = view.getWindowToken();
if (binder == null)
return;
// prevent a bug in some keyboards that makes the text hang on the screen
if (view instanceof EditText)
((EditText)view).setText(((EditText)view).getText().toString());
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
}
I have below line in My AndroidManifest.xml file and it works great. Please try this code. Dont put any extra code. And still if you not get any success then let me know. I will like to help you out.
<activity android:name=".pl.Mobile.AddJobNew" android:screenOrientation="portrait" android:configChanges="keyboard|orientation" android:windowSoftInputMode="adjustPan"/>
Try with above code for your activity.
Try something like that
editext1.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (v instanceof EditText)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
v.requestFocusFromTouch();
return true;
}
return false;
}
});
If it not work for you try to add
editext1.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
Also is good to remove request focus from layout. and maybe set this android:windowSoftInputMode="stateHidden|stateAlwaysHidden" in manifest on activity

How to prevent keyboard to show in EditText onTouch?

What I want to do is this: I have an EditText and when the user click on it, the user can move the caret position, same as the EditText, but without showing the keyboard.
I've tried with setInputType(0); and it hides completly the keyboard but the cursor doesn't appears.
Is there any way to do this?
Thank's
The following tricks works for me. Caret and soft keyboard both active onTouch event of editText. So Call touch event and then hide the keyboard manually.
myEditText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final boolean ret = dialerNumber.onTouchEvent(event);
final InputMethodManager imm = ((InputMethodManager) myContext
.getSystemService(Context.INPUT_METHOD_SERVICE));
try{
imm.hideSoftInputFromWindow(myEditText.getApplicationWindowToken(), 0);
}catch(Exception e){
e.printStackTrace();
}
return ret;
});
I haven't tried suppressing the keyboard altogether, but I have hidden the soft keyboard manually before using the InputMethodManager.hideSoftInputFromWindow method.
Does this work?
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

Categories

Resources