I'm implementing a fragment which is based in a ViewPager for sliding slides (see here).
So, I would like to know if it's possible to get a new sliding tab dynamically, based on a text length (which is displayed in a TextView inside of ViewPager) I want to display inside that ViewPager (to get a similar book view).
Thank you.
Go back to that link you posted about ViewPager and re-read it focusing on PagerAdapter. The concept is similar to the list adapter. So for example, you create a PagerAdapter that will take your content (in a constructor, for example), and determine how the content is broken up into pages. Say you pass in some content and your PagerAdapter determines there are 17 pages. Your override for PagerAdapter.getCount() would return 17. Then in instantiateItem() (or maybe getItem(), if you're using FragmentPagerAdapter), you would create the view that is to be displayed at page position, which is a parameter.
Fair Warning! It sounds like you want to page some text horizontally like pages in a book. You should know that TextView won't be able to do things like, "well I'm this size, so I can display this much text". You will need to use things like android.text.StaticLayout to figure out stuff like that. Do-it-yourself text layout is advanced Android programming, so brace yourself.
Related
I am relatively new to android.
I have to design an Activity that has a 2 * 4 grid.
Each grid item will have an image view and a text view
On swiping left or right the user will move to a different page but the UI will remain the same.
Just the data set to the grid will change.
I want the number of pages to be configurable.
Also I want to reuse the same xml file for UI instead of creating two new xml files for the other 2 pages.
Which is the best possible way and the most efficient way to implement this and any examples on how to do this ? Any help will be appreciated. Thank you
I think you can use ViewPager with Fragment. You can see in this example https://developer.android.com/training/animation/screen-slide.html
You could achieve this with a ViewPager, each page (probably Fragments) containing a RecyclerView with an Adapter..
The fragment code, layout file and adapter implementation would be identical for every page. You just need to pass in the respective data to display to each page.
I am new to android and I am working on a project that displays cards (like on Google Now but all cards are the same size). My question is, is it possible to add the same fragment (a general design for the card) multiple times to the same screen and have them be position after each other in a row or column? If so how would I go about that?
Yes you can. Create a template fragment (eg. TempFragment extends Fragment). Then initilise a new TempFragment each time you need with the data you want to display and use a FragmentManager to add it to the view you want. As for the row or column layout you can use TableLayout in your main layout. Your question is very vague and I'm afraid I can only help you this far.
You can use your Adapter's item to implement your fragment. Google's Api Demos has this kinds of example( Support4Demo).
You can use RecyclerView with its Adapter class. Google uses same subject for displaying a similar layout items.
https://www.androidhive.info/2016/01/android-working-with-recycler-view/
you can see here ... there is all you need.
I want to have a bunch of images that show how my app works and have a dot-type pager at the bottom, like the example below. What UI Element should I be using?
Is Android ViewPager suitable for this? how will be adding the small circles below which act like page numbers.
EDIT: After I posted this question, I found this library. Would like feedback on how to do this from scratch.
Actually, if you look thought the code at https://github.com/JakeWharton/ViewPagerIndicator you can get a nice feedback on how to do it from scratch. The lib uses a custom View , but you can emulate this kind of behavior with some existent views, for instance. Basically:
Create a LinearLayout below your ViewPager. Set the orientation to horizontal;
Put some RadioButtons in it (you can do it dynamically based on your adapter size);
In your Activity, Fragment or whatever set a PagerListener for your ViewPager;
This listener is provided by the ViewPager API and you can check and uncheck your RadioButtons according to the position in the ViewPager with the method onPageSelected(int position), for instance.
There you go, a (very) basic ViewPagerIndicator.
I want to avoid using intents in android tabs. so I am doing views, but I would like an example on how a large project does this.
Mainly because I am not just displaying static information configured on the view xml, but I am pulling a lot of information from a server when a user interacts with a button within a view. The code seems like it can get really long and messy with this view implementation, instead of when each view is in a separate activity.
I would like to see how others separated their methods in a nice neat and organized way.
and that example hello-tabwidget is nowhere near what I am looking for, thanks
use a viewflipper. its like the text switcher. u put a layout inside of a viewflipper and trigger it wen a tab is selected.
http://www.warriorpoint.com/blog/2009/05/26/android-switching-screens-in-an-activity-with-animations-using-viewflipper/
I need to implement an application which uses a sliding view (ViewFlipper) for one of its activity, this activity displays information stored on and array and passed from the previous activity via intent. The number of slides or tabs is expected to vary depending on the number of elements in the array. i.e for 4 elements in the array,we shall get 4 slides and when there is only 3, we have the same amount of slides displaying respective information accordingly.
Any idea on how to implement this, an example if possible? I am very new to android development. please help
Create an .xml file now implement a view flipper design the layout of how each pages work. I suggest using a relative layout and add those views inside the view flipper. Seems pretty basic now go search tuts on .xml, viewflippers, types of layout, textviews, and don't put questions here next time asking for answers. We are a community after all helping people with problems who actually tries to do something not for copy and paste people.
For this purpose it's best not to use the ViewFlipper, but instead use a ViewPager to which you can dynamically add 'subviews' (pages) using a PagerAdapter. ViewPager will provide performance benefits over ViewFlipper as it loads (and removes) pages on-demand (like a ListView).
Set-up of the ViewPager is in essence the same as setting up a ListView. You can use the ViewPager on Android <3.0 devices using the compatibility library.
Create your own class that extends PagerAdapter, and set mViewPager.setAdapter(mAdapter);. In your adapter, in the method instantiateItem() you will build your 'page' and add it to the parent with mViewPager.addView(newView);.
For a more detailed example of how to set-up a ViewPager see my answer here.