17

I created two EditText with "android:inputType="number" property.

here I am using hardware Keyboard, so when I perform Space Key event on textField, focus control directly shift from editText view to some other random view of screen. In normal text field type it took it as an another character, that's fine.

Any one have idea how can use Space key event to retain focus on same field.

CoDe
  • 11,056
  • 14
  • 90
  • 197
  • 1
    instead of `android:inputType="number"` put `"android:digits="0123456789 `. – g00dy Jul 30 '13 at 11:23
  • Thanks gOOdy, but as mention here http://stackoverflow.com/a/17944786/2624806, in this case it will allow to insert decimal then that I don't want...but why space Key event generating this behaviour..that's the point...have any idea over this? – CoDe Jul 30 '13 at 11:50
  • Ok, let me ask you this, what symbols is your EditText field accepting ? – g00dy Jul 30 '13 at 11:55
  • I got u gOody, but if i just use these digit then focus not jump from current view to another view. so i use type as an "number". – CoDe Jul 30 '13 at 12:06
  • Well, you can't use the space key then, instead, you can shift the focus manually in the program to the next EditText with `if(myEditText.requestFocus()) { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); }` for example. – g00dy Jul 30 '13 at 12:08
  • 1
    I handle key event for these numeric field and it solve my problem for a now....but why space key event showing such behaviour will be a interesting stuff. – CoDe Jul 31 '13 at 10:12
  • Maybe try to catch the event when you press this key and see to what it corresponds. Hardware keyboard is a little bit different from the soft one. Go and try it, it's really interesting what is this sending. – g00dy Jul 31 '13 at 10:27
  • Please post your Layout XML, and any code where you might set listeners/etc for the two EditTexts. – mawalker Dec 24 '15 at 01:41

3 Answers3

3

Using editText.setInputType(InputType.TYPE_CLASS_NUMBER) may probably solve your problem.

Ashish John
  • 1,867
  • 2
  • 23
  • 38
  • Bounty got awarded automatically by SO, the above answer still doesn't solve my issue or anything relevant as every android dev would understand that its all same if you define runtime or in xml so nothing to take positive from above answer. Please don't consider the awarded answer as right answer here. I will keep waiting for new answer post and if any solve my issue, I will award 500 point bounty afterwards. As it seems that people are doing fake upvote and getting half bounty points. – MKJParekh Dec 28 '15 at 06:54
2

Change/Add your EditText Property with android:imeOptions="actionNext" like as follows

<EditText
    android:id="@+id/edit_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:imeOptions="actionNext"
    android:inputType="text" /> 

Or with android:imeOptions="actionNone" for default behavior of EditText

<EditText
    android:id="@+id/edit_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:imeOptions="actionNone"
    android:inputType="text" />
SachinS
  • 2,223
  • 1
  • 15
  • 25
  • post your EditText from Xml – SachinS Dec 23 '15 at 11:36
  • I have tried many things, imeOptions, inputType, digits... all are having same behavior. Currently seeing your answer I changed it to your given code, still issue is the same. Please if possible keep 5-10 **numeric only edittext** in UI and press space. you will come to know. – MKJParekh Dec 23 '15 at 12:38
  • post your layout file – SachinS Dec 24 '15 at 04:45
  • @MKJParekh did u tried solution given by "mawalker" – CoDe Dec 24 '15 at 05:31
  • @Shubh mawalker 's answer suggest to implement listener, I have specified already that I am not looking for any solution where I have to assign listener to my 100's of views in each screen. So the answer may resolve the issue but it's not for me. – MKJParekh Dec 28 '15 at 07:00
2

So it looks like your problem doesn't have to do with the 'input' type of your EditText, but rather the key press events coming from the keyboard....

So 'hopefully' this should fix your problem for you (by 'skipping over' the 'next' event from the 'space' button being pressed.)

 // do for both EditText(s)
 editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        log.d("main","KeyPress:"+ actionID);
        if (event != null ) { log.d("main","KeyPress:" + event.getKeyCode() ); }
            if ( actionId == EditorInfo.IME_ACTION_NEXT) {
                // do 'nothing' or 'add a space' if you want that
                return true;
            }
            return false;
        }
    });

This is likely what is happening (hard to tell without your XML)

So I found something that should help you figure out the pattern + possibly lead to solving it... from https://stackoverflow.com/a/17990096

if there is a android:nextFocus.... setting in your XML (or equivalent in code) and/or the physical keyboard 'space' is also signalling IME_ACTION_NEXT (or another IME action similar to it)


If the EditorInfo.IME_ACTION_NEXT isn't the IME action that is causing your problems, then you can try this... to determine WHAT is.

from: https://stackoverflow.com/a/4171427

if you have PHYSICAL keyboard you can use to detect keyDown events and handle them appropriately,

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {
    log.d("main","KeyPress:" + keyCode);
    if (event != null ) { log.d("main","KeyPress:" + event.getKeyCode() ); }
   // if you want to 'handle' the keyPess here, best to use switch like below
   // switch (keyCode) {
   //     case KeyEvent.KEYCODE_A:
    //    {
    //        //your Action code
    //        return true;
    //    }
   /// }
    return super.onKeyDown(keyCode, event);
}

BUT... if you have software keyboard You need to use addTextChangedListener/TextWatcher Because the physical key press is 'eaten' by the EditText (from what I saw in another post, but seems to be correct from my testing.)

mMyEditText.addTextChangedListener(new TextWatcher()
{
    public void afterTextChanged(Editable s) 
    {
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) 
    {
        /*This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after. It is an error to attempt to make changes to s from this callback.*/ 
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {
    }
);

You can override what the EditText does when a 'space' is entered.

This seems pretty easy when in 'software keyboard', but physical keyboard seems to be a bit more difficult.

https://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html

Similar to this question (but 'space' instead of 'enter') Android - Handle "Enter" in an EditText


This could help you determine the 'pattern' of the focus being changed. if it is random or not (most likely -not- random, but possibly 'interesting' to look into)

http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html

// try this to see
View.OnFocusChangeListener changeListener = new View.OnFocusChangeListener()                
    {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            log.d("YourClassName", "FocusChanged" + hasFocus)
        }
    };

 EditText1.setOnFocusChangeListener(changeListener);
 EditText2.setOnFocusChangeListener(changeListener);
 Button.setOnFocusChangeListener(changeListener);
 // ... etc... (add to views to see if there is pattern)
Community
  • 1
  • 1
mawalker
  • 2,072
  • 2
  • 22
  • 34
  • Thanks for the answer, but I am not looking to assign listeners to all my 100 views. – MKJParekh Dec 28 '15 at 07:01
  • You could probably create a single listener (that ignores space on physical keyboards) to every view (thereby not using up too much code or ram, etc.) – mawalker Dec 28 '15 at 15:11