A23149577
A23149577

Reputation: 2145

Android setTextSize changes editText height and shape

Here is my problem:

I want to change the editText text size in my java code. I have already defined its height and width. The issue is I want to set the text size in a way that the text appears completely in the editText and also its weight and height won't be changed. Here is my editText parameters definition and a snapshot of my UI (et[][] is my editTexts):

LayoutParams params = new LayoutParams(
                LayoutParams.WRAP_CONTENT,      
                LayoutParams.WRAP_CONTENT
        );

    params.setMargins(5, 5, 5, 5);

        et[z][0].setText(tmpDate);
        et[z][0].setLayoutParams(params);
        et[z][0].setTextSize(10);
        et[z][0].setWidth(45);
        et[z][0].setHeight(30);

        et[z][1].setText(tmpMoney);
        et[z][1].setLayoutParams(params);
        et[z][1].setWidth(90);
        et[z][1].setHeight(30);//this one is without setTextSize

enter image description here

As you can see, the shape and the margins for first editText is changed when I add setTextSize

Upvotes: 2

Views: 526

Answers (2)

Umer Kiani
Umer Kiani

Reputation: 3370

All you need to do is not to hard code the width and height of edittext. If edittext is set to fixed height and width it wont allow it to scale when text size is changed resulting in the issue what you are having. All you need to do is set the width and height of edittext to Wrap_Content this will work perfectly and scale your edittext accordingly and as required.

Hope this helps you. Stll need any help feel free to ask.

Upvotes: 0

Junior Buckeridge
Junior Buckeridge

Reputation: 2085

You need to remove the padding from the editText (and probably set the gravity property to "center"). If the edittext doesn't has any padding, then you're probably using a nine-patch drawable that you should change (if this is the case I recommend you to create an xml drawable for your edittext's background). If you need any further help let us now.

Hope it helps.

Upvotes: 1

Related Questions