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.
Related
I have looked everywhere for examples on how I can have individual fragments set up as in a list view. It seems either this is highly not recommended or not possible. Does anyone have examples that I can look at? Essentially the app that I am trying to build is a an e-commerce app with a list of items to purchase. I know it may be overkill to have every item to be listed as a fragment but for this project, I need to do this.
This is highly not recommended. If your using a ListView then your xml layout file for each list item will be the same. Ideally if you use a ListView your elements will be mostly the same. You could have one Fragment for when an item is selected from your ListView, however. This one Fragment will be able to function properly for all elements of the ListView. That way you only have on Fragment for all of the elements in your ListView. Having a Fragment for each item in a ListView would be very inefficient.
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'm currently creating a Quiz section for an app. This is what it looks like for the most part:
My problem is that the number of answers for each question can vary from 2-4. I can see two solutions for this:
Create multiple layouts with the correct number of answers. When the user moves to the next question, I change the layout according to how many answers there are for that question.
Have one layout for the activity. Determine how many answers there are for a question and alter the layout programmatically.
Which of the two is the better route, or is there an even better alternative?
The best solution for your problem is to use a ListView or a RecyclerView because these components are dynamic and can display from 0 to n views perfectly.
ListView: https://developer.android.com/reference/android/widget/ListView.html
RecyclerView: https://developer.android.com/reference/android/support/v7/recyclerview/package-summary.html
After use it, you are able to control when your user click.
Use a recyclerview Creating android recyclerview
Definitely the 2nd approach is better,
Why? Because in 1st approach you will be inflating the entire layout for each question. With 2nd approach you can just change the content of TextView and the adapter of RecylerView that publishes your answer.
For a school project a need to build a android quiz application with multiple question types. These question types are: Multiple Choice, Textbox, Radio button and a minigame in which you need to sort images in the right order in a draggable gridview.
I think to use multiple activities for this isn't needed and that i just can switch the overall layout between multiple xml layout files for each of these question types if i first check what the type of the question is. Most other posts on stackoverflow are only related to quizzes with one type of question, without minigames in between the questions so i wanted to know what the best method is to switch between these different question types and layouts. At least i know i need a switch/case to check the different question types (for my prototype located in a array) and switch to right type if you press the "Next question" Button that is located under the answers (under the gridview in case of the minigame).
I already searched on the internet for it and i found the following options but i don't know what's best in my case. I also wanted to make the app compatible with android version 2.3.3. Some examples could also help me very much!!
(Adapter)Viewflipper
An Adapter that changes a inner layout
setContentView() of the main activity
Including a layout in another layout and change it
Make some views/layouts visible and invisible.
Make a new activity for each question (if there is no other option)
You can do it with a ViewFlipper. In the ViewFlipper XML, just list each layout for each question type and just have the user scroll through the flipper to get to the type they want. You can probably do this by setting a OnTouchListener and calling showNext() as they scroll.
Setting the correct data for each view is solely up to you; you would just have to match the correct data based on what view they are in to populate it correctly. That's it :)
It depends on how you want to structure your quizes are the questions supposed to be mixed together? Are they all seperate quizes.
If they are separate the easiest approach would probably to implement a view pager. With the view pager it will create a new fragment for each page. From here you have a two options, you can either create a new fragment for each quiz type. Or you can create one quiz fragment and pass an argument to that fragment to select the correct layout. Either way you'll likely want a new layout for each quiz type. As for your minigame you can just include that as a page in your view pager in between two quiz views.
All of the above will work on 2.3 and above if you use the support library. I would recommend checking out some tutorials on viewPager they are fairly straightforward to use.
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.