• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scrolling in Java code

 
Dirk Lucas
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have been searching the net and this forum but could not find a working answer. Context: I am a novice when it comes to programming. I am learning and I did some tutorials and am now writing an app using Eclipse environment.

I made a javapage witch adds buttons using Java (its a long list of vegetables so this I figured is handier than making a lot of XML pages + I learn more)

My problem: I made this list show on screen using the AVD. But is shows only 7 buttons. I would like to scroll down so I can see the other buttons as well. I do not know how to implement this and I find only XML solutions on the web which do not work since there is no container in XML in this case.

Would anyone here hold the answer! I would be quite happy to learn!

First, the Java code:



My XML code:


 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might think you are 'learning more' by writing the GUI code in Java, but even if you are, you are learning bad habits. XML GUIs are the best thing to happen to applications on any platform (in my opinion, of course). The correct way to do it is to use XML for the layouts (but not by hard coding a long list of buttons for your veggies... we will get to the correct way later).

For the ScrollView, your ScrollView should contain just one child view (the LinearLayout) and should be set at the activity's content pane. You should not set the layout file as the content pane, then add the LinearLayout to the ScrollView (as in your code) because the ScrollView would never become visible.

So, the correct way to do it using ScrollView would be to use an XML layout file with the outermost element being the ScrollView and the LinearLayout nested inside the ScrollView:

You would make the ScrollView match parent in height (so it's view port is the size of the screen), and the LinearLayout wrap content (so all the contents in the LinearLayout are layed out and the LinearLayout pushes the total needed space as the scrollable area to the ScrollView).

If you want to do it in Java code, without changing the XML for some reason, then you need to:
1) Get the LayoutInflator from the Activity
2) Use the LayoutInflator to inflate the XML layout into a view (probably should cast it to LinearLayout at this point)
3) Create a new ScrollView
4) Add the inflated layout from #2 into the ScrollView
5) Add the buttons to the LinearLayout
6) Set the ScrollView as the activity's Content Pane

Either of those two options will work, but neither is very good - your data (the list of food) becomes entwined in the GUI code, so it is hard to manage independently or isolate/troubleshoot one thing without the other. It also isn't very flexible - what happens when you use a Tablet and the view needs to change? Or when you get a bigger list and you want to display two columns? Or more information? Or pictures? Using a LinearLayout filled with buttons inside a ScrollView makes that kind of hard. What you want is something more flexible. And that is where AdapterViews and ArrayAdapters come into play. In your situation, the best case is to:
1) Make your layout a ListView. This replaces the LinearLayout and the ScrollView. It also means the views you put in to the list don't need to be buttons, because ListView handles selection/clicking. It also is very memory efficient so it works well on low memory devices or when your item list grows to huge levels.
Your main layout, I will call it all_food.xml file:

2) Create a second XML file that contains a TextView which will display the name of the food. For this scenario I will call it 'food_item.xml'.

3) Set R.layout.all_food as the content pane
4) In Java Code, create an ArrayAdapter, using R.layout.food_item as the resource id, and add the list of Food to it.
5) Then get the foodlist view (the ListView) and give it the ArrayAdapter

This assumes your Food class has a good toString() method. If it doesn't the easiest/best thing to do is make one (and while you are at it, make good hashCode() and equals() methods, neither of which are needed here but both of which are necessary to have a good 'value' object for future use). Also, you will eventually want to react to the user's touch. You would do that in your code by adding OnClickListeners to each button, then looking up the content of the Button to see which item was clicked on. With the AdapterView you set an OnItemClickedListener once on the AdapterView itself (not the views it displays). That gives you an onItemClicked() method which has everything you need to get direct access to the Food the user clicked on:


With this method you can easily swap different bits and pieces out without affecting other things. Need to show two columns instead of one? No problem, just put in a GridView instead of a ListView and nothing else needs to change. Want an icon, no problem, change your food_item.xml file, and maybe change the constructor in the ArrayAdapter, but nothing else needs to change. Need a complex view showing different values in the food? No problem, just a few changes tot he XML and extend the ArrayAdapter class. But the main UI and the Activity code don't need to change. Need to store food in a database, not problem, use a CursorAdapter. etc...

For more info:
http://developer.android.com/guide/topics/ui/binding.html
http://developer.android.com/guide/topics/ui/layout/listview.html
http://www.vogella.com/tutorials/AndroidListView/article.html
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+5 on using XML layouts instead of doing it programmatically.
 
Dirk Lucas
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waw Steve thank you for taking the time to write your extensive answer! I am going to test the given options a bit it and than think again how to implement.

I understand XML layouts work better. I might actually go for the listview in the end. I just feel that, to be a reasonable app creator I also need to fully understand Java. But I guess I will learn that anyway, since any interaction in the app will probably still be in Java;)
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, you will learn Java One of the most important things to learn is the separation of concerns. And using XML for the GUI helps separate GUI from business logic. The majority (if not all) of your logic/functionality will be in Java, so have no fear - you will be learning that too

Best of luck!
 
Dirk Lucas
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve, I am sorry to bug you again. The listview works as a charm.

However;

I have been trying to get the onItemSelectedListener working. But no matter what I try I keep getting "the method getItemAt is undefined for the type AdapterView".

Do you have a hint in the right direction maybe?

Edit: I should ad: The foodpage should link to another listview. This new page is also Adapterbased; each button in the Foodlist should give a different view based on timeclass below and uses 1 shared layout I made (time_listview & time_textview).


Timeclass:



 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dirk Lucas wrote:I have been trying to get the onItemSelectedListener working. But no matter what I try I keep getting "the method getItemAt is undefined for the type AdapterView".


Glad you got the ListView working The method should be getItemAtPosition(), I had a typo in the last post.
 
Dirk Lucas
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haaaaa! I was wondering why it was so hard to find info on getItemAt()! Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic