10

I am Working on Custom Keyboard Application

This is background color of key when user select blue

If user select green this should be background color

This is code for background color of input.xml in softkeyboard :-

     @Override
    public View onCreateInputView() {


      Log.e("onStartInputView ","On StartInput View Called--");

      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
      String Backgroundcolour = preferences.getString("BackgroundColour","");

     Log.e("Brithnesss- -","----"+Backgroundcolour);

    if(Backgroundcolour.equalsIgnoreCase("black"))
    {

    this.mInputView = (KeyboardView) getLayoutInflater().inflate(
            R.layout.input, null);


    }else
    {
        this.mInputView = (KeyboardView) getLayoutInflater().inflate(
            R.layout.input1, null);
        //this.mInputView.setB
    }

    this.mInputView.setOnKeyboardActionListener(this);
    this.mInputView.setKeyboard(this.mQwertyKeyboard);
    return this.mInputView;
}

 @Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);
    // Apply the selected keyboard to the input view.

    setInputView(onCreateInputView());

}

I am not getting how to set background image for specific key.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
user
  • 471
  • 2
  • 16
  • 31
  • Refer this http://stackoverflow.com/questions/15789997/how-to-change-background-color-of-key-for-android-soft-keyboard – Asha Soman Aug 14 '13 at 06:30
  • i am not getting form this much properly can u please tell how i would set different color keys? – user Aug 14 '13 at 06:38
  • I need to set background for specific key background instead of same key background image for whole keyboard. i need urgently – user Aug 14 '13 at 06:56
  • Hi @user...I had done with the colorful keys but the text in the keys are not visible..Can you please help me??? – Sagar Gangawane Dec 03 '17 at 13:01

3 Answers3

10

As an example, there's a small downloadable project that creates a custom numeric keyboard. To the CustomKeyboardView class there or to your own custom keyboard class, add a method like the following. It overrides the onDraw() method and draws the background of the key defined with code 7 (in this case the "0") red and all the other keys blue.

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    List<Key> keys = getKeyboard().getKeys();
    for (Key key : keys) {            
        if (key.codes[0] == 7) {
            Log.e("KEY", "Drawing key with code " + key.codes[0]);
            Drawable dr = (Drawable) context.getResources().getDrawable(R.drawable.red_tint);
            dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
            dr.draw(canvas);

        } else {
            Drawable dr = (Drawable) context.getResources().getDrawable(R.drawable.blue_tint);
            dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
            dr.draw(canvas);
        }            
    }
}

tinted keys

In this case, I didn't use 9-patch images, but just some simple 50% transparent square images and achieved an effect where the existing buttons are merely tinted with the colors I wanted. To get a more custom result, I could make my background drawables 9-patch images and do the following. Note that the two keys with icons don't render correctly because the icons aren't defined as 9-patch images and I didn't make any special effort to allow them to scale well for this example. I also didn't address the use of different images/effects for the various states for the keys; others have shown how to do that.

@Override
public void onDraw(Canvas canvas) {
    // super.onDraw(canvas);

    List<Key> keys = getKeyboard().getKeys();
    for (Key key : keys) {
        if (key.codes[0] == 7) {
            NinePatchDrawable npd
                = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.red_key);
            npd.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
            npd.draw(canvas);

        } else {
            NinePatchDrawable npd
                = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.blue_key);
            npd.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
            npd.draw(canvas);
        }

        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(48);
        paint.setColor(Color.GRAY);

        if (key.label != null) {
            canvas.drawText(key.label.toString(), key.x + (key.width / 2),
                            key.y + (key.height / 2), paint);
        } else {
            key.icon.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
            key.icon.draw(canvas);
        }
    }
}    

replaced keys

scottt
  • 8,301
  • 1
  • 31
  • 41
  • how to change the popup background programatically when long press on key of custom keyboardview? – sneha desai Oct 07 '16 at 09:53
  • The link provided in the answer doesn't answer the question. – Maihan Nijat Oct 31 '16 at 01:11
  • @MaihanNijat, It wasn't supposed to by itself; it's just a convenience that provides a fairly standard style custom keyboard. The code explicitly added in the answer demonstrates a simplistic change to that project (or the similar code provided in the official docs and other S/O posts) to answer the OP's question. – scottt Nov 01 '16 at 05:17
  • @scottt what do you use to close keyboard? – ste9206 Jan 23 '17 at 15:39
  • @ste9206 I had done with the colorful keys but the text in the keys are not visible..Can you please help me?? – Sagar Gangawane Dec 03 '17 at 13:03
3

I created a keyboard app in which I use the KeyBackground property in KeyboardView, like so:

<KeyboardView android:keyBackground="@drawable/buttonbgselector" .../>

To do this dynamically I use the following code:

@Override 
public View onCreateInputView() {
    mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null);
    mInputView.setBackgroundResource(R.drawable.buttonbgselector);
    mInputView.setOnKeyboardActionListener(this);
    mInputView.setKeyboard(mQwertyKeyboard);
    return mInputView;
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
1

Keeping it simple, you should make class MyKeyboardView and do some hack similar to this.

public class MyKeyboardView extends android.inputmethodservice.KeyboardView {

    Context context;
    public MyKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context ;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/Hippie.otf");
        paint.setTypeface(font);
        paint.setTextSize(40);

        List<Key> keys = getKeyboard().getKeys();
        for(Key key: keys) { // int i = 0 ; switch(i) and implement your logic 

        if(key.pressed){
            NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.glow);
            npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height);
            npd.draw(canvas);
            if(key.label != null)
                canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint);
        }else if(key.modifier){  // boolean that defines key is function key
            NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.btn_keyboard_special);
            npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height);
            npd.draw(canvas);
            if(key.label != null)
                canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint);
        }


        break;
    }
}
Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
Vinit Siriah
  • 327
  • 3
  • 12