Making single tap look like double tap Android studio - java

I'm trying to hide my keyboard when Edittext is tapped.
problem is when setting an OnClickListener I have to double tap the Edittext if i want the hideKeyboard(); function to start.
Is it possible to set 2 OnClickListeners or something like that so it will appear like a double tap?
this is how i hide the keyboard:
private void hideKeyboard() {
System.out.println("Hiding keyboard");
inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

Related

Problem hiding keyboard when scrolling to an EditText field

I have a ScrollView that contains several EditText and AutoCompleteTextView views. I'd like to hide the keyboard if the user scrolls or taps out of the text box.
So I added this to my Fragments onViewCreated()
myScrollView?.viewTreeObserver?.addOnScrollChangedListener {
hideKeyboard()
}
The problem is, when I start scrolling the keyboard hides, BUT when I click an EditText or AutoCompleteTextView, the view starts to scroll to that field to show the keyboard, but because, the scrollview has the addOnScrollChangedListener it hides the keyboard. And the view just jumps back and forth with hiding and showing the keyboard.
What is the correct way to handle this scenario?
The hideKeyboard() comes from an extension class
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
fun Fragment.hideKeyboard() {
view?.let {
activity?.hideKeyboard(it)
}
}

How can I force the keyboard to appear?

When the user clicks on an editText the keyboard is brought up for the user to type. Can I make the keyboard to appear when a user clicks on a button instead of the editText? Can the numpad appear instead of the normal keyboard?
Can i make the keyboard to appear when a user clicks on a button
instead of the editText?
Yes, you need to set the focus and pop up the keyboard using InputMethodManager
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Focus the field.
editText.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
Can the numpad appear instead of the normal keyboard?
Yes, using input type
Either in the xml tag of edittext
<EditText...
android:inputType="number"/>
or in java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
public static void toggleKeyboard(Context context) {
try {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
} catch (Exception e) {
Log.e("On show keyboard error: %s", e.getMessage());
}
}

Closing keyboard after button click (breaks if no EditText is focused on) [duplicate]

This question already has answers here:
How to stop EditText from gaining focus when an activity starts in Android?
(54 answers)
Closed 8 months ago.
I put this on my button listener:
InputMethodManager inputManager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
It works perfectly for when the keyboard is up: it closes the keyboard and then continues its execution. The problem is that when no EditText was ever pressed (none of them are highlighted/focused on), it breaks and the app "stops working".
I guess it would be nice if I could check if an EditText had ever been pressed.
I tried to do this to check:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) // returns true if any view is currently active in the input method
{
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
EDIT
I ended up doing this:
I created this method:
public static void hideSoftKeyboard (Activity activity, View view)
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
I then called the method in my button listener like this:
hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of my class and v is the View I used in my button listener method.
Mediocre/bad solution, because it doesn't work as general as I want it to:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText x = (EditText)findViewById(R.id.editText);
x.requestFocus(); // this way no matter what, an EditText is focused on.
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);

Why won't showSoftInput display the virtual keyboard?

Essentially I am trying to show the virtual keyboard and gather input without the use of a visible EditText or TextView. I realize that toggleSoftInput can be used to do this however I need to use showSoftInput because I want to use a TextWatcher for manipulating the input. Also, the engine I am using is c++ so I am trying to do as little java-only code as possible so I am avoiding the .xml files. So here goes...
public class GameActivity extends Activity {
protected GameView view = null;
protected EditText editText;
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
view = new GameView(this);
setContentView(view);
editText = new EditText(this);
editText.setCursorVisible(false);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
}
public boolean showKeyboard()
{
JniApp.log("showKeyboard() in Java invoked!!!");
editText.requestFocus();
editText.requestFocusFromTouch();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
Where showKeyboard() is my c++ call into java. I have checked to make sure that editText is receiving focus and it is. However, showSoftInput returns false. Any help would be greatly appreciated.
UPDATE: After some debugging it looks as if requestFocus returns true but the activity still says view is the current focus.
Maybe try it with .SHOW_IMPLICIT instead of .SHOW_FORCED ?
Have you tried it on other emulators / devices with maybe other Android versions?

Another page poping up instead of keyboard

I want to open calculator which I created. This is the code I wrote:
TextView.OnClickListener listener = new TextView.OnClickListener(){
public void onClick(View v) {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textOut1.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut2.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut3.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut5.getWindowToken(), 0);
startActivity (new Intent("com.easyPhys.start.calculator"));
}
};
textOut1.setOnClickListener(listener);
textOut2.setOnClickListener(listener);
textOut3.setOnClickListener(listener);
textOut5.setOnClickListener(listener);
But what happens is virtual keyboard opens and my calculator opens only than I press ENTER. What is wrong with my code?
Try the onClick event listener on the TextView, instead of the OnEditorActionListener.
For it to work you also need to add the following attribute to the TextView in the xml.
android:clickable="true"
Don't forget it, without it it won't work.
The OnEditorActionListener only fires when some action is performed on the editor, and that's why the activity only shows when you click the keyboard. On the other hand, the onClick listener should fire right after you click the TextView.

Categories