A. Steenbergen
A. Steenbergen

Reputation: 3440

Resizing Text programmatically, text out of Viewbounds, fix?

I am trying to resize rows in a linear layout, depending on the amount of rows in the layout (the layout has fixed height). The rows are supposed to always completely fill the parent layout. So 1 row is the full size of the parent, 2 rows both are half the size etc. This works, I am now trying to resize the text aswell, but it gets cut off when I resize it, even though the textview is apearantly big enough (i switched on the show view bounds function of jelly bean)

private void setHeightOfRows(ViewGroup parent) {
    int height = parent.getHeight();
    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        LinearLayout.LayoutParams p = (LayoutParams) parent.getChildAt(i).getLayoutParams();
        p.height = height / childCount;
        parent.getChildAt(i).setLayoutParams(p);
        ((ViewGroup) parent.getChildAt(i)).setClipChildren(false);
        Log.i("FontSize", "Set font size to " + 10 / childCount);
        ((TextView) parent.getChildAt(i).findViewById(R.id.name_text)).setTextScaleX(10 / childCount);// .setTextSize(10/childCount);
    }
}

This method gets executed in a ViewTreeObserver, so after the UI is rendered.

How can I resize the text without having it cut off?

Upvotes: 1

Views: 483

Answers (2)

Dheeraj Bhaskar
Dheeraj Bhaskar

Reputation: 19039

Simple problem. Simple Solution MUST exist. :)

  • Have you considered using android:padding and android:margin.
  • How about minHeaight and minWidth?

OR

  • If you're doing by code, there is a setPadding there must be a similar thing for Margin too.
  • There is a setMinHeight and setMinWidth

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93542

SetTextScaleX stretches text in the X direction (horizontally). It looks more like you want to shrink text so its smaller top to bottom, right? In that case you don't want setTextScaleX, you want setTextSize.

Upvotes: 1

Related Questions