0

I'm new to Android programming and I'm working on my first application "FunFacts" where there should be more than 100 cool things to learn about nature, science etc. My question is: How can I switch from one page to another using two buttons "left" and "right"? which method should I use? I don't think that a horizontal scrollview is the solution.

So if I have 2 different layouts, page one and page two, how can I switch from one to other in my application?

Thank you!

kabuko
  • 36,028
  • 10
  • 80
  • 93

1 Answers1

0

It sounds like you might want a ViewPager. Below is a link to the Android tutorial. This creates a slideshow type effect which sounds like what you're going for. You said you will have different layouts which this will use one layout but you can customize the content and I would think you would want one so it all looks relatively the same

Docs

Update

No, you don't have to create 100 layouts or activities. Just one of each. Following will be a link to a post that explains it a little better. That post explains how it is like a listview in that you add the different pages to your view adapter as you would with a list. You can use the same layout for all and just add new data to it (like a list). Probably store them in an ArrayList or possibly a DB if you have that many and want to have a lot of data attached to them. I hope this makes sense and helps a little. I would suggest starting one with just a couple pages to see how it works, following these examples, and if you get stuck then post a more specific question as to what you can't get to work.

Link

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • but how can i switch the pages by clicking a button? – Ronald Reagan Jan 09 '13 at 21:44
  • Here is a link to another post that shows it pretty well, I think [http://stackoverflow.com/questions/10852744/android-viewpager-prev-next-button] Hope this helps – codeMagic Jan 09 '13 at 21:51
  • If this answer works for what you need, you may kindly accept the answer :) If not let me know and I will try to help more – codeMagic Jan 09 '13 at 22:56
  • thank you..Your answer was great but.. I read some examples of pageviewer in android but the concept is still not clear to me.. i mean i still don't understand how it works.. there are no videos about this so i hope that someone could help me to understand how it works with a copmplete axample code or with a file. Thank you so much. – Ronald Reagan Jan 10 '13 at 17:22
  • Did you follow the link that I posted with my answer? Here is another in the docs if you haven't followed it yet http://developer.android.com/reference/android/support/v4/view/ViewPager.html Those show a pretty good way of getting started. Try something simple with it then build on it. It would be much easier to answer if you started one of those then asked a specific question about what you can't get to work. Good Luck – codeMagic Jan 10 '13 at 17:31
  • Now it lookes more clear to me.. but i have a question.. in my application should be 100 pages.. so there are 100 layouts for example and in this way i will have to create 100 activities too right? each layout for one activity(fragment).. yes or no? Thank you – Ronald Reagan Jan 10 '13 at 22:58
  • I have updated my answer. Let me know if that doesn't make sense. You will probably have to start on a small part (a couple pages) before it will make much sense – codeMagic Jan 10 '13 at 23:16
  • I want to have an effect like here: https://play.google.com/store/apps/details?id=com.ximad.wff_lite_mx&feature=search_result where you can switch from one page to another – Ronald Reagan Jan 11 '13 at 00:41
  • I fully understand what you want. Notice how the layouts on the two screens are the same. The only difference is the data. – codeMagic Jan 11 '13 at 00:55
  • Ok the viewpager works! The next thing to do now is what you sad before.. add different pages to my view adapter – Ronald Reagan Jan 11 '13 at 14:48
  • Ok, I'm glad you got it going. If you have got it started then you should probably accept this answer and post a new question if you get stuck on a particular problem with it. SO doesn't like us extending the chat too much. But I will be glad to help you with what I can if I see any more questions – codeMagic Jan 11 '13 at 15:16
  • If i will have some problems probably i will open a new question.. Thank you so much codeMagic! – Ronald Reagan Jan 11 '13 at 15:36
  • No problem, glad I could help! I don't mind at all its just that it gets a little difficult through an extended chat in discussion. Good luck to you! BTW, Love the name! :) – codeMagic Jan 11 '13 at 15:39
  • hi codemagic.. i got a question.. can you help http://stackoverflow.com/questions/14282307/viewpage-solution – Ronald Reagan Jan 11 '13 at 19:28