Android Question Using a ToolBar as ActionBar at Runtime

SNOUHyhQs2

Member
Licensed User
Longtime User
Its my first time using Toolbar (https://www.b4x.com/android/forum/t...3-using-a-toolbar-as-actionbar.49053/#content) (AppCompat 3.30)

I dont really use Layout (designer) so i always initialize and add most of my object/views via code.

Here is my code, and i cant see my toolbar. Is this a bug?

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim mainToolbar As ACToolBarLight
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.Initialize("Activity") 'toolbar not visible even if i remove this
   
   mainToolbar.Initialize("mainToolbar")
   Activity.AddView(mainToolbar, 0dip, 0dip, 100%x, 56dip)
 
   mainToolbar.PopupTheme = mainToolbar.THEME_LIGHT
   mainToolbar.Title = "Title"
   mainToolbar.TitleTextColor = Colors.White
   mainToolbar.SubTitle = "Sub Title"
   mainToolbar.SubTitleTextColor = Colors.White
   mainToolbar.Elevation = 4dip
   
End Sub

Toolbar will only be visible if I apply colors on it;
B4X:
    Dim cd As ColorDrawable
    cd.Initialize(0xFFF44336, 0)
    mainToolbar.Background = cd


B4X:
SetApplicationAttribute(android:theme, "@style/MyAppTheme")

'Theme
CreateResource(values, theme.xml,
<resources>
    <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#F44336</item>
        <item name="colorPrimaryDark">#D32F2F</item>
        <item name="colorAccent">#FFC107</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="windowActionModeOverlay">true</item>       
    </style>
</resources>
)
 
Top