Android Question Help needed with ListView and ActionBar

Augustinas Impalis

Member
Licensed User
Longtime User
I have created a form in the designer and linked a ListView to one of the button, however the ListView overlaps the whole application and sticks to the screen can someone have a look at my code and explain to me how i can fix that? I want the ListView to appear on its own screen.

B4X:
Sub Process_Globals
    Private currentPage As Int
End Sub

Sub Globals
      Dim mFragment As MapFragment
      Dim gmap As GoogleMap
      Dim MapPanel As Panel
      Dim bar As StdActionBar
      Dim vp As StdViewPager
    Dim ListView1 As ListView
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
  MapPanel.Initialize("")
  bar.Initialize("bar")
  bar.NavigationMode = bar.NAVIGATION_MODE_TABS
 
  Dim h As Int = CalculateHeight(True, False)
  vp.Initialize("vp", 3, 100%x, h)
  MapPanel.Initialize("")
  Activity.AddView(vp.AsView, 0, 0, 100%x, h)
 
  vp.Panels(0).AddView(MapPanel,0, 0, 100%x, h)
  vp.Panels(1).Color = Colors.Red
  vp.Panels(2).LoadLayout("0")
 
  bar.AddTab("Map")
  bar.AddTab("Red panel")
  bar.AddTab("Equipment")
 
  'Google Maps
  If mFragment.IsGooglePlayServicesAvailable = False Then
  ToastMessageShow("Google Play services not available.", True)
  Else
  mFragment.Initialize("Map", MapPanel)
  End If
 
  'ListView
 
End Sub

Sub CalculateHeight (TabsMode As Boolean, SplitEnabled As Boolean) As Int
  If 100%x >= 480dip Then
    Return 100%y
  Else
    Dim fix As Int
    If TabsMode Then fix = 48dip
    If SplitEnabled Then fix = fix + 48dip
    Return 100%y - fix
  End If
End Sub

Sub VP_PageSelected (Position As Int)
  If bar.SelectedIndex <> Position Then bar.SelectedIndex = Position
End Sub

Sub bar_TabChanged(Index As Int, STab As StdTab)
  If vp.currentPage <> Index Then  vp.ScrollTo(Index, False)
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    currentPage = bar.SelectedIndex
End Sub


'Google Maps

Sub Map_Ready
  Log("map ready")
  gmap = mFragment.GetMap
  If gmap.IsInitialized = False Then
      ToastMessageShow("Error initializing map.", True)
  Else
      gmap.AddMarker(53.544133, -2.605544, "Royce's")
      Dim cp As CameraPosition
      cp.Initialize(53.544133, -2.605544, gmap.CameraPosition.Zoom)
      gmap.AnimateCamera(cp)
  End If
End Sub


Sub Machine_Button_Click
    ListView1.Initialize("ListView1")
    Dim GD As GradientDrawable
    GD.Initialize("TR_BL", Array As Int(Colors.White, Colors.LightGray))
    Activity.Background = GD
    ListView1.ScrollingBackgroundColor = Colors.Transparent
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize(File.DirAssets, "Pic1.jpg")
    Dim Bitmap2 As Bitmap
    Bitmap2.Initialize(File.DirAssets, "Pic2.jpg")
    Dim Bitmap3 As Bitmap
    Bitmap3.Initialize(File.DirAssets, "Pic3.jpg")
    Dim Bitmap4 As Bitmap
    Bitmap4.Initialize(File.DirAssets, "Pic4.jpg")
    Dim Bitmap5 As Bitmap
    Bitmap5.Initialize(File.DirAssets, "Pic5.jpg")
    Dim Bitmap6 As Bitmap
    Bitmap6.Initialize(File.DirAssets, "R.png")
    ListView1.TwoLinesAndBitmap.ItemHeight = 160dip
    ListView1.TwoLinesAndBitmap.ImageView.Height = 150dip
    ListView1.TwoLinesAndBitmap.ImageView.Width = 150dip
    ListView1.TwoLinesAndBitmap.Label.Gravity = Gravity.CENTER
    ListView1.TwoLinesAndBitmap.Label.TextColor = Colors.Blue
    ListView1.TwoLinesAndBitmap.SecondLabel.Gravity = Gravity.CENTER
    ListView1.TwoLinesAndBitmap.SecondLabel.TextColor = Colors.Cyan
    ListView1.SingleLineLayout.ItemHeight = 100dip
    ListView1.SingleLineLayout.Label.TextSize = 20
    ListView1.SingleLineLayout.Label.TextColor = Colors.Blue
    ListView1.SingleLineLayout.Label.Gravity = Gravity.CENTER
    ListView1.FastScrollEnabled = True
    ListView1.AddTwoLinesAndBitmap("Machine #1", "This is the second line.", Bitmap1)
    ListView1.AddTwoLinesAndBitmap("Machine #2", "This is the second line.", Bitmap2)
    ListView1.AddTwoLinesAndBitmap("Machine #3", "This is the second line.", Bitmap3)
    ListView1.AddTwoLinesAndBitmap("Machine #4", "This is the second line.", Bitmap4)
    ListView1.AddTwoLinesAndBitmap("Machine #5", "This is the second line.", Bitmap5)
    Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
End Sub
Sub Free_Weight_Button_Click
   
End Sub
Sub Cardio_Button_Click
   
End Sub
 

Attachments

  • Screenshot_2014-04-19-12-28-15.png
    Screenshot_2014-04-19-12-28-15.png
    481.2 KB · Views: 265

Augustinas Impalis

Member
Licensed User
Longtime User
Do you want the ListView to appear in one of the "tabs"? Your current code adds it to the activity. You should instead add it to one of the panels.

I tried to add it to a new panel but i am unsure how to declare it and how to assign the list view when a 'Machine_Button' is clicked, could you show me how it's done it would help a lot as am trying to add 3 ListViews under one tab.
 
Upvote 0

Augustinas Impalis

Member
Licensed User
Longtime User
Where is the ListView supported to be displayed? If you want to show it on the screen then it is usually best to show it in a new activity.

Different examples with 2 layouts

I want to add a new panel which will display the ListView when one of the buttons is clicked.. i want the list to appear on the same tab but with a new panel displaying this ListView then i could click back and clicked a different button to display a different ListView but i dont know how to code it
 
Upvote 0

Augustinas Impalis

Member
Licensed User
Longtime User
See the link I posted.
I recommend you to show it in a new activity. It will be simpler.

Right I got the panels to work however how do I assign the ListView code to the ListView component in the Designer (see image below pnlLayout2)? I don't know how to do it or where to put it, how can i modify the code below?

B4X:
Dim btnNext1, btnNext2 As Button
    Dim pnlLayout1, pnlLayout2 As Panel
    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("TwoLayouts")
    pnlLayout1.Top=0
    pnlLayout1.Left=0
    pnlLayout1.Visible=True

    pnlLayout2.Top=0
    pnlLayout2.Left=0
    pnlLayout2.Visible=False
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnNext1_Click
    pnlLayout1.Visible=False
    pnlLayout2.Visible=True
   
    ListView1.Initialize("ListView1")
    Dim GD As GradientDrawable
    GD.Initialize("TR_BL", Array As Int(Colors.White, Colors.LightGray))
    Activity.Background = GD
    ListView1.ScrollingBackgroundColor = Colors.Transparent
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize(File.DirAssets, "Pic1.jpg")
    Dim Bitmap2 As Bitmap
    Bitmap2.Initialize(File.DirAssets, "Pic2.jpg")
    Dim Bitmap3 As Bitmap
    Bitmap3.Initialize(File.DirAssets, "Pic3.jpg")
    Dim Bitmap4 As Bitmap
    Bitmap4.Initialize(File.DirAssets, "Pic4.jpg")
    Dim Bitmap5 As Bitmap
    Bitmap5.Initialize(File.DirAssets, "Pic5.jpg")
    ListView1.TwoLinesAndBitmap.ItemHeight = 160dip
    ListView1.TwoLinesAndBitmap.ImageView.Height = 150dip
    ListView1.TwoLinesAndBitmap.ImageView.Width = 150dip
    ListView1.TwoLinesAndBitmap.Label.Gravity = Gravity.CENTER
    ListView1.TwoLinesAndBitmap.Label.TextColor = Colors.Blue
    ListView1.TwoLinesAndBitmap.SecondLabel.Gravity = Gravity.CENTER
    ListView1.TwoLinesAndBitmap.SecondLabel.TextColor = Colors.Cyan
    ListView1.SingleLineLayout.ItemHeight = 100dip
    ListView1.SingleLineLayout.Label.TextSize = 20
    ListView1.SingleLineLayout.Label.TextColor = Colors.Blue
    ListView1.SingleLineLayout.Label.Gravity = Gravity.CENTER
    ListView1.FastScrollEnabled = True
    ListView1.AddTwoLinesAndBitmap("Machine #1", "This is the second line.", Bitmap1)
    ListView1.AddTwoLinesAndBitmap("Machine #2", "This is the second line.", Bitmap2)
    ListView1.AddTwoLinesAndBitmap("Machine #3", "This is the second line.", Bitmap3)
    ListView1.AddTwoLinesAndBitmap("Machine #4", "This is the second line.", Bitmap4)
    ListView1.AddTwoLinesAndBitmap("Machine #5", "This is the second line.", Bitmap5)
    Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
End Sub

Sub btnNext2_Click
    pnlLayout1.Visible=True
    pnlLayout2.Visible=False
End Sub
 

Attachments

  • ListView.png
    ListView.png
    28.4 KB · Views: 247
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately you don't give enough information.
Do you still have one Activity ?
Are you still usingTabHost ?
Are both Panels in the image in the same layout file ?
When you define Views in the Designer you must not initialze nor add them in the code !

You need to explain exactly what you want and post a project, as a zip file, showing the problem.
With just a few code snippets it's diffircult to undersand what you want to do, what you have done and how.
 
Upvote 0

Augustinas Impalis

Member
Licensed User
Longtime User
Unfortunately you don't give enough information.
Do you still have one Activity ?
Are you still usingTabHost ?
Are both Panels in the image in the same layout file ?
When you define Views in the Designer you must not initialze nor add them in the code !

You need to explain exactly what you want and post a project, as a zip file, showing the problem.
With just a few code snippets it's diffircult to undersand what you want to do, what you have done and how.

I apologize for not giving enough information klaus, I struggle with coding and I have been using the TwoPanelActivity from Erel's link...

I am trying to get that working first so that I can integrate it in the main project later.

Both panels are in the same layout file like the TwoPanelActivity example

About the Views in the Designer I didnt know about that as my coding knowledge is not great, How do I apply labels and buttons above the ListView?

I have added the zip

Second code snipped is the full code from the project

What do you suggest I do?

Thanks
 

Attachments

  • Project.zip
    7.9 KB · Views: 184
Upvote 0

klaus

Expert
Licensed User
Longtime User
Did you have a look at the Beginner's Guide ?
Might be intersting to get the first steps into B4A.
What Activites are, what their lifecycle is, how to use layouts etc.

Try to make a smaller project that shows the problem.
If the last code snippet is almost the whole project there is no reason that the project is that big to post here.
Use in the IDE the menu Files / Export As Zip,
Are your image files big files ?
Do you have lots of not used files in your project ?

If you want to display two different Panels in two different Activities you need two separate layout files.

You still haven't explained in detail what exactly you want to do, what you have done and how.
Very often, explaining in words what you want to do will clarify things especially for you.
 
Upvote 0

Augustinas Impalis

Member
Licensed User
Longtime User
Did you have a look at the Beginner's Guide ?
Might be intersting to get the first steps into B4A.
What Activites are, what their lifecycle is, how to use layouts etc.

Try to make a smaller project that shows the problem.
If the last code snippet is almost the whole project there is no reason that the project is that big to post here.
Use in the IDE the menu Files / Export As Zip,
Are your image files big files ?
Do you have lots of not used files in your project ?

If you want to display two different Panels in two different Activities you need two separate layout files.

You still haven't explained in detail what exactly you want to do, what you have done and how.
Very often, explaining in words what you want to do will clarify things especially for you.

Thank you for the suggestions klaus i will definitely have a look at that.

For the time being i have attached my project, how do I attach ListView1 in the Machine_ListView sub to one of the tabs in the action bar? to simply display the list in a tab.

Thank you!
 

Attachments

  • Project.zip
    135.6 KB · Views: 211
Upvote 0

klaus

Expert
Licensed User
Longtime User
It's the last time I ask you to explain exactly what you want to do !!!

I looked at your project:
I had to remove the map service stuff to make it run, I don't have one.
Images are missing !
There is no layout where you have a ListView !
What event from what view is supposed to call Machine_ListView ?
In what Tab is the ListView supposed to be shown ?
Why don't you add the ListView in the layout where you need it as Erel already told you. I don't know where you want it.
It's very frustrating trying to help poeple who don't really help you to help them !
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find your modified project with what I suppose your are looking for.
I added the ListView onto the Machine layout and splitted the Machine_ListView routine into two routines.
I suggest you to use meaningful names for layouts, views, variables etc.
 

Attachments

  • Project1.zip
    165.7 KB · Views: 218
Upvote 0
Top