0

I have a custom keyboard in my application, want to change text color at runtime based on user preference. I'm able to set KeyTextColor in XML, but no such attribute to set it programmatically. This is how i set in Xml :

<?xml version="1.0" encoding="utf-8"?>
<app:android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:keyBackground="@drawable/key_background"
    android:keyPreviewHeight="@dimen/dp_0"
    android:keyTextSize="40sp"
    android:keyTextColor="#00C853">//I set green text color here
</app:android.inputmethodservice.KeyboardView>

Want to set the same KeyTextColor from the program . Any ideas?

2 Answers2

1

This is not exactly what you asked, but it solved my problem. You can define your different themes by adding different keyboard.xml in layout folder(like the one in your question); And change them runtime.

@Override
public View onCreateInputView() {

    ...

    int theme_id=keyboard_prefs.getKeyboardThemeID();

    if(theme_id== KeyboardConstants.KEYBOARD_THEME_DARK_ID)
        mInputView=(LatinKeyboardView) getLayoutInflater().inflate(R.layout.keyboard_dark, null);
    else //if(theme_id==2)
        mInputView=(LatinKeyboardView) getLayoutInflater().inflate(R.layout.keyboard_light, null);

    }    
touhid udoy
  • 4,005
  • 2
  • 18
  • 31
0

First, create a class (suppose its name is mKeyboardView) that extends from the KeyboardView.
Then change your XML tag and make the colors in android:keyTextColor = transparent as :

   <com.example.mKeyboardView
    ...
    android:keyTextColor="@android:color/transparent"
    >
    </com.example.mKeyboardView>

Then in mKeyboardView override the onDraw function, and draw the letters manually as follows:

public class mKeyboardView extends KeyboardView

    @ColorInt private int MY_COLOR = 0XFF263238;

    @Override
     public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
            
        List<Keyboard.Key> keys = getKeyboard().getKeys();
            for (Keyboard.Key key : keys) {
                drawKey(key, canvas);
            }
        }

    Paint setupKeyText() {
        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(70);
        paint.setFakeBoldText(false);
        paint.setColor(MY_COLOR);
    
        return paint;
    }

    void drawKeyText(Keyboard.Key key, Canvas canvas) {
        if (key.label != null && !key.label.toString().isEmpty()) {
            Paint paint = setupKeyText();
            int x = key.x + (int) (key.width / 2.0);
            int y = key.y + (int) ((key.height /2) + (key.height /3.5));
    
            canvas.drawText(key.label.toString(), x , y, paint);
        }
    }
}

As in the above code, you can control the font size, color, thickness, and many other properties.

Hope my answer helps someone.