Reputation: 41
I have used "Android Icon Set" (in eclipse), which has created different size icons in various drawable folders. Now when I am setting this icon using following code:
<Key android:codes="49" android:keyIcon="@drawable/my1" android:keyWidth="7%p"/>
Icon is not taking the full keysize, it is coming in center for some devices and coming outside the key in other devices. I need to fix this urgently please guide me. My main requirement is that I want certain different color keys on my custom keyboard (based on samplesoft keyboard), as it is difficult to change key background I am using images and now images are not occupying full space.
Upvotes: 3
Views: 3137
Reputation: 327
If you tired of finding the solution. you must try to override onDraw() of your KeyboardView with your own logic like i did.
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();
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(25);
paint.setColor(Color.RED);
List<Key> keys = getKeyboard().getKeys();
for(Key key: keys) {
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;
}
}
Upvotes: 2
Reputation: 1646
I actually just ran into this problem myself. Unfortunately I concluded that for the amount of effort to get it working I would be better off ignoring KeyboardView and just building my own using buttons. KeyboardView doesn't provide anything that is terribly valuable, and by building your own you get full control.
Looking at the source code for the android built in keyboard https://android.googlesource.com/platform/packages/inputmethods/LatinIME/ I see google has re-implemented their own KeyboardView which supports different background colours along with many other features. If my keyboard needs were complex I would have considered extracting the relevant code from the LatinIME project and re-using that.
Upvotes: 3
Reputation: 6788
Can't you use the colors like that:
in the folder drawable
, create key_diff.xml
, which should contain:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#AABBCC"/>
</shape>
And for example, create a second shape, for the normal keys (keys_norm.xml
):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#0000CC"/>
</shape>
In your case I think that the difficult thing is to control the size of the drawabe, in case it's an image (considering also that the "container" has a fixed ratio size and the bg of it can not be controlled).
Upvotes: -2