B4A Library [Class] SlideMenu

This is a class that implements a sliding menu as seen in many apps like Google+, Evernote, Facebook etc.

Requires B4A 2.0 or newer.
 

Attachments

  • SlideMenuClass_1_0.zip
    12.6 KB · Views: 5,158
  • screenshot-1340094419260.png
    screenshot-1340094419260.png
    32.3 KB · Views: 6,447

Yafuhenk

Active Member
Licensed User
Longtime User
What does say your log when the app crashes ?

B4X:
java.lang.RuntimeException: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=75.90512, y[0]=291.39294, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8462230, downTime=8460963, deviceId=2, source=0x1002 } recycled twice!
   at android.view.MotionEvent.recycle(MotionEvent.java:1660)
   at android.view.ViewRootImpl.finishMotionEvent(ViewRootImpl.java:2942)
   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2933)
   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2492)
   at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:870)
   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2501)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4424)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   at dalvik.system.NativeStart.main(Native Method)

Henk
 

Yafuhenk

Active Member
Licensed User
Longtime User
Thanks Informatix!

That did the job. I changed CallSub2 in CallSubDelayed2 in Slidemenu Class

B4X:
Private Sub mListView_ItemClick (Position As Int, Value As Object)
   Dim subname As String
   Hide   
   subname = mEventName & "_Click"
   If SubExists(mModule, subname) Then
      'CallSub2(mModule, subname, Value)
      CallSubDelayed2(mModule, subname, Value)
   End If
End Sub

Henk
 

melamoud

Active Member
Licensed User
Longtime User
minor improvment willbe to set the height of the slidePanel to the height of the list, so it wont show a very high white bottom

this code works for me is that all I need to do ?

At class_globals add
Private itemHeight As Int

at init sub add this line
itemHeight = mListView.TwoLinesAndBitmap.itemHeight + 2


at the end of addItem sub add this
B4X:
   mSlidePanel.Height = mListView.Size * itemHeight - 2
   If (mSlidePanel.Height > 100%y - mSlidePanel.Top) Then mSlidePanel.Height = 100%y - mSlidePanel.Top
   mListView.Height = mSlidePanel.Height
 

melamoud

Active Member
Licensed User
Longtime User
And if you add this to the end of addItem sub
B4X:
Dim c As Canvas
   c.Initialize(mListView)
   Dim w As Int = c.MeasureStringWidth(Text,mListView.TwoLinesAndBitmap.Label.Typeface,mListView.TwoLinesAndBitmap.Label.TextSize) + mListView.TwoLinesAndBitmap.ImageView.Width   
   Log ("w=" & w)
   If (w > mSlidePanel.Width ) Then ' if smaller no need to change, leave the width as the wider of all labels
      mSlidePanel.Width = w
      mListView.Width = w
   End If

you get automatic width no need to set width to begin with

is that make sense ?
 

melamoud

Active Member
Licensed User
Longtime User
full and improved addItem Sub

I fixed some of the calculations and make it grow from the bottom up (comment the ...top part if you do not want it like that:
B4X:
Public Sub AddItem(Text As String, Image As Bitmap, ReturnValue As Object)
   Dim item As ActionItem
   item.Initialize
   item.Text = Text
   item.Image = Image
   item.Value = ReturnValue
   
   If Not(Image.IsInitialized) Then
      mListView.AddTwoLinesAndBitmap2(Text, "", Null, ReturnValue)
   Else
      mListView.AddTwoLinesAndBitmap2(Text, "", Image, ReturnValue)
   End If
   'Log (mListView.Size & " items Hight:" & (mListView.Size * itemHeight))
            
   'mSlidePanel.Height = mListView.Size * itemHeight - 2
   'If (mSlidePanel.Height > 100%y - mSlidePanel.Top) Then mSlidePanel.Height = 100%y - mSlidePanel.Top

   mSlidePanel.Height = mListView.Size * itemHeight - 2   
   If (mSlidePanel.Height > 100%y) Then mSlidePanel.Height = 100%y
   mSlidePanel.Top = 100%y-mSlidePanel.Height ' set to grow from the bottom      
   mListView.Height = mSlidePanel.Height 
   'log ("width=" & mlistView.Width)
   Dim c As Canvas
   c.Initialize(mListView)
   Dim w As Int = c.MeasureStringWidth(Text,mListView.TwoLinesAndBitmap.Label.Typeface,mListView.TwoLinesAndBitmap.Label.TextSize) + mListView.TwoLinesAndBitmap.Label.Left+ 10
   'Log ("w=" & w)
   If (w>100%x) Then w = 100%x
   If (w > mSlidePanel.Width ) Then ' if smaller no need to change, leave the width as the wider of all labels
      'Log ("setting width to be:" & w)
      mSlidePanel.Width = w
      mListView.Width = w      
   End If
End Sub


you will need to add this var Private itemHeight As Int
AND ADD IT IN INIT as
itemHeight = mListView.TwoLinesAndBitmap.itemHeight + 2
 

rafaelbr20

Member
Licensed User
Longtime User
Very Nice Library !!!

How can i change the color when the item is clicked ? By Default is blue !!

Thanks

Rafael
 

corwin42

Expert
Licensed User
Longtime User
The list is done with a standard ListView object which does not have the possibility to change the color (It should be the system default color for selected list items). If you want to change the color you will have to modify the class and use something like a scrollview or the mListView library or UltimateListView (not free) for this.
 

rafaelbr20

Member
Licensed User
Longtime User
The list is done with a standard ListView object which does not have the possibility to change the color (It should be the system default color for selected list items). If you want to change the color you will have to modify the class and use something like a scrollview or the mListView library or UltimateListView (not free) for this.

No problem ! Thanks So Much !!
 

LucaMs

Expert
Licensed User
Longtime User
The haste and laziness are my biggest problem (I hope that it is not only for me :) )

I tried your example, launching a second activity from one item on your menu. Going back (act2 finish), the menu does not appear. Probably ust change something in the main, in the resume.

But this is not my question:

I'm wrong or the menu is "linked" to the activity? (given the initialization, I'd say I'm not mistaken).

How can you get the menu that is common to all the acvitities?

Thank you in advance

(ps. remember, I'm using Google Translate :rolleyes:)
 

Ratna Fang

Member
Licensed User
Longtime User
hi corwin,
i'm trying to implement sliding menu. so far it works properly, and it's nice.
but i found something weird.

i use sliding menu, additem about 5 or 6 items.
to get the value when user choose the menu, i use:
B4X:
Sub SlideMenu_Click(Item As Object)
    Dim ix As Int

    ix=Item

    Select Case ix
    Case 1
        'profile
        StartActivity(ProfileForm)
    Case 2
        'new
        StartActivity(NewArticle)
    Case 3
        'search
    Case 4
        'Category
    Case 5
        'About
        StartActivity(AboutUs)
    Case 6
        'logout
     
        StartActivity(LoginForm)
        Activity.Finish
    End Select
End Sub

the slide menu was working good and calls the correct action using slide or menu button. but after i got back from the called activity (pressing back to get back to this current form which has a slide menu), i can't slide the menu, i can't event call the slide menu with menu button on below. it's stopped. i have to press the power button to turn off the screen, wait for a moment (10-20 seconds), then turn the screen on again to access the slide menu.

is there anything i should type, perhaps in Activity_Resume?
here is the activity log:
B4X:
LogCat connected to: B4A-Bridge: Hisense AD683G-a1000033e86438
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
Installing file.
PackageAdded: package:rony.cerita.seru
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
startService: class anywheresoftware.b4a.samples.httputils2.httputils2service
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (menuutama) Create, isFirst = true **
** Activity (menuutama) Resume **
** Activity (menuutama) Pause, UserClosed = false **
** Activity (profileform) Create, isFirst = true **
** Activity (profileform) Resume **
** Activity (profileform) Pause, UserClosed = true **
** Activity (menuutama) Resume **
** Activity (menuutama) Pause, UserClosed = false **
** Activity (menuutama) Resume **
** Activity (menuutama) Pause, UserClosed = false **

profileform has a slidemenu and it is called from menuutama using slidemenu.
profileform has never failed but menuutama will hang for a while then it works again.

something wrong with slidemenu when Resume event is raised.

thanks :)
 

Douglas Farias

Expert
Licensed User
Longtime User
how do i delete the lines that separate each item?
 

GMan

Well-Known Member
Licensed User
Longtime User
I am using the sample code:
B4X:
Private Sub mListView_ItemClick (Position As Int, Value As Object)
  Dim subname As String
  Hide
  subname = mEventName & "_Click"
  If SubExists(mModule, subname) Then
      'CallSub2(mModule, subname, Value)
      CallSubDelayed2(mModule, subname, Value)
  End If
End Sub

But nothing happens except the ToastMessage was shown.

I tried also
B4X:
If SubExists(Main, subname) Then
        ''CallSub2(Main, subname, Value)
        CallSubDelayed2(Main, subname, Value)
End If
(replace mModule with Main (where the subs are placed) but also nohting happened
 
Top