Android Question How to make horizontal scrollable menu?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Have a simple menubar (using AppCompat), setup like this:

B4X:
Sub TextToBitmap(s As String, FontSize As Float, lFontColour As Long, _
     iWidth As Int, iHeight As Int, _
     TF As Typeface, strAlignment As String) As Bitmap
 
 Dim bmp As Bitmap
 Dim cvs As Canvas
  
 bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
 cvs.Initialize2(bmp)
 Dim h As Double = cvs.MeasureStringHeight(s, TF, FontSize)
 cvs.DrawText(s, bmp.Width / 2, bmp.Height / 2 + h / 2, TF, FontSize, lFontColour, strAlignment)
  
 Return cvs.Bitmap
 
End Sub

#IgnoreWarnings: 11
Sub Activity_CreateMenu(Menu As ACMenu)
 
 Dim item As ACMenuItem
 Dim oIcon As BitmapDrawable
 
 Menu.Clear
 
 oIcon.Initialize(TextToBitmap("F", 24, Colors.RGB(0, 0, 0), 28, 24, Typeface.DEFAULT, "CENTER"))
 item = Menu.Add2(1, 1, " Find", oIcon)
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 
 oIcon.Initialize(TextToBitmap("P", 24, Colors.RGB(0, 0, 255), 28, 24, Typeface.DEFAULT, "CENTER"))
 item = Menu.Add2(2, 2, " Pat", oIcon)
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 
 oIcon.Initialize(TextToBitmap("D", 24, Colors.RGB(255, 0, 0), 28, 24, Typeface.DEFAULT, "CENTER"))
 item = Menu.Add2(3, 3, " Drugs", oIcon)
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 
 oIcon.Initialize(TextToBitmap("V", 24, Colors.RGB(0, 192, 0), 28, 24, Typeface.DEFAULT, "CENTER"))
 item = Menu.Add2(4, 4, " Vals", oIcon)
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 
 oIcon.Initialize(TextToBitmap("C", 24, Colors.RGB(255, 0, 255), 28, 24, Typeface.DEFAULT, "CENTER"))
 item = Menu.Add2(5, 5, " Clin", oIcon)
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 
 oIcon.Initialize(TextToBitmap("B", 24, Colors.RGB(0, 224, 224), 28, 24, Typeface.DEFAULT, "CENTER"))
 item = Menu.Add2(6, 6, " BP", oIcon)
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
 
 oIcon.Initialize(TextToBitmap("M", 24, Colors.RGB(0, 0, 0), 28, 24, Typeface.DEFAULT, "CENTER"))
 item = Menu.Add2(7, 7, " Map", oIcon)
 item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS

End Sub

Now I have run out of space and there is a large overflow menu, which I haven't shown in the above code. What I would like to do is move some items from the overflow menu to the above main menu
and make the main menu somehow horizontally scrollable. So swiping it from right to left will show the next group of menu items.
What would be best/simplest way to do this?
Any simple example somewhere that demonstrates this?

RBS
 

Attachments

  • img1.png
    10.4 KB · Views: 404

udg

Expert
Licensed User
Longtime User
I had a very old library of mine that could handle something similar: dgActionBar.
If interested I could look for that code and share what I find there. Presumably it was made up of an imageview (logo on far left), an horizontal scrollview (scrolling images for primary menu) and the "hamburger menu" on its far right.
Just keep in mind it is very old..
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User

Sounds a bit complex to me and maybe simplest solution is to make the logo clickable and on clicking that show a new menu with different icons.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Sounds a bit complex to me and maybe simplest solution is to make the logo clickable and on clicking that show a new menu with different icons.

RBS

Mostly solved this by not setting a logo (not doing: ActionBar.LogoBitmap = bmpIcon64) and adding instead a button to the ActionBar:

B4X:
 btnLogo.Initialize("btnLogo")
 btnLogo.SetBackgroundImage(bmpIcon64)
 ActionBar.AddView(btnLogo, 28dip, 28dip, 0)

Clicking the logo button rotates the menu groups by removing and adding menu items.

There are 2 minor problems still:
1. Logo image is now after the ActionBar Title.
2. There is a bit of a wasted gap before the ActionBar Title.

See attached image.

Any suggestions how to fix these 2 problems?


RBS
 

Attachments

  • img2.png
    10.8 KB · Views: 322
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…