Coova
Coova

Reputation: 1858

Resize EditText to fit Text Size

I am looking to resize an EditText height, based on the height of the text size.

The reason I want to do this is because some users will set their text size to "Large" inside the Android Accessibility Options, and than some of the text will be cut off within my applications EditText views.

What can I do to dynamically adjust the height to match the Text Size? I do not need auto resizing or anything fancy.

Here is some code inside my custom EditText Class

    mCurrentTextSize = getTextSize();
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                                                                                                DisplayUtils.pixel2dp(getContext(), mCurrentTextSize));
    setLayoutParams(layoutParams);

Inside a Utility Class

public static int pixel2dp(Context context, float pixel) {
    DisplayMetrics metrics = new DisplayMetrics();  
    float logicalDensity = metrics.density;

    return (int) Math.ceil(pixel / logicalDensity);
}

Upvotes: 3

Views: 1725

Answers (1)

Akeshwar Jha
Akeshwar Jha

Reputation: 4576

Why not use layout_height=wrap_content? It will automatically re-adjust the EditText so as to contain the text.

Upvotes: 1

Related Questions