I have an activity in which there are several buttons and a spinner containing a list of preset values. Unfortunately, if the user accidentally or mistakenly taps the spinner, the soft keyboard appears. Why? There is nowhere on the screen that expects typed input from the user.
More to the point, how can I prevent this from happening? After some research, I tried adding the following code:
m_TricksPicker.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm=(InputMethodManager)getApplicationContext().
getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getParent().getCurrentFocus().getWindowToken(), 0);
return false;
}
}) ;
Unfortunately, this throws a null pointer exception. Any other suggestions how to stop the keyboard from appearing (and I'd really like to understand why the system thinks it's necessary in the first place, when no user input is required)?
Sorry for the delay, but I have redesigned the interface not to use a spinner at all.
Related
There is the proper method which is to draw the line myself via canvas. I want to avoid the complexity and I am asking here to double check if there is a simpler method I can use.
I am currently using a temporary method, which is to use SpannableString to highlight the nearest text 'character' that the cursor is on. But as you know, this "selects" the current character. I prefer the cursor to be between two characters instead of on top of one character.
I also don't want keyboard focus because I already laid out some nice buttons for the user to interact with. I don't want the app to use the Android keyboard and input methods. Only my buttons should be enough.
I tried accessing TextView methods:
Remove keyboard focus and input via the manifest
setFocusable(false)
setFocusableInTouchMode(false)
setCursorVisible(true) or cursorVisible="true"
This didn't work for me. I know that if I make the TextView focusable, then the cursor will be visible. But if it's not focusable, then the cursor does not show, even if it it is: cursorVisible="true".
What should I do?
Solution:
Disable input method of EditText but keep cursor blinking
Maybe try leaving out the xml attribute android:editable entirely
and then try the following in combination to
keep the cursor blinking and prevent touch events from popping up
a native IME(keyboard)..
/*customized edittext class
* for being typed in by private-to-your-app custom keyboard.
* borrowed from poster at http://stackoverflow.com/questions/4131448/android-how-to-turn-off-ime-for-an-edittext
*/
public class EditTextEx extends EditText {
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false; //for some reason False leads to cursor never blinking or being visible even if setCursorVisible(true) was
called in code.
}
}
Step 2 change the above method to say return true;
Step 3 Add another method to above class.
#Override public boolean isTextSelectable(){ return true; }
Step 4 In the other location where the instance of this class has been
instantiated and called viewB I added a new touch event handler
viewB.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
viewB.setCursorVisible(true);
return false;
} });
Step 5 Check to make sure XML and or EditText instantiation code
declares IME/keyboard type to be 'none'. I didnt confirm relevance,
but Im also using the focusable attributes below.
<questionably.maybe.too.longofa.packagename.EditTextEx
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="none">
Sorry for so many xml attributes. My code uses them all, testing in
4.2.1, and has results. Hope this helps.
I would like to ask if someone could give me a simple explanation of the KeyEvent.ACTION_MULTIPLE in Android and an example when it is triggered.
Here -> http://developer.android.com/reference/android/view/KeyEvent.html#ACTION_MULTIPLE it says that:
When interacting with an IME, the framework may deliver key events with the special action ACTION_MULTIPLE that either specifies that single repeated key code or a sequence of characters to insert.
What does it mean reapeted key code? That a key is pressed and held down? Sorry but it is not really clear to me cause I am not English and I am new in the Android developing.
Thanks for the attention!
EDIT:
So the event is triggered only when an arrow key of the keyboard is pressed and held down? As the user whose answer was accepted says here -> What triggers (or generates) KeyEvent.ACTION_MULTIPLE?, is it correct?
Your question made me curious :)..so I tried this code,and I was able to repeat this with a few keys.eg.
Backpress:when you press this key continuously,the IME starts deleting one word a ta time instead of one letter.similarly,it is possible determine multiple presses of keys which support such action.
This again depends on the IME.This would also be useful majorly from an IME app's Point.You cannot often replicate it is because long press usually triggers a different character.
Another point would be that KeyCodes are also inputted from the presence of a hardware keyboard.So this can come up and depends how you handle it.
While the http://developer.android.com/reference/android/view/View.OnKeyListener.html says that this action is triggerred only hardware keyboards,it worked with my software keyboard too.ALTHOUGH they are not expected to do so.I guess you can say that this would into picture when you use a hardware keyboard
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et = (EditText) findViewById(R.id.checkText);
KeyListener listener = et.getKeyListener();
Log.d("tag", listener.toString());
et.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d("tag", event.getAction()+"event");
return true;
}
});
}
}
I disabled softkeypad in my application because I have my own custom keypad. But the problem is when I clicked on the edittexts in order to enter data through my custom keypad ,that edittexts are not getting highlighted at all. Even cursor is not visible inside that respective clicked edittext. Why there are always side effects while disabling soft keypad? I tried all the suggestions that are there in the sources including stackoverflow, but nothing worked. Can I get the perfect solution to get the edittext highlighted when clicked?
you need to call textView.requestFocus() when clicked this way your editText can be highlight
dont forget also to add in your XML File
this attribute android:focusableInTouchMode="true" to your EditText
I don't know why those side effects occur, but in this post there is a workaround how disable the keyboard and still have the cursor. That worked for me except that I also needed to request focus, so it's:
//disable keypad
et.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = et.getInputType(); // backup the input type
et.setInputType(InputType.TYPE_NULL); // disable soft input
et.onTouchEvent(event); // call native handler
et.setInputType(inType); // restore input type
et.requestFocus(); // request focus
return true; // consume touch even
}
});
I have a view in my app which contains a ListView. Sometimes a row in the list is highlighted when the activity starts or resumes and if I hit the Enter key (on the physical keyboard of my device) then it does a click on the row.
I tried to override the OnKeyListener of the main view or of the list or to set it to null but it didn't change anything.
How can I achieve this ?
EDIT : sorry if it wasn't clear.
I want the ListView to react on clicks but only if I touch the screen, not when I push the enter key. And I don't want the list to react to the physical arrow keys. Basically a row should be focused or clicked in any other way than touching the screen.
Try to set focus to another view from your screen.
It's not 100% clear, but I suppose that you want your ListView to not react to clicks on rows. You're better off with returning false for .areAllItemsEnabled() on your adapter, and false for every row for .isEnabled(). This way you can switch off unneeded interactions on rows in your list.
It's not 100% right but since my view only had a ListView the following code did what I wanted :
menuListView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
return true;
}
return false;
}
});
When onKeyListener is set Backspace/delete key is not functioning.
I set an OnKeyListener on my EditText. Then default actions of some keys became not functioning. Like DELETE/Backspace. Then I changed to use my own text-deleting function by manipulating the string inside. But it seems to be pretty complex.
I have to get selection, make substring, and so on. Are there other solutions to get the key functioning normally?
It depends on the IME you are using.
Some IME implements delete function without sending KEYCODE_DEL.
Try other IME than the default.
For example, if you press DEL button long enough, some IME deletes all text in the edit box.
This cannot be done through KEYCODE_DEL.
I had this problem too, I solved it by returning false in onKeyListener function. This should execute normal operations on other keys.
.setOnKeyListener(new DialogInterface.OnKeyListener()
{
#Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
//your workarounds;
return false;
}
})
There are two known issues affecting KEYCODE_DEL for the default (LatinIME) Google Keyboard that ships with Android: Issues 42904 and 62306.
I have researched this and have devised a workaround, with code, that seems to get around both of these issues. That workaround can be found here:
Android - cannot capture backspace/delete press in soft. keyboard
I have similar problems that you are facing and I somehow managed to stumble on the solution. Apparently, I had setOnKeyListener to 'return true'. After I changed it to 'return false', the phone keyboard works perfect with backspace functioning properly once again on edittext. Hope this helps:
Solution: One of your existing onkeylistener codes contain 'return true'. Rectify it by setting existing code from 'return true' to 'return false'
.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
...
return false;
}
});