I want to do the following :
I have an edit text and want to be able to select text from it without showing the soft keyboard and without editing it's content, just to select text to use it in another screen
N.B. this edit text is inside dialog box that shown above the activity, the current scenario that it show up the keyboard when open the dialog
I have tried the following
android:windowSoftInputMode="stateHidden"
I have also used the following code :
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
hideSoftKetboard (v);
return true;
}
});
private void hideSoftKetboard (View v){
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
can anyone help please ?
Use setInputType(0); will solve the problem
Related
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
This question already has answers here:
How to hide soft keyboard on android after clicking outside EditText?
(49 answers)
Hide keypad in android while touching outside Edit Text Area
(3 answers)
Closed 4 years ago.
I have a fragment containing an EditText for search input and a ListView. I've got the search part working, but now I want to close the keyboard when the user clicks the on the screen outside of the EditText. I'd also like to consume the click event (because it is a ListView, if I don't consume the click event the user may accidentally click on a ListView item they didn't want to open)
I know how to do this in an activity, but it seems to be different for fragments.
What I've tried so far:
Implement View.OnClickListener in the fragment and implement onClick as such:
#Override
public void onClick(View v) {
System.out.println("onClick true");
if (!(v instanceof EditText)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.
INPUT_METHOD_SERVICE);
if (getActivity().getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
}
}
This never seemed to post "onClick true" when I clicked.
Override onTouchEvent in the fragment's activity and implement onTouchEvent as such:
#Override
public boolean onTouchEvent(final MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.
INPUT_METHOD_SERVICE);
if (getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
return true;
}
Any help would be greatly appreciated. Thanks in advance.
If I get you right you can try to use a OnFocusChangeListener:
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
final InputMethodManager inputManager = (InputMethodManager) getAppContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null && !hasFocus) {
inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
This is just a sample, but maybe it points into the right direction.
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;
}
});
I have 2 EditText fields which I have the user fill out, followed by a DatePicker. I am trying to hide the keyboard as soon as the user touches the DatePicker (as the keyboard is covering the save/cancel buttons.)
dpDate.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard();
return false;
}
});
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
This is currently what I have, however, it does not appear to be working. When I touch the DatePicker, click it, anything.. the keyboard will not go away.
Any help is appreciated!
EDIT:
The layout is as follows
Amount: (EditText)
Memo: (EditText)
Date: (DatePicker)
--------------------
Cancel OK
When user touches that datepicker to change the date, I immediately want the keyboard to go away.
So you may need to just do Context.INPUT_METHOD_SERVICE instead of just INPUT_METHOD_SERVICE.
I'm currently developing a calculator app where I have made a custom keypad and would like to hide the virtual keyboard. I have found solutions where I can hide it, but the cursor also gets hidden. The functionality I want is the same as the com.android.calculator2 app. I have looked at the source code of that but I still can't get it to work.
I think you are getting it wrong. There is a much easier solution(and a more obvious one).
Make the EditText uneditable.
Bind to the EditText in your code (findViewById)
In your buttons, get the text and add to the current string and then display it.
Eg.
say you pressed the '1' button.
in your one.setOnclickListener(), do this:
String S=EditText.getText()+"1";
EditText.setText(s);
Edit:
If you just want to hide the keyboard while keeping the cursor, try this code:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});