thetheodor
thetheodor

Reputation: 248

(How to set) / (can't see) the cursor on a Textfield in Libgdx?

I have tried to create a drawable (ninepatch) and in textfieldstyle done this

greenTextFieldStyle.cursor = getSkin().getDrawable("textFieldCursor1");

But I can't see any cursor...

This is the current cursor image, I have also tried a 1 * 20 image without success.

It's really small

it's basically a 3 * 3 image, with a + in the middle and points outside for the ninepatch.

Upvotes: 4

Views: 3657

Answers (1)

bemeyer
bemeyer

Reputation: 6231

Use getSkin().newDrawable("textFieldCursor1",Color.green) and add a min width to it greenTextFieldStyle.cursor.setMinWidth(2f);

here a compleat Skin inclusive "selectionframe":

Skin skin = new Skin();
skin.add(
        "background",
        new NinePatch(this.game.manager.get("hud/ninepatchframe.png",
                Texture.class), 5, 5, 5, 5));
skin.add("cursor", this.game.manager.get("data/cursor.png"));

TextFieldStyle tStyle = new TextFieldStyle();
tStyle.font = getDefaultFont(25); //here i get the font
tStyle.fontColor = Color.BLACK;
tStyle.background = skin.getDrawable("background");
tStyle.cursor = skin.newDrawable("cursor", Color.GREEN);
tStyle.cursor.setMinWidth(2f);
tStyle.selection = skin.newDrawable("background", 0.5f, 0.5f, 0.5f,
        0.5f);

this.nameField = new TextField("enter name here", tStyle);

Here how i get the Bitmap Font. I am using the extansion from libgdx to create it from a .ttf. Take a look at the link how to add the nativ files.

Here the code how to use it:

public BitmapFont getDefaultFont(int size) {
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(
            Gdx.files.internal("fonts/font.ttf"));
    defaultFont = generator.generateFont(size + 5); //some offset
    return defaultFont;
}

Upvotes: 4

Related Questions