Android Question the same view on each Activity

peacemaker

Expert
Licensed User
HI, All

I remember that seen somewhre, but cannot find.
If each activity has the same ACtoobar with a menu - the best way to make a class for this code batch.
The view is loaded from separated Layout files.
But full toolbar control must be saved.
How ?

Trying to make the class named "toolbar":

B4X:
Sub Class_Globals
    Private Act As Activity
    Dim ACToolBarLight1 As ACToolBarLight
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Activity As Activity, Title As String, SubTitle As String)
    Act = Activity
    For Each v As View In Act.GetAllViewsRecursive
        If v Is ACToolBarLight Then
            ACToolBarLight1 = v
            Exit
        End If
    Next
    If Title = "" Then
        ACToolBarLight1.Title = Application.LabelName
    Else
        ACToolBarLight1.Title = Title
    End If
    ACToolBarLight1.SubTitle = SubTitle
    ACToolBarLight1.InitMenuListener
End Sub

Sub Activity_CreateMenu(Menu As ACMenu)
    Menu.Clear
'    ACToolBarLight1.Menu.Add2(0, 0, "RU -> EN", Null)
 
End Sub

Sub ACToolBarLight1_MenuItemClick (Item As ACMenuItem)
    Log("Clicked: " & Item.Title)
    If Item.Id = 0 Then 'back
    else If Item.Id = 1 Then    'settings
        'StartActivity(Prefs1)
    else If Item.Id = 2 Then    'exit
        'Exit_Click
    End If
End Sub

#If Java
public boolean _onCreateOptionsMenu (android.view.Menu menu)
    { try { _activity_createmenu (new de.amberhome.objects.appcompat.ACMenuWrapper (menu)); return true; }
    catch (Exception e) { return false; } }
#End If
And error during compilation:

B4X:
B4A line: 4
End Sub
javac 1.8.0_151
src\peacemaker\upravdom\toolbar.java:156: error: method _activity_createmenu in class toolbar cannot be applied to given types;
    { try { _activity_createmenu (new de.amberhome.objects.appcompat.ACMenuWrapper (menu)); return true; }
            ^
  required: toolbar,ACMenuWrapper
  found: ACMenuWrapper
  reason: actual and formal argument lists differ in length
 
Last edited:

peacemaker

Expert
Licensed User
No :(

B4X:
Compiling generated Java code.    Error
B4A line: 68
End Sub
javac 1.8.0_151
src\peacemaker\upravdom\main2.java:486: error: cannot find symbol
    { try { _activity_createmenu (new de.amberhome.objects.appcompat.ACMenuWrapper (menu)); return true; }
            ^
  symbol:   method _activity_createmenu(ACMenuWrapper)
  location: class main2
Task is to incapsulate whole this same code batch into a single place, and only get events at menu taps.
 
Last edited:

peacemaker

Expert
Licensed User
Indeed, forgotten.
v.0.2 is attached, error the same.
 

Attachments

Semen Matusovskiy

Well-Known Member
Licensed User
Cut Activity.LoadLayout("Main2") in main activity and place it in class after Public Sub Initialize(Activity As Activity, Title As String, SubTitle As String)
 

peacemaker

Expert
Licensed User
Opps, my and your projects are both compiled OK in the release mode only!
If debug - this error:


B4X:
B4A Version: 8.50
Parsing code.    (0.01s)
Building folders structure.    (0.00s)
Compiling code.    (1.06s)
Compiling layouts code.    (0.02s)
Organizing libraries.    (1.49s)
Generating R file.    (0.57s)
Compiling debugger engine code.    (0.75s)
Compiling generated Java code.    Error
B4A line: 4
End Sub
javac 1.8.0_151
src\b4a\example\actoolbarinclass\clstoolbar.java:150: error: method _activity_createmenu in class clstoolbar cannot be applied to given types;
    { try { _activity_createmenu (new de.amberhome.objects.appcompat.ACMenuWrapper (menu)); return true; }
            ^
  required: clstoolbar,ACMenuWrapper
  found: ACMenuWrapper
  reason: actual and formal argument lists differ in length
 

Semen Matusovskiy

Well-Known Member
Licensed User
Well, I see. But there are much more serious problems. At first, _onCreateOptionsMenu should do be in Activity and not clear how to coordinate with class.

So, if to use a class, probably, following way is more comfortable.

In main activity. Remove all beginning with Sub Process_Globals and paste
B4X:
Sub Process_Globals
End Sub

Sub Globals
    Private tb As clsToolbar
End Sub

Sub Activity_Create (FirstTime As Boolean)
    tb.Initialize (Activity, "Hello, World", "Toolbar class for any activity")  
End Sub

Sub Exit_Click
    Activity.Finish
End Sub

Sub Enter_Click
    tb.ACToolBarLight1.SubTitleTextColor = Colors.Magenta
End Sub

Sub Activity_Finish
    Log ("Finish")
End Sub
Class (full text)
B4X:
Sub Class_Globals
    Private Act As Activity
    Dim ACToolBarLight1 As ACToolBarLight
End Sub

Public Sub Initialize (Activity As Activity, Title As String, SubTitle As String)
    Activity.LoadLayout("Main2")
    Activity.AddMenuItem ("Enter", "Enter")
    Activity.AddMenuItem ("Exit", "Exit")
    Act = Activity
  
    ACToolBarLight1.Title = Application.LabelName
    ACToolBarLight1.Title = Title
    ACToolBarLight1.SubTitle = SubTitle
    ACToolBarLight1.InitMenuListener
End Sub
This is possible to compile in debug mode also.
 

peacemaker

Expert
Licensed User
Munues Activity.AddMenuItem are not visible on Appcompat activity.
 

peacemaker

Expert
Licensed User
Wow, indeed !
Add into your activity code
B4X:
#Extends: android.support.v7.app.AppCompatActivity
 
Top