Android show input keyboard only when user press a button - android

I have a PopupWindow in one Activity.
When user press on the list item in the activity, the window will popup for getting input from users.
There are some EditText in the window. And also I provided some buttons that preset some text on it, so when user press on it then it will enter to the edittext.
I can disable the softkeyboard when the window first popup. But when I change the focus on edittext (Move from one edittext to another edittext), the keyboard shown up.
I want the softkeyboard show only when user press on the "show keyboard" button in the popup window
How can I do it?
Updated:
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
selectedEditText = (EditText)view;
String text = selectedEditText.getText().toString();
selectedEditText.setSelection(text.length());
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(selectedEditText.getWindowToken(), 0);
}
}
I tried the code above but it still showing.

set onFocusChangedListener() on every editText you have and disable Keyboard Pop inside it if it has focus..
Show it only when the user Presses the button..

There is very simple solution too; create fake focus above your EditText and you good to go.
Like this:
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true">
</LinearLayout>
<!--Your EditText-->
<EditText
...
</EditText>
also you can add these two line above your mentioned EditText in any VIEW, like TextView and so on.
android:focusable="true"
android:focusableInTouchMode="true"

Go to the xml and find the activity in which you want to hide keyboard
add given attribute to the Activity..
android:windowSoftInputMode="stateAlwaysHidden"

Related

My PopupWindow keeps stealing my EditText's focus

I have a simple "command dialog" that's a PopupWindow containing an EditText and a couple of buttons. I want the following behavior:
Touching outside the popup window dismisses it
When the popup appears, the EditText should grab (and keep) the focus
My EditText looks like this:
<EditText
android:id="#+id/send_cmd_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:hint="#string/send_cmd_hint"
android:textColor="#FFFFFF"
android:minEms="100"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:textColorHint="#BBBBBB"
android:inputType="text"
android:imeOptions="actionSend" >
<requestFocus />
</EditText>
The popup construction looks like this:
val popup = PopupWindow(view)
popup.isOutsideTouchable = true
popup.isTouchable = true
popup.isFocusable = true
popup.contentView = view
// This bizarre hack allows outside touching to dismiss it.
popup.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
sendButton.setOnClickListener {
sendCommand()
popup.dismiss()
}
if (A.isHardwareKeyboardAvailable()) {
// This only fires for the hardware keyboard.
editText.setOnKeyListener { _, code, event -> handleKey(code, event) }
} else {
setImeListener()
}
editText.requestFocus()
There seems to be a race condition occurring. I've got a hardware keyboard attached, and there are two ways to open the popup:
I can hit the Enter key, which the Activity notices, and then it calls the function to construct and show the popup.
I can press a button in my UI, which calls the same function.
Whenever I open the popup by pressing the button, the EditText gets the focus and everyone is happy.
Whenever I open the popup by pressing the Enter key on my hardware keyboard, the EditText gets the focus briefly, and then becomes unfocused. I suspect without proof that the PopupWindow is stealing the focus.
I need the popup to be focusable -- otherwise the keypresses simply go to the Activity, where it becomes difficult to route them back to the EditText.
I also need the popup to be outsideTouchable, so it will dismiss when you touch outside it.
I've tried setting isTouchable to false, but it seems to have no effect either way.
I've tried various oddball suggestions I've found related to this issue, such as calling popup.update() and so on. Nothing has solved the problem.
Again, this only happens when I have opened the popup by hitting the Enter key. The only difference I can see is that perhaps the Activity gets the focus temporarily before I create the popup, whereas if I press the UI button to open the popup, the Activity never gets the keyboard focus. Not sure, though.
The only thing that has worked for me is to set a timer:
android.os.Handler().postDelayed({ editText.requestFocus() }, 250)
This does the trick, but is obviously lame and fragile.
Any idea how to prevent my popup from stealing the focus from an EditText inside it?
Find the solution
final PopupWindow popUp = new PopupWindow(vbl.getMainLayout());
....
popUp.setFocusable(true);
popUp.update();

EditText: how to display the number while you are typing it?

I have three EditText widgets in my app and the XML for one of them is below. They work, mostly, but the problem is that the actual widget is at the bottom of my devices screen. When I tap the widget, and the numeric keypad is displayed, it completely covers the text entry window and I can't see what number I'm typing. Can I modify TextView so that when the Keypad is displayed on my device, it also displays the number that I'm currently typing?
<EditText
android:id="#+id/EditTextPrices" android:layout_width="100dip"
android:layout_height="wrap_content" android:inputType="number|numberDecimal"
android:singleLine="true" android.imeOptions="actionDone"/>
Another problem is that when I finish typing one number, instead of returning me to the main activity screen, it brings up the numeric keypad for the next TextEdit widget. I don't want this to happen. I had thought that setting android.imeOptions="actionDone" would cause the keypad to go away and be done when I finished typing the number, but not so. How can I stop the 'next' window from appearing?
Look into adding android:windowSoftInputMode to your manifest file. "adjustPan" as it's value is probably what you want though I've found that it sometimes fails to account for the space taken just above a softkeyboard by the strip of word-guesses while you're typing things in.
Add android:windowSoftInputMode=”adjustPan” to your manifest for the activity. That will cause the activity to scroll up so that the focused edit text is always on screen.
The other advice given to use adjustPan is good, and another idea is to put your layout in a scroll view.
Regarding your second problem, how are you handling actionDone?
For example this is how I hide the keyboard (or do whatever you want when they press done).
final EditText editTextPrices = (EditText) findViewById(R.id.editTextPrices);
editTextPrices.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionID, KeyEvent event) {
if (actionID == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager) MyClass.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(bugemailaddress.getWindowToken(), 0);
// DO OTHER HANDLING OF INPUT HERE
return true;
}
return false;
}
});

Show/Hidden Soft Keyboard event in Android

I need to get the Show/Hidden event from the SoftKeyboard in Android.
I did a research, but nothing worked.
I wanted that 'cause we're working with a tablet 5.0'' with low resolution, so when you edit a EditText, the keyboard rise in full screen, and then or you press the "enter or next" key, or you press the back button to hide the keyboard... I need to update some fields with the new value, but I can't use the TextWatcher 'cause have some business logics on what's the right fields to update, and 'cause I just want to update when the user really finish the input.
And the onFocusChanged isn't a option, 'cause we don't want our customers needing to cliking in the next field, and hiding the keyboard to see the new values.
I don't want to override the onTouchEvent too, see if where the user cliked isn't the same field that he is editing.
Sorry for the specific problem and more specific solution that I'm asking for.
And sorry for my bad English :D
Use tree view observer to detect any change in view height and that time you can check whether keyboard state
final View activityRootView = findViewById(R.id.chat_pane_root_view);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if(mgr.isAcceptingText())
{
//keyboard is up
}
}
});
First of all in your declairation of your activity in manifest file declaire like this
<activity android:name="Map" android:windowSoftInputMode="stateHidden">
from this property your keyboard is being hidden,
after this you are declaire the imeOption property in your main.xml file edittext like this
<EditText
android:id="#+id/edttexttitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:hint="#string/tasktitle"
android:imeOptions="actionNext"
android:inputType="text"
android:textSize="#dimen/font_size_medium"
android:textColor="#color/darkgray1" />
this code is for your first edittext if more then one and if only one edittext then your need to change the imeOption of edittext like
android:imeOptions="actionGo"

Android Display Numeric Keypad on Button Click

In my application, I am trying to display the numeric keypad when the user clicks on a button.
When the button is clicked, I shift the focus to the EditText in my layout using requestFocus() and next I need to display the numeric keypad so that the user can type in the values..
The values will always be numeric and hence I need to show only the numeric keypad.
I tired using this inside my button's onClick() method but it does not work.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Please provide me with any solution to this.
Also, my application is for an Android tablet supporting 4.0.3.
This one in your EditText property
android:inputType="phone" (This will displayed phone numeric keypad)
or
android:inputType="number" (This will displayed numeric keypad)
Now, you have to just set Focus on your EditText on Button's click..
something like,
edtNumber = (EditText) findViewById(R.id.number);
// Button's onClick....
#Override
public void onClick(DialogInterface dialog, int which)
{
edtNumber.requestFocus();
}
in EditText put below line.
android:inputType="number"

Show soft keyboard (numeric) using AlertDialog.Builder

I have an EditText in an AlertDialog, but when it pops up, I have to click on the text box before the keyboard pops up. This EditText is declared in the XML layout as "number", so when the EditText is clicked, a numeric keypad pops up. I want to eliminate this extra tap and have the numeric keypad pop up when the AlertDialog is loaded.
All of the other solutions I found involve using
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
This is NOT an acceptable solution, as this results in the standard keyboard rather than the numeric keyboard popping up. Does anyone have a way to make the numeric keyboard pop up in an AlertDialog, preferably while keeping my layout defined in XML?
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Mark Runs")
.setView(markRunsView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
int numRuns = Integer.parseInt(runs.getText().toString());
// ...
})
.setNegativeButton("Cancel", null)
.show();
Edit: I want to be perfectly clear that my layout already has:
android:inputType="number"
android:numeric="integer"
I also tried this:
//...
.setNegativeButton("Cancel", null)
.create();
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_CLASS_NUMBER);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
but that also did not work. With the setSoftInputMode line, I still get the full keyboard when the AlertDialog loads; without it, I still get nothing. In either case, tapping on the text box will bring up the numeric keypad.
Edit Again:
This is the XML for the EditText
<EditText
android:id="#+id/runs_marked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dip"
android:inputType="number"
android:numeric="integer">
<requestFocus/>
</EditText>
Coming a bit late, but today I had this same problem. This is how I solved it:
Call the keyboard when the Dialog opens just like you did:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Now, into the Dialog I'm assuming you have a EditText field that accepts only numbers. Just request focus on that field and the standard keybord will automatically transform into the numeric keyboard:
final EditText valueView = (EditText) dialogView.findViewById(R.id.editText);
valueView.requestFocus();
Now you just have to remember to dismiss the keyboard once you're done with the Dialog. Just put this in the positive/negative/neutral button click listener:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(valueView.getWindowToken(), 0);
Just try to set the InputType by using setInputType().
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
I think that you should keep on using the setSoftInputMethod hack but also provide hints to android that you want a numeric input.
Using xml layout attributes
For this, you can use several xml attributes to your EditText xml definition (see android:inputType for available options)
Examples:
<EditText android:inputType="phone" ...
<EditText android:inputType="number" ...
<EditText android:inputType="numberSigned" ...
<EditText android:inputType="numberDecimal" ...
You can also both hint android to show digital keyboard and restrict input to acceptable characters with android:numeric
Examples:
<EditText android:numeric="integer" ...
<EditText android:numeric="signed" ...
<EditText android:numeric="decimal" ...
Programatically
Use EditText.setRawInputType(int) with constants such as TYPE_CLASS_NUMBER you will find in android:inputType
or TextView.setKeyListener(new NumberKeyListener())
EDIT
AlertDialog focus by default on the positive button. It seems that it is what is causing trouble here. You may look at this similar question and its answer.
Try to specify in layout xml for your edit box:
<EditText ...>
<requestFocus/>
</EditText>

Categories

Resources