InflationX / Calligraphy

Custom fonts in Android the easy way...
Apache License 2.0
1.09k stars 82 forks source link

Toolbar's subtitle is cleared if subtitle set programmatically #63

Open idunnololz opened 10 months ago

idunnololz commented 10 months ago

I have a toolbar whose subtitle is set programmatically based on arguments supplied to the fragment. If I use Calligraphy, the subtitle is cleared after it is set. The culprit is Calligraphy.

Calligraphy sets a ToolbarLayoutListener whenever a layout is inflated that contained a Toolbar. When this listener is registered, it immediately takes a snapshot of the value of the subtitle. On global layout, it then restores the value of the subtitle to the value it snapshotted. The issue is, if I set the subtitle of the Toolbar between these events (eg. in onViewCreated()), then that value is discarded.

I'm not certain why Calligraphy needs to snapshot the subtitle but this behavior is unintuitive and the only fix I can think of is registering my own GlobalLayoutListener and setting the subtitle after Calligraphy is done restoring the snapshotted subtitle.

Code where caligraphy registers the global layout listener: https://github.com/InflationX/Calligraphy/blob/master/calligraphy/src/main/java/io/github/inflationx/calligraphy3/Calligraphy.java#L158 Code where caligraphy is snapshotting the value of the subtitle: https://github.com/InflationX/Calligraphy/blob/master/calligraphy/src/main/java/io/github/inflationx/calligraphy3/Calligraphy.java#L230 Code where caligraphy restores the value of the subtitle: https://github.com/InflationX/Calligraphy/blob/master/calligraphy/src/main/java/io/github/inflationx/calligraphy3/Calligraphy.java#L253