Android KeyBoard.Key disable iconPreview of special keys?












5















I customize my own soft keyboard by implementing the KeyboardView.OnKeyboardActionListener interface.



When the keys are pressed, it will show a preview popup.



My problem is how to disable the preview popup for special keys such as SHIFT and DELETE?



I have tried to set the android:iconPreview attribute to null but it didn't work.



<Key
android:codes="-1"
android:keyIcon="@drawable/key_shift"
android:keyWidth="15%p"
android:isModifier="true"
android:isSticky="true"
android:keyEdgeFlags="left" />


Have any idea?



Thanks in advance!










share|improve this question























  • I have the same problem.. can't disable preview for individual keys...

    – hsgubert
    Sep 25 '12 at 18:29
















5















I customize my own soft keyboard by implementing the KeyboardView.OnKeyboardActionListener interface.



When the keys are pressed, it will show a preview popup.



My problem is how to disable the preview popup for special keys such as SHIFT and DELETE?



I have tried to set the android:iconPreview attribute to null but it didn't work.



<Key
android:codes="-1"
android:keyIcon="@drawable/key_shift"
android:keyWidth="15%p"
android:isModifier="true"
android:isSticky="true"
android:keyEdgeFlags="left" />


Have any idea?



Thanks in advance!










share|improve this question























  • I have the same problem.. can't disable preview for individual keys...

    – hsgubert
    Sep 25 '12 at 18:29














5












5








5


1






I customize my own soft keyboard by implementing the KeyboardView.OnKeyboardActionListener interface.



When the keys are pressed, it will show a preview popup.



My problem is how to disable the preview popup for special keys such as SHIFT and DELETE?



I have tried to set the android:iconPreview attribute to null but it didn't work.



<Key
android:codes="-1"
android:keyIcon="@drawable/key_shift"
android:keyWidth="15%p"
android:isModifier="true"
android:isSticky="true"
android:keyEdgeFlags="left" />


Have any idea?



Thanks in advance!










share|improve this question














I customize my own soft keyboard by implementing the KeyboardView.OnKeyboardActionListener interface.



When the keys are pressed, it will show a preview popup.



My problem is how to disable the preview popup for special keys such as SHIFT and DELETE?



I have tried to set the android:iconPreview attribute to null but it didn't work.



<Key
android:codes="-1"
android:keyIcon="@drawable/key_shift"
android:keyWidth="15%p"
android:isModifier="true"
android:isSticky="true"
android:keyEdgeFlags="left" />


Have any idea?



Thanks in advance!







android keyboard preview android-softkeyboard






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '11 at 10:27









John SmithJohn Smith

5137




5137













  • I have the same problem.. can't disable preview for individual keys...

    – hsgubert
    Sep 25 '12 at 18:29



















  • I have the same problem.. can't disable preview for individual keys...

    – hsgubert
    Sep 25 '12 at 18:29

















I have the same problem.. can't disable preview for individual keys...

– hsgubert
Sep 25 '12 at 18:29





I have the same problem.. can't disable preview for individual keys...

– hsgubert
Sep 25 '12 at 18:29












3 Answers
3






active

oldest

votes


















5














First you must implement OnKeyboardActionListener



then use onPress() and onRelease() to control the preview popup like this:



public void onPress(int primaryCode) {
if (primaryCode==-2||primaryCode==-5||primaryCode==-4){
mInputView.setPreviewEnabled(false);
}
}

public void onRelease(int primaryCode) {
mInputView.setPreviewEnabled(true);
}





share|improve this answer



















  • 3





    Thanks for reply. I've tried and got a problem: if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup. Any idea about that?

    – John Smith
    Nov 29 '11 at 4:51



















2

















public void onCreate() {

mInputView.setPreviewEnabled(false);

}

public void onPress(int primaryCode) {
if (primaryCode==-1||primaryCode==-2||primaryCode==-5||primaryCode==-4){

} else {
mInputView.setPreviewEnabled(true);
}
}

public void onRelease(int primaryCode) {
mInputView.setPreviewEnabled(false);
}








share|improve this answer
























  • Giving full code is discouraged, please explain why your solution works and what it does.

    – nbokmans
    Oct 21 '15 at 13:57



















0














The problem with the above solution, as commented is that




if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup




To counter this, I had to extend the KeyboardView class
Disclaimer - The following contains reflection api



Here is the modified Keyboard class



import android.content.Context
import android.inputmethodservice.KeyboardView
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import com.continental.testapplication.utils.dpToPx
import java.lang.reflect.Method

class ModifiedKeyboardView :KeyboardView{
constructor(context: Context, attrs: AttributeSet):super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int):super(context, attrs, defStyleAttr)
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int, defStyleRes:Int):
super(context, attrs, defStyleAttr, defStyleRes)

/**
* Return true, if preview is to be shown, false otherwise. If not implemented,
* the preview is going to be shown.....
*/
var keyPreviewIndexListener:((Int)->Boolean) ?= null

private val findKeyIndicesMethod:Method = KeyboardView::class.java.getDeclaredMethod(
"getKeyIndices",Int::class.java,Int::class.java, (IntArray::class).java).also {
it.isAccessible = true
}
private val previewText:TextView = KeyboardView::class.java.getDeclaredField(
"mPreviewText").let {
it.isAccessible = true
it.get(this) as TextView
}


override fun onTouchEvent(me: MotionEvent?): Boolean {
if(me == null) return super.onTouchEvent(me)
when(me.action){
MotionEvent.ACTION_DOWN -> isPreviewEnabled = true
MotionEvent.ACTION_MOVE -> {
val touchX = me.x - paddingLeft
var touchY = me.y.toInt() - paddingTop
val verticalCorrection = dpToPx(14f, context)
if (touchY >= -verticalCorrection)
touchY += verticalCorrection.toInt()

val keyIndex:Int = findKeyIndicesMethod.invoke(this, touchX.toInt(), touchY.toInt(), null) as Int

isPreviewEnabled = keyPreviewIndexListener?.invoke(keyIndex)?:true
if(!isPreviewEnabled){
previewText.visibility = View.INVISIBLE
}


}
}
return super.onTouchEvent(me)
}
}


Paste it as is.



Next, in the class where you are manipulating the keyboard,



 keyboardView.keyPreviewIndexListener = {
it != spaceIndex && it != doneIndex && it != deleteIndex && it != `your_custom_index`
}


To find the indexes you can just do the following



 doneIndex = keyboardView.keyboard.keys.indexOfFirst {
it.codes[0] == Keyboard.KEYCODE_DONE
}


This will prevent the movement. Please append the other solution also.
i.e



override fun onPress(primaryCode: Int) {
Log.e("onPress", primaryCode.toString())
checkAndActivatePreview(primaryCode)

}

override fun onRelease(primaryCode: Int) {
Log.e("onRelease", primaryCode.toString())
deactivatePreview()
}

private fun checkAndActivatePreview(primaryCode: Int) {
keyboard.isPreviewEnabled =
(primaryCode != `your_custom_code`
&& primaryCode != SPACE_KEY_CODE && primaryCode != Keyboard.KEYCODE_DELETE
&& primaryCode != Keyboard.KEYCODE_DONE)
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8180942%2fandroid-keyboard-key-disable-iconpreview-of-special-keys%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    First you must implement OnKeyboardActionListener



    then use onPress() and onRelease() to control the preview popup like this:



    public void onPress(int primaryCode) {
    if (primaryCode==-2||primaryCode==-5||primaryCode==-4){
    mInputView.setPreviewEnabled(false);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(true);
    }





    share|improve this answer



















    • 3





      Thanks for reply. I've tried and got a problem: if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup. Any idea about that?

      – John Smith
      Nov 29 '11 at 4:51
















    5














    First you must implement OnKeyboardActionListener



    then use onPress() and onRelease() to control the preview popup like this:



    public void onPress(int primaryCode) {
    if (primaryCode==-2||primaryCode==-5||primaryCode==-4){
    mInputView.setPreviewEnabled(false);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(true);
    }





    share|improve this answer



















    • 3





      Thanks for reply. I've tried and got a problem: if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup. Any idea about that?

      – John Smith
      Nov 29 '11 at 4:51














    5












    5








    5







    First you must implement OnKeyboardActionListener



    then use onPress() and onRelease() to control the preview popup like this:



    public void onPress(int primaryCode) {
    if (primaryCode==-2||primaryCode==-5||primaryCode==-4){
    mInputView.setPreviewEnabled(false);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(true);
    }





    share|improve this answer













    First you must implement OnKeyboardActionListener



    then use onPress() and onRelease() to control the preview popup like this:



    public void onPress(int primaryCode) {
    if (primaryCode==-2||primaryCode==-5||primaryCode==-4){
    mInputView.setPreviewEnabled(false);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(true);
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 26 '11 at 16:08









    sunsetsunset

    511




    511








    • 3





      Thanks for reply. I've tried and got a problem: if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup. Any idea about that?

      – John Smith
      Nov 29 '11 at 4:51














    • 3





      Thanks for reply. I've tried and got a problem: if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup. Any idea about that?

      – John Smith
      Nov 29 '11 at 4:51








    3




    3





    Thanks for reply. I've tried and got a problem: if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup. Any idea about that?

    – John Smith
    Nov 29 '11 at 4:51





    Thanks for reply. I've tried and got a problem: if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup. Any idea about that?

    – John Smith
    Nov 29 '11 at 4:51













    2

















    public void onCreate() {

    mInputView.setPreviewEnabled(false);

    }

    public void onPress(int primaryCode) {
    if (primaryCode==-1||primaryCode==-2||primaryCode==-5||primaryCode==-4){

    } else {
    mInputView.setPreviewEnabled(true);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(false);
    }








    share|improve this answer
























    • Giving full code is discouraged, please explain why your solution works and what it does.

      – nbokmans
      Oct 21 '15 at 13:57
















    2

















    public void onCreate() {

    mInputView.setPreviewEnabled(false);

    }

    public void onPress(int primaryCode) {
    if (primaryCode==-1||primaryCode==-2||primaryCode==-5||primaryCode==-4){

    } else {
    mInputView.setPreviewEnabled(true);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(false);
    }








    share|improve this answer
























    • Giving full code is discouraged, please explain why your solution works and what it does.

      – nbokmans
      Oct 21 '15 at 13:57














    2












    2








    2










    public void onCreate() {

    mInputView.setPreviewEnabled(false);

    }

    public void onPress(int primaryCode) {
    if (primaryCode==-1||primaryCode==-2||primaryCode==-5||primaryCode==-4){

    } else {
    mInputView.setPreviewEnabled(true);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(false);
    }








    share|improve this answer
















    public void onCreate() {

    mInputView.setPreviewEnabled(false);

    }

    public void onPress(int primaryCode) {
    if (primaryCode==-1||primaryCode==-2||primaryCode==-5||primaryCode==-4){

    } else {
    mInputView.setPreviewEnabled(true);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(false);
    }








    public void onCreate() {

    mInputView.setPreviewEnabled(false);

    }

    public void onPress(int primaryCode) {
    if (primaryCode==-1||primaryCode==-2||primaryCode==-5||primaryCode==-4){

    } else {
    mInputView.setPreviewEnabled(true);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(false);
    }





    public void onCreate() {

    mInputView.setPreviewEnabled(false);

    }

    public void onPress(int primaryCode) {
    if (primaryCode==-1||primaryCode==-2||primaryCode==-5||primaryCode==-4){

    } else {
    mInputView.setPreviewEnabled(true);
    }
    }

    public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(false);
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 21 '15 at 13:31









    EUI CHAN HONGEUI CHAN HONG

    626




    626













    • Giving full code is discouraged, please explain why your solution works and what it does.

      – nbokmans
      Oct 21 '15 at 13:57



















    • Giving full code is discouraged, please explain why your solution works and what it does.

      – nbokmans
      Oct 21 '15 at 13:57

















    Giving full code is discouraged, please explain why your solution works and what it does.

    – nbokmans
    Oct 21 '15 at 13:57





    Giving full code is discouraged, please explain why your solution works and what it does.

    – nbokmans
    Oct 21 '15 at 13:57











    0














    The problem with the above solution, as commented is that




    if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup




    To counter this, I had to extend the KeyboardView class
    Disclaimer - The following contains reflection api



    Here is the modified Keyboard class



    import android.content.Context
    import android.inputmethodservice.KeyboardView
    import android.os.Build
    import android.support.annotation.RequiresApi
    import android.util.AttributeSet
    import android.view.MotionEvent
    import android.view.View
    import android.widget.TextView
    import com.continental.testapplication.utils.dpToPx
    import java.lang.reflect.Method

    class ModifiedKeyboardView :KeyboardView{
    constructor(context: Context, attrs: AttributeSet):super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int):super(context, attrs, defStyleAttr)
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int, defStyleRes:Int):
    super(context, attrs, defStyleAttr, defStyleRes)

    /**
    * Return true, if preview is to be shown, false otherwise. If not implemented,
    * the preview is going to be shown.....
    */
    var keyPreviewIndexListener:((Int)->Boolean) ?= null

    private val findKeyIndicesMethod:Method = KeyboardView::class.java.getDeclaredMethod(
    "getKeyIndices",Int::class.java,Int::class.java, (IntArray::class).java).also {
    it.isAccessible = true
    }
    private val previewText:TextView = KeyboardView::class.java.getDeclaredField(
    "mPreviewText").let {
    it.isAccessible = true
    it.get(this) as TextView
    }


    override fun onTouchEvent(me: MotionEvent?): Boolean {
    if(me == null) return super.onTouchEvent(me)
    when(me.action){
    MotionEvent.ACTION_DOWN -> isPreviewEnabled = true
    MotionEvent.ACTION_MOVE -> {
    val touchX = me.x - paddingLeft
    var touchY = me.y.toInt() - paddingTop
    val verticalCorrection = dpToPx(14f, context)
    if (touchY >= -verticalCorrection)
    touchY += verticalCorrection.toInt()

    val keyIndex:Int = findKeyIndicesMethod.invoke(this, touchX.toInt(), touchY.toInt(), null) as Int

    isPreviewEnabled = keyPreviewIndexListener?.invoke(keyIndex)?:true
    if(!isPreviewEnabled){
    previewText.visibility = View.INVISIBLE
    }


    }
    }
    return super.onTouchEvent(me)
    }
    }


    Paste it as is.



    Next, in the class where you are manipulating the keyboard,



     keyboardView.keyPreviewIndexListener = {
    it != spaceIndex && it != doneIndex && it != deleteIndex && it != `your_custom_index`
    }


    To find the indexes you can just do the following



     doneIndex = keyboardView.keyboard.keys.indexOfFirst {
    it.codes[0] == Keyboard.KEYCODE_DONE
    }


    This will prevent the movement. Please append the other solution also.
    i.e



    override fun onPress(primaryCode: Int) {
    Log.e("onPress", primaryCode.toString())
    checkAndActivatePreview(primaryCode)

    }

    override fun onRelease(primaryCode: Int) {
    Log.e("onRelease", primaryCode.toString())
    deactivatePreview()
    }

    private fun checkAndActivatePreview(primaryCode: Int) {
    keyboard.isPreviewEnabled =
    (primaryCode != `your_custom_code`
    && primaryCode != SPACE_KEY_CODE && primaryCode != Keyboard.KEYCODE_DELETE
    && primaryCode != Keyboard.KEYCODE_DONE)
    }





    share|improve this answer




























      0














      The problem with the above solution, as commented is that




      if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup




      To counter this, I had to extend the KeyboardView class
      Disclaimer - The following contains reflection api



      Here is the modified Keyboard class



      import android.content.Context
      import android.inputmethodservice.KeyboardView
      import android.os.Build
      import android.support.annotation.RequiresApi
      import android.util.AttributeSet
      import android.view.MotionEvent
      import android.view.View
      import android.widget.TextView
      import com.continental.testapplication.utils.dpToPx
      import java.lang.reflect.Method

      class ModifiedKeyboardView :KeyboardView{
      constructor(context: Context, attrs: AttributeSet):super(context, attrs)
      constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int):super(context, attrs, defStyleAttr)
      @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
      constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int, defStyleRes:Int):
      super(context, attrs, defStyleAttr, defStyleRes)

      /**
      * Return true, if preview is to be shown, false otherwise. If not implemented,
      * the preview is going to be shown.....
      */
      var keyPreviewIndexListener:((Int)->Boolean) ?= null

      private val findKeyIndicesMethod:Method = KeyboardView::class.java.getDeclaredMethod(
      "getKeyIndices",Int::class.java,Int::class.java, (IntArray::class).java).also {
      it.isAccessible = true
      }
      private val previewText:TextView = KeyboardView::class.java.getDeclaredField(
      "mPreviewText").let {
      it.isAccessible = true
      it.get(this) as TextView
      }


      override fun onTouchEvent(me: MotionEvent?): Boolean {
      if(me == null) return super.onTouchEvent(me)
      when(me.action){
      MotionEvent.ACTION_DOWN -> isPreviewEnabled = true
      MotionEvent.ACTION_MOVE -> {
      val touchX = me.x - paddingLeft
      var touchY = me.y.toInt() - paddingTop
      val verticalCorrection = dpToPx(14f, context)
      if (touchY >= -verticalCorrection)
      touchY += verticalCorrection.toInt()

      val keyIndex:Int = findKeyIndicesMethod.invoke(this, touchX.toInt(), touchY.toInt(), null) as Int

      isPreviewEnabled = keyPreviewIndexListener?.invoke(keyIndex)?:true
      if(!isPreviewEnabled){
      previewText.visibility = View.INVISIBLE
      }


      }
      }
      return super.onTouchEvent(me)
      }
      }


      Paste it as is.



      Next, in the class where you are manipulating the keyboard,



       keyboardView.keyPreviewIndexListener = {
      it != spaceIndex && it != doneIndex && it != deleteIndex && it != `your_custom_index`
      }


      To find the indexes you can just do the following



       doneIndex = keyboardView.keyboard.keys.indexOfFirst {
      it.codes[0] == Keyboard.KEYCODE_DONE
      }


      This will prevent the movement. Please append the other solution also.
      i.e



      override fun onPress(primaryCode: Int) {
      Log.e("onPress", primaryCode.toString())
      checkAndActivatePreview(primaryCode)

      }

      override fun onRelease(primaryCode: Int) {
      Log.e("onRelease", primaryCode.toString())
      deactivatePreview()
      }

      private fun checkAndActivatePreview(primaryCode: Int) {
      keyboard.isPreviewEnabled =
      (primaryCode != `your_custom_code`
      && primaryCode != SPACE_KEY_CODE && primaryCode != Keyboard.KEYCODE_DELETE
      && primaryCode != Keyboard.KEYCODE_DONE)
      }





      share|improve this answer


























        0












        0








        0







        The problem with the above solution, as commented is that




        if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup




        To counter this, I had to extend the KeyboardView class
        Disclaimer - The following contains reflection api



        Here is the modified Keyboard class



        import android.content.Context
        import android.inputmethodservice.KeyboardView
        import android.os.Build
        import android.support.annotation.RequiresApi
        import android.util.AttributeSet
        import android.view.MotionEvent
        import android.view.View
        import android.widget.TextView
        import com.continental.testapplication.utils.dpToPx
        import java.lang.reflect.Method

        class ModifiedKeyboardView :KeyboardView{
        constructor(context: Context, attrs: AttributeSet):super(context, attrs)
        constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int):super(context, attrs, defStyleAttr)
        @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int, defStyleRes:Int):
        super(context, attrs, defStyleAttr, defStyleRes)

        /**
        * Return true, if preview is to be shown, false otherwise. If not implemented,
        * the preview is going to be shown.....
        */
        var keyPreviewIndexListener:((Int)->Boolean) ?= null

        private val findKeyIndicesMethod:Method = KeyboardView::class.java.getDeclaredMethod(
        "getKeyIndices",Int::class.java,Int::class.java, (IntArray::class).java).also {
        it.isAccessible = true
        }
        private val previewText:TextView = KeyboardView::class.java.getDeclaredField(
        "mPreviewText").let {
        it.isAccessible = true
        it.get(this) as TextView
        }


        override fun onTouchEvent(me: MotionEvent?): Boolean {
        if(me == null) return super.onTouchEvent(me)
        when(me.action){
        MotionEvent.ACTION_DOWN -> isPreviewEnabled = true
        MotionEvent.ACTION_MOVE -> {
        val touchX = me.x - paddingLeft
        var touchY = me.y.toInt() - paddingTop
        val verticalCorrection = dpToPx(14f, context)
        if (touchY >= -verticalCorrection)
        touchY += verticalCorrection.toInt()

        val keyIndex:Int = findKeyIndicesMethod.invoke(this, touchX.toInt(), touchY.toInt(), null) as Int

        isPreviewEnabled = keyPreviewIndexListener?.invoke(keyIndex)?:true
        if(!isPreviewEnabled){
        previewText.visibility = View.INVISIBLE
        }


        }
        }
        return super.onTouchEvent(me)
        }
        }


        Paste it as is.



        Next, in the class where you are manipulating the keyboard,



         keyboardView.keyPreviewIndexListener = {
        it != spaceIndex && it != doneIndex && it != deleteIndex && it != `your_custom_index`
        }


        To find the indexes you can just do the following



         doneIndex = keyboardView.keyboard.keys.indexOfFirst {
        it.codes[0] == Keyboard.KEYCODE_DONE
        }


        This will prevent the movement. Please append the other solution also.
        i.e



        override fun onPress(primaryCode: Int) {
        Log.e("onPress", primaryCode.toString())
        checkAndActivatePreview(primaryCode)

        }

        override fun onRelease(primaryCode: Int) {
        Log.e("onRelease", primaryCode.toString())
        deactivatePreview()
        }

        private fun checkAndActivatePreview(primaryCode: Int) {
        keyboard.isPreviewEnabled =
        (primaryCode != `your_custom_code`
        && primaryCode != SPACE_KEY_CODE && primaryCode != Keyboard.KEYCODE_DELETE
        && primaryCode != Keyboard.KEYCODE_DONE)
        }





        share|improve this answer













        The problem with the above solution, as commented is that




        if I press other key (e.g. key A) and move my finger to the key SHIFT, the preview icon still popup




        To counter this, I had to extend the KeyboardView class
        Disclaimer - The following contains reflection api



        Here is the modified Keyboard class



        import android.content.Context
        import android.inputmethodservice.KeyboardView
        import android.os.Build
        import android.support.annotation.RequiresApi
        import android.util.AttributeSet
        import android.view.MotionEvent
        import android.view.View
        import android.widget.TextView
        import com.continental.testapplication.utils.dpToPx
        import java.lang.reflect.Method

        class ModifiedKeyboardView :KeyboardView{
        constructor(context: Context, attrs: AttributeSet):super(context, attrs)
        constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int):super(context, attrs, defStyleAttr)
        @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int, defStyleRes:Int):
        super(context, attrs, defStyleAttr, defStyleRes)

        /**
        * Return true, if preview is to be shown, false otherwise. If not implemented,
        * the preview is going to be shown.....
        */
        var keyPreviewIndexListener:((Int)->Boolean) ?= null

        private val findKeyIndicesMethod:Method = KeyboardView::class.java.getDeclaredMethod(
        "getKeyIndices",Int::class.java,Int::class.java, (IntArray::class).java).also {
        it.isAccessible = true
        }
        private val previewText:TextView = KeyboardView::class.java.getDeclaredField(
        "mPreviewText").let {
        it.isAccessible = true
        it.get(this) as TextView
        }


        override fun onTouchEvent(me: MotionEvent?): Boolean {
        if(me == null) return super.onTouchEvent(me)
        when(me.action){
        MotionEvent.ACTION_DOWN -> isPreviewEnabled = true
        MotionEvent.ACTION_MOVE -> {
        val touchX = me.x - paddingLeft
        var touchY = me.y.toInt() - paddingTop
        val verticalCorrection = dpToPx(14f, context)
        if (touchY >= -verticalCorrection)
        touchY += verticalCorrection.toInt()

        val keyIndex:Int = findKeyIndicesMethod.invoke(this, touchX.toInt(), touchY.toInt(), null) as Int

        isPreviewEnabled = keyPreviewIndexListener?.invoke(keyIndex)?:true
        if(!isPreviewEnabled){
        previewText.visibility = View.INVISIBLE
        }


        }
        }
        return super.onTouchEvent(me)
        }
        }


        Paste it as is.



        Next, in the class where you are manipulating the keyboard,



         keyboardView.keyPreviewIndexListener = {
        it != spaceIndex && it != doneIndex && it != deleteIndex && it != `your_custom_index`
        }


        To find the indexes you can just do the following



         doneIndex = keyboardView.keyboard.keys.indexOfFirst {
        it.codes[0] == Keyboard.KEYCODE_DONE
        }


        This will prevent the movement. Please append the other solution also.
        i.e



        override fun onPress(primaryCode: Int) {
        Log.e("onPress", primaryCode.toString())
        checkAndActivatePreview(primaryCode)

        }

        override fun onRelease(primaryCode: Int) {
        Log.e("onRelease", primaryCode.toString())
        deactivatePreview()
        }

        private fun checkAndActivatePreview(primaryCode: Int) {
        keyboard.isPreviewEnabled =
        (primaryCode != `your_custom_code`
        && primaryCode != SPACE_KEY_CODE && primaryCode != Keyboard.KEYCODE_DELETE
        && primaryCode != Keyboard.KEYCODE_DONE)
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 11:55









        DebanjanDebanjan

        1,341923




        1,341923






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8180942%2fandroid-keyboard-key-disable-iconpreview-of-special-keys%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...