Jahusek
Jahusek

Reputation: 117

textView.getLineCount() returns 0 after setTextSize method

I'm trying to resize my textview to fit in 1 line.

while (titleText.getLineCount() > 1) {
    float scaledDensity = ManagerStorage.mainActivity.getResources().getDisplayMetrics().scaledDensity;
    titleText.setTextSize(titleText.getTextSize() / scaledDensity - 0.5f);
    Log.i("lines", "" + titleText.getLineCount());
    }

However, while loop executes only once. After setTextSize method, getLineCount will always return 0. While loop is executed with runOnUiThread on runnable that works after view is returned by onCreateView method.

Upvotes: 2

Views: 1206

Answers (1)

Muhammed Arshad K
Muhammed Arshad K

Reputation: 495

Try this

while (titleText.getLineCount() > 1) {
        float scaledDensity = ManagerStorage.mainActivity.getResources().getDisplayMetrics().scaledDensity;
        titleText.setTextSize(titleText.getTextSize() / scaledDensity - 0.5f);

        titleText.post(new Runnable() {
            @Override
            public void run() {
                int lineCount = titleText.getLineCount();
                Log.i("lines", "" + titleText.getLineCount());
                // Use lineCount here
            }
        });
    }

Upvotes: 3

Related Questions