Can I choose different layout for the Fragment based on the Button that has been clicked on the Activity, or should I create one Fragment class for each Button?
I know that Fragments exist to accommodate different UI but in my case I have many buttons that on click display a FrameLayout and I was thinking if it is possible to save time from copy/paste ?
I'd say that depends on how your layouts/fragments look like. If they are huge and very similar it could be better to have one layout. You could also think about using <merge /> or <include/> in that case.
This way you can maintain equal parts in (sub-)layouts without the need of code repetition.
Have a look at Googles site here.
Related
I have looked everywhere but don't seem to be able to find what I am looking for. I have an activity with multiple buttons, each button opens a new activity with an identical recyclerview layout, but different data. I am wondering if it is possible to use one activity and layout instead of multiple? this way instead of having 10+ activities (one for each button) I only have to manage one when a button is clicked and simply pass the necessary list data to it.
I believe you could set the intents for each button to call the same activity but with an integer such as 1-10, in the activity it takes the value and decides which list should be presented? If anyone thinks of how this could be done or a simpler way I would greatly appreciate it!
Yes, there are various ways to do that.
You could use multiple fragments on the same activity. Then add/remove fragments on each button click.
You can have multiple layouts within your activity. Say you have two buttons, and you have three layouts layout1, layout2, layout3, sequentially one after the another. So if initially, layout1 is visible and the rest are gone using layout.setVisiblity(View.GONE), if you click button1, ypu can do layout1.setVisiblity(View.GONE); layout3.setVisiblity(View.GONE); layout2.setVisiblity(View.VISIBLE) and vice-versa for pressing button2.
Are all the activities opened by the buttons similar? If so, you could only take care of the changes in the elements of the layout & specify conditions.
For instance, if you click a button, instead of changing the whole thing, you only go into the buttons & change their texts with btn.setText("..."). You could define different conditional statements inside the onClickListener of that button.
It could be something like:
if(btn.getText().equals("a certain text that you set to the button")){
doSomething();
else if(btn.getText().equals("another option")){
doSomethingElse();
Following this logic, you could continually update the elements in your layout & your code will decide what to do depending on what's stored in these elements.
The second option that comes to my mind would be creating different xml layout files & simply changing the layout of your MainActivity to the appropriate one depending on what stage of your process you are at.
I hope this helps,
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.
Let's say i have the Android XML file home_page.xml.
on this home_page.xml i have some variations that i want to show at different activities, and i'd like to reuse the same main layout home_page.xml.
For example, imagine variations on the page such as:
there's 2 more buttons if the user is in state A
there's 1 more editText field if the user is in state B (same activity as state A)
there's a different arrangement of layout on the Z-axis in a frame layout if the user is in state C (same activity as state A)
i know it's possible to programmatically say hide views and set views as visible. but is there a better way to do this via xml or something?
Android recommends using 2 Tags for re-using the layouts across different screens.
Include
When to Use ?
If you already know the layout that you want to re-use, create a new XML file and define the layout. Use tag to re-use it.
Merge
When to Use ?
To eliminate redundant view groups in your view hierarchy when including one layout within another, we can use tag.
Refer to this link - http://developer.android.com/training/improving-layouts/reusing-layouts.html for code sample and more details.
You can hide views but using the Visibility flag.
View v = findViewById(R.Id.my_view);
v.setVisiblity(View.GONE); //etc.
I've tried stuff like this before. I had mixed results. This is fine if you are doing things, like asking the user for a name, then showing an address input or something. But if you find yourself with like 3 or 4 conditions for one editText and then different ones for a button in the same class you might want to just use different layouts. A lot easier to manage.
Android 2.3.3
I have an activity where I display "Device's Contacts" in my custom view(imageview, 3 textboxes, checkbox) inside a ListView. What I want to do is, display two listviews with both showing alternate contacts (Splitting the listview into two) side by side. This is because, I want to utilize space on landscape mode of larger displays.
I haven't seen many questions on SO and somewhere I have read that, this approach will mess up scrolling of listviews and will get messy.
So, can someone explain why/how would it get messy and is there another way to use the space in larger displays in my scenario.
Thanks.
This sounds like exactly what the Building a Dynamic UI with Fragments android tutorial was designed to address.
As #ooops mentions you will need to put each ListView within its own container. In the tutorial I linked to, they accomplished this by using 2 Fragment instances, each that loaded different content. Whether you copy their example and use the <fragment> tab (admitidly I don't remember which API level that works on) or use a different container like a regular LinearLayout you should be able to acomplish this easily.
You could do this, but each ListView should be in it's own container.
For examlple How to use multiple listviews in a single activity on android?
But be aware that #Rarw is more correct in the way of good programming practice. Fragments are recommended for such purpose.
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/