5

I'm testing my app on android 4.0.4 Samsung galaxy duos, the problem is if I set:

android:textIsSelectable="true"

the keyboard doesn't appear, though it appears on the emulator. Any suggestions?

Here is how my EditBox looks like

<EditText 

        android:focusable="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"        
        android:inputType="textVisiblePassword|textCapWords|textMultiLine"
        android:textIsSelectable="true"
        android:gravity="top|left"
        android:minLines="6"
        android:maxLines="10"
        android:minHeight="140dp"
        android:hint="@string/message"
        />
      <requestFocus />
gunar
  • 14,660
  • 7
  • 56
  • 87
Dakait
  • 2,531
  • 1
  • 25
  • 43
  • I'm seeing the same problem with textIsSelectable on devices – HannahMitt Aug 14 '14 at 14:14
  • @HannahMitt try setting `android:windowSoftInputMode="stateHidden"` in the activity defined in the Manifest.xml that will keep the keyboard hidden(in my case it did) – Dakait Aug 15 '14 at 05:03
  • I actually want the keyboard to show, not to be hidden. I just took out the textIsSelectable and then it behaved as normal. Didn't expect that behvaiour from that attribute. – HannahMitt Aug 15 '14 at 15:32
  • adding `` doesn't help? – Dakait Aug 18 '14 at 06:31

3 Answers3

7

Add a requestfocus. In xml:

<EditText>
    <requestFocus />
</EditText>  

Programmatically

edittext.requestFocus();

To "force" the SoftKeyboard to appear, you can do it dynamically:

InputMethodManager mImm = (InputMethodManager) 
                        getSystemService(Context.INPUT_METHOD_SERVICE);  
mImm.showSoftInput(SearchEdit, InputMethodManager.SHOW_IMPLICIT);  

Set onFocusChangeListener to appear your keyboard:

edittext.setFocusable(true);  
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {  
   @Override  
   public void onFocusChange(View v, boolean hasFocus) {  
       if (hasFocus)   
           mImm.showSoftInput(SearchEdit, InputMethodManager.SHOW_IMPLICIT);  
       else  
           mImm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);  
   } 
});  

See InputMethodManager Documentation for more information.
See also these answers to forcing the Keyboard to appear: Forcing the Soft Keyboard open

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • it works if i show it on click event, im searching for a solution so i dont have to programatically pop it open, if i couldnt find one im gonna accept your answer... tnx – Dakait Dec 11 '13 at 07:46
  • Do your focus work when `android:textIsSelectable` is true (I mean, is your cursor visible)? And if yes, your `Soft Keyboard` doesn't appear, right? – Blo Dec 11 '13 at 08:03
  • It's weird, but maybe you could do some things: like disabled `focus` in xml and add it dynamically / same with `textIsSelectable` / add a `onFocusChangeListener` to appear your keyboard (see my update answer) – Blo Dec 11 '13 at 08:31
  • Try this things one by one, etc. And let me know if it works, I'm curious about that weird issue. Thx – Blo Dec 11 '13 at 08:40
  • on a very tight schedule, i'll investigate it later and will give you feedback... – Dakait Dec 11 '13 at 09:04
1

You need to put requestFocus inside EditText element:

<EditText 

        android:focusable="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"        
        android:inputType="textVisiblePassword|textCapWords|textMultiLine"
        android:textIsSelectable="true"
        android:gravity="top|left"
        android:minLines="6"
        android:maxLines="10"
        android:minHeight="140dp"
        android:hint="@string/message">
      <requestFocus />
<EditText/>
gunar
  • 14,660
  • 7
  • 56
  • 87
  • What happens if you try this on other devices? – gunar Dec 11 '13 at 07:39
  • i dont have another device ATM, if i show it programatically as suggested in the @Fllo answer it is shown – Dakait Dec 11 '13 at 07:42
  • What else do you have in this layout? Isn't there another View that requests focus? – gunar Dec 11 '13 at 07:44
  • 1
    If you don't put `android:textIsSelectable="true"` then the focus behaves fine? – gunar Dec 11 '13 at 07:45
  • yup it behaves fine if i remove `textIsSelectable=true` – Dakait Dec 11 '13 at 07:47
  • i removed the `` from the other box, it disabled by default so i also put ` android:focusable="false"` but to no effect – Dakait Dec 11 '13 at 07:50
  • i have just checked that the copy/past option menu is not shown in the native sms application of the device, could that be interfering ? – Dakait Dec 11 '13 at 08:23
  • Try to remove `android:focusable` property and make sure you have only this `EditText` with `requestFocus`. What happens then? – gunar Dec 11 '13 at 08:26
  • im gonna go with the manual approach... will investigate it later, tnx for your time... – Dakait Dec 11 '13 at 08:32
0

here is my extension fun

fun EditText.showKeyboardOnLayout() {
    val mEditText = this
    viewTreeObserver.addOnGlobalLayoutListener(object : 
      ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
          viewTreeObserver.removeOnGlobalLayoutListener(this)
          mEditText.apply {
            if (isShown && isFocusable && isEnabled && requestFocus()) {
            // The EditText is visible, focusable, enabled, and has received focus
            showKeyboardWithRetry()
        }
    }
}

  private fun showKeyboardWithRetry() {
      val inputMethodManager = 
        context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
     inputMethodManager.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT)
     val isKeyboardShown = inputMethodManager.isActive(mEditText)
     if (!isKeyboardShown) {
        postDelayed({
            inputMethodManager.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT)
        }, 100)
     }
    }
  })

}

Use like use like:
binding.phoneEditText.showKeyboardOnLayout()

Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40