2

I've created a customized keyboard of my own.

When I click & drag over the keyboard the key should be highlighted according to my finger move. When I lift up my finger the corresponding letter has to be printed on the EditText.

How could I do this?

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
sudhakar
  • 43
  • 3

2 Answers2

0

Assuming you are just drawing your 'customized' keyboard onto a Canvas. Obviously using the built-in keyboard is better. However if you really want to do this yourself, here is a solution.

Draw each character of your keyboard, store it's x,y location in an array corresponding to each key as you draw it.

//initialise
int[] letterX = new int[29];
int[] letterY = new int[29];
char[] keyboardChar = {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M',' ', '<','#'}; 

somewhere in your 'draw' method:

// first keyboard row (do this for each row moving adding to y for next row
    x=10; y=50; keySpacing = 30; // starting x, y position
    for ( int charIndex = 0; charIndex < 10; charIndex++) {
    letterX[charIndex] = x;
    letterY[charIndex] = y;
            textWidth = mPaint.measureText(keyboardChar, charIndex, 1);
            if ( !letterHit ) { 
                canvas.drawText(keyboardChar, charIndex, 1, x - (textWidth/2), y, mPaint);
            } else {
                // this key is being pressed so draw highlighted somehow                    
                canvas.drawText(keyboardChar, charIndex, 1, x - (textWidth/2), y,mPaint);
            }   
            x += keySpacing;
     }

In the onTouchEvent, compare the x, y touch position to your array of letterX, letterY this will tell you if a key is being pressed. The array index tells you which character it is. You'd need some code in the draw method to print out the key highlighted if its being pressed (example below assumes 16px tolerance).

                for (int j=0; j < 29; j++ ) {
                    if (( Math.abs(touchX - letterX[j]) < 16 ) && ( Math.abs(touchY - letterY[j]) < 16 ) && !keyboardLock ) {
                        letterHit = j;
                    }
                }

You'll still need more logic (for deleting etc) and you'll need to maintain a string. That's why really this is best to use the system keyboard if possible.

Kevin
  • 1,200
  • 1
  • 11
  • 19
0

I think you need something like this post .

Community
  • 1
  • 1
Heidar
  • 498
  • 1
  • 17
  • 35