Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-27438] Android: Toolbar "extendBackground" property ignored if added to window after open

GitHub Issuen/a
TypeBug
PriorityLow
StatusOpen
ResolutionUnresolved
Affected Version/sRelease 6.2.0
Fix Version/sn/a
ComponentsAndroid
Labelsandroid, engSchedule, toolbar
ReporterJoshua Quick
AssigneeJoshua Quick
Created2019-10-02T20:10:01.000+0000
Updated2019-10-14T20:56:02.000+0000

Description

*Summary:* If you add Toolbar to a Window after it has been opened, then Toolbar property "extendBackground" is ignored. The Toolbar will instead be overlapped by the top status bar. *Steps to reproduce:*

Build and run the below code on Android.

Tap on the "Add/Remove" button.

Notice that the toolbar added to the top is overlapped by the status bar. (This is the bug.)

var wasAdded = false;
var windowSettings = {
	extendSafeArea: true,
};
if (Ti.Android) {
	windowSettings.windowFlags = Ti.UI.Android.FLAG_TRANSLUCENT_STATUS;
	windowSettings.theme = "Theme.AppCompat.NoTitleBar";
}
var window = Ti.UI.createWindow(windowSettings);
var toolbar = Ti.UI.createToolbar({
	title: "My Toolbar",
	titleTextColor: "white",
	backgroundColor: "blue",
	extendBackground: true,
	top: 0,
//	bottom: 0,
	width: Ti.UI.FILL,
});
var button = Ti.UI.createButton({
	title: "Add/Remove Toolbar",
})
button.addEventListener("click", function() {
	if (wasAdded) {
		window.remove(toolbar);
	} else {
		window.add(toolbar);
	}
	wasAdded = !wasAdded;
});
window.add(button);
window.open();
*Result:* !Android-bad.png|thumbnail! *Expected Result:* !Android-good.png|thumbnail! *Recommended Solution:* Our toolbar Java code should call the [requestApplyInsets()](https://developer.android.com/reference/android/view/View#requestApplyInsets()) method when attached to the window. This requests the view hierarchy to re-dispatch the window insets to all child views, which is used to apply an internal padding to the toolbar needed to make "extendBackground" work. We'll need to add something like the following to our Toolbar code.
@Override
public void onAttachedToWindow()
{
	super.onAttachedToWindow();

	if (getFitsSystemWindows()) {
		if (android.os.Build.VERSION.SDK_INT >= 20) {
			requestApplyInsets();
		} else {
			requestFitSystemWindows();
		}
	}
}
*Work-around:* Add the Toolbar to the Window before opening it and use Toolbar methods show() and hide() to control its visibility instead.

Attachments

FileDateSize
Android-bad.png2019-10-02T20:01:32.000+000089642
Android-good.png2019-10-02T20:01:32.000+000089771

Comments

No comments

JSON Source