1

So basically what I need is that I need to update whole application TextView's font size and I don't want to create one by one variable for all TextView.

I try to extend TextView but seem can't find any method that is called everytime TextView appears on screen.

Tried this one:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
}

That method seems only called when TextView first appears on screen or I call setTextSize.

Any idea?

Rendy
  • 5,572
  • 15
  • 52
  • 95

2 Answers2

1

Tranversals life cycle events: animate -> measure -> layout -> draws

A view or activity can retrigger the measure and layout pass with a call to the requestLayout() method.

After the measure and layout calculation, the views draw themselves. This operation can be triggered with the invalidate() method from the View class.

You need call invalidate() to update TextView on screen

mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • So I still need to create all variables or findViewById for each my TextView I assume? – Rendy Dec 01 '15 at 13:30
  • if you only apply some property as font size to all textview i think you don't need custom textview, only put them to a style and put this style to xml. And you don't create variable. – mdtuyen Dec 01 '15 at 13:49
  • If have some property that TextView has not, you can add them to a attribute (xml file) and get them on custom textview and you can add these property to a style and everything same with original as I said – mdtuyen Dec 01 '15 at 13:50
  • I have tried the answer by Janarthanan from http://stackoverflow.com/questions/3241729/android-dynamically-change-style-at-runtime, but the font size isn't changed. – Rendy Dec 01 '15 at 15:05
  • following exact way in that answer but it seems the style is not applied – Rendy Dec 01 '15 at 15:18
  • got it works, previously because of I set android:textSize in the TextView's XML, but still doesn't managed it works to change the style not in OnCreate method. – Rendy Dec 01 '15 at 15:25
  • i dont understand, you can add style on layout file by style="@style/" why you need add it dynamically – mdtuyen Dec 01 '15 at 15:38
  • as I check code, setTextAppearance() put each property set in style to textview and call invalidate() so that you can call it any where. Not only onCreate() – mdtuyen Dec 01 '15 at 15:40
  • Using setTextAppearance need me to call all the TextView right? – Rendy Dec 01 '15 at 15:43
0

If the textview is in scroll view, you can use the below code to identify whether your text view currently in visible region of the screen.

Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (textView.getLocalVisibleRect(scrollBounds)) {
    // textView is within the visible window
} else {
    // textView is not within the visible window
}
Jayakrishnan Salim
  • 977
  • 1
  • 10
  • 24