I'm designing a music player app and this is my second time working with android studio and fragments in general. I've watched a lot of tutorial videos on this but I'm having trouble getting multiple fragments to show. Maybe using fragments isn't the way of going about this. But basically what I have right now is a fixed menu at the top and 5 fragments just below it (on top of each other).
What I was hoping to do was by clicking each button one fragment would load so you would have one activity but 5 fragments that load when buttons are pressed. But the issue i'm having right now is instead of stopping one fragment and loading another one, they're being loaded on top of each other. Is there a way to go about doing this?
Below is an image of the design I had in mind. The blue box is where the five fragments are and right now they're overlapping. Any help would be appreciated :)
EDIT: Turns out the answer was I needed to define one layout for all of the fragments. Thanks everyone.
Please post code. But here's what I can make out so far from your problem,
I assume you are just using FragmentManager.beginTransaction.add() again and again.
If it is the case please change your code to
FragmentManager.beginTransaction().replace(
containerViewID,
new FragmentYouWantToReplace
);
What this does is removes previous fragment and adds this new fragment.
So you can have your default fragment using add() during activity load, but for each button click you use replace().
When you call a new fragment you should replace always the same layout, the one you want to show the information, like this:
frag_results frag_results = new frag_results();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.framelayout_secundary, frag_results, frag_results.getTag()).commit();
Related
I have 2 activities, In first activity I have a button and I want when user clicks or move up that button the second activity come from bottom and stop when it goes to the half of the screen. I don't understand how can I achieve this. I also searched google but they show some type of dialog boxes :(.
This is what I want. When app start 1st activity is shown on the screen but when user click ^ this button both 2 activities show 50% on the screen.
Can anybody tell me how can I achieve this. Is it possible???
You would achieve this using Fragments.
I would suggest you start with the offical documentation - https://developer.android.com/training/basics/fragments/index.html
In order to create the interface above you could use fragments instead of activity.
In your case you need to have an activity with two fragments (you can use coordinatorlayout).
If you don't have enough knowledge about fragments I recommend to read those articles:
Codepath fragments guide
Vogella fragments guide
Convert Activity to Fragment and use <FrameLayout> on page to display/remove fragments from page.
Fragments are reusable components Refer to convert Activity to Fragment
This might help you understand how to work with Fragments link
You should read about fragments. They can be used for the UI you described. You can make it so that a second fragment is GONE, and when the user presses a button, it becomes visible. You should probably use a relative layout for the two fragments.
I have to deal with two ActionBar displayed at the same time on the screen. There's an onClickListener on the second one which allows the second activity to be shown on the entire screen after a click, with an animation.
I have no idea how to do that. Two activies, fragments? ViewPager? ... Absolutely no idea. Could you please help me? Thank you.
EDIT : here is a picture of what I wanna achieve http://romainpellerin.eu/so_android.png
You have to see it in order to clearly understand my problem.
Definitely Fragments.
When you bring in the second fragment specify animations in the fragment transaction before you commit it.
There are tons of tutorials online. Here some more help.
Android Fragments and animation
SDK
http://developer.android.com/guide/components/fragments.html
how to make fragments backwards compatible
http://developer.android.com/training/basics/fragments/support-lib.html
I'm experiencing a strange problem. I have an app, based on fragments. When I want to change fragments, I first check if the fragment is added. After hiding the previous fragment, if the next fragment is already added before, I just show it. If it is not added before, then I add it. And one of my fragments has a VideoView declared in it's XML file.
Now my problem is, if the fragment is not added already, when I try to add it, the whole screen goes black for a second and then comes back with the new fragment. The portion that I am placing the fragment is only a part of the screen, so it should only refresh that portion of the screen. After the fragment is added, when I try to navigate to it (when I use show() method), I do not experience this behaviour. I believe this has something to do with the VideoView, because if I remove it from XML, I no longer experience this problem. I tried adding the VideoView programmatically, but I still had the problem. I also tried to use a SurfaceView to play the video, but in that case, again I have the same problem.
I would appreciate any suggestions. Thank you.
After scanning all the related questions regarding multiple activities under one tab, I found out that they all refer to using ActivityGroup which is deprecated.
Basically I have 2 tabs, the first one contains a main activity with several buttons and when button is clicked 1 for instance a new activity should open under the same tab, clicking back should bring me to the main view under the first tab. The second tab contains only one activity.
Tried inflating a new view for the sub-activities:
FrameLayout frameLayout = tabHost.getTabContentView();
frameLayout.removeAllViews();
View view = getLayoutInflater().inflate(layoutID, tabHost, false);
frameLayout.addView(view);
But I only get the view, I cannot handle it like an activity; meaning adding buttons or listeners, it's only a view.
Google suggests using fragments instead, but I am having the hard time implementing this. Maybe replacing the activities with fragments could be the solution.
I could really use a good tab\fragments example right about now...
Any suggestions? Thanks in advance.
Yes, the solution to your problem is using Android Fragments.
I understand that you have trouble figuring out what to do. It's everybody's difficulty actually. However, with enough patience to google out things, you will realize it's actually not that difficult.
What I will be telling is basically some tips on how to change your code to fragments. From your current code, there's actually a major change you need to do but it's worth it.
What you are doing is actually you're making a dynamic UI. You have chunks of fragments which eventually can have different listeners which you should later on defined. Here's a sample demo you can actually wok on.
If you want a detailed tutorial, I followed this one. And, yes, it works!
Have fun learning Android and don't forget Android is directly linked to Google. And you can easily google things and ask specific questions here should that question was never asked before.
I'm currently creating an app in which the main screen is build up out of 2 Fragments.
When the user selects options on the main screen, one part of the screen gets replaced by a new Fragment, all pretty much basic stuff.
Now I'm trying to create a screen with several tabs, which all open a new fragment inside them. I had this working with regular intents, but that was before switching to Fragments.
I read that this is possible by using a FragmentActivity, but sadly you can't replace a Fragment with a FragmentActivity, simply because the transaction won't let you.
Is there any way of doing this inside an ordinary Fragment? Or should I try mimicking the behavior by using a layout with a fragment inside which gets replaced by another one at the press of a button, much like the main screen? (Or won't that work due to fragments in fragments?)
There is an example in Android's support library that describes what seems to be what you need. You can find it here: FragmentTabs.