hide fragments in frame layout android - android

I'm using a frame layout to have more than one fragment in the same section of the screen.
So, I have a frame layout tag and then few fragment tags under it in XML.
So, I can see the overlap of these fragments.
I want to hide all the fragments except the one I want to show.
Where should I call the hide function?

If you are using support library for fragments use getSupportFragmentManager().beginTransaction().replace(<resource id for frame layout>, <new fragment>).commit()
This would replace all the fragments that are attached to this framelayout with the new fragment.

Related

Android TabLayout & Fragments

I have an Activity which contains a TabLayout and a ViewPager. The first tab contains a calendar and the second tab contains a timer.
Tapping on a date in the first tab should display a fragment with some event details while simultaneously hiding or removing that menu tab.
To this end, how can I use a FrameLayout as a fragment container when my layout already contains a ViewPager? Should I create a new Activity to get what I want, or is there a more efficient solution?
What you are describing is basically a list/details pattern. In short, yes, you should create a new activity for the details. It is the expected flow of an android app and it makes the implementation for you so much easier.

Is it possible to implement a TabLayout with activities

I have a Tablayout kind of a bottom navigation, and I want that when the user clicks in a cardview that I have it sends him to another Tablayout but when that happens the bottom navigation does not appear anymore. That is only possible with activities right?
A TabLayout is just a view like any other. It can be inflated as a part of another view, or as the content view for a Fragment or an Activity. So, you're not restricted to using just an Activity with a TabLayout.
Now, if you want it so that when you click on a CardView you're taking to another screen without a TabLayout, you have a couple of options:
Open a new activity
Since this second Activity is going to have a different content view, it's not going to have the TabLayout from the first activity.
Hide the TabLayout and change the content surrounding the CardView
Since we're sticking with the first Activity, we're going to need to make all of our content changes to the views in the Activity. This means setting the visibility on the TabLayout to View.GONE and potentially lots of changes to the rest of the views depending upon what your layout contains.
I noticed that you didn't mention a ViewPager at all. Typically, this is what will be used with a TabLayout to swap between Fragments when you click on each tab. You could have all of your tabs' content in separate Fragments and then when you click on a CardView, just swap that tab's Fragment out for a different one and hide the TabLayout.
So, to answer your question, it is easier to just open a new activity, but it is possible to not use a second activity if you want to put in the work to modify your first one in runtime.

How can I animate a Fragment inside a viewpager?

I have got a viewpager that hosts two Fragments. Now I would like to replace one of the Fragments on a button click. How can I animate this replacement? (e.g. using the flip-card effect)
Since it is difficult two put all my code in here, this is what I basically use
http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
You can animate it using a PageTransformer
Or, if you want the animation on replacing a fragment, you can set a custom animation on the FragmentTransaction

Mixing layout values each other when use same layout for different fragments in ViewPager

In my Android application there are two fragments called FragmentA and FragmentB. I added both fragments to a ViewPager using a FragmentAdapter So when I run the application I'm getting a unexpected layout (both fragment layouts are mixed together). Whenever I tried changing the currentItem of ViewPager by swiping for each time the layout is mixing each other.
My question: Is there any problem if we use same layout to different fragments in a ViewPager?
I found the answer.
There is no problem if you use getView() function of Fragment to refer the current root view of a fragment. So that you can get exact views in the Fragment. You can use like this(getView().findViewByid(...);
But there is a problem if you use activity reference to refer views in the fragment. Because all views in each fragment which is using same layout xml has same id. So if you change the value of a view in a fragment that will reflect in other fragments which are using the same layout in ViewPager. One more thing, think like this, when the activity created, all the fragments in the ViewPager of that activity will also be created and runs in background. So if you change the value of a view, android will return 1st view which is having the same id. Android knows only id of views. So always refer views of fragment using it root view (getView()).

Docking a fragment window in android

Is it possible to have two fragments - one on the left which controls the one on the right, and dock and undock the left fragment such that on docking the one on the left, only the fragment on the right occupies the screen ? If so how?
You can create a horizontal linear layout as your main layout for your activity and within that layout add two linear layouts that will be the place holders for your two fragments ie leftLinLayout and rightLinLayout. When the activity loads add the two fragments dynamically to the two layouts using a FragmentTransaction.
Within the fragments it is possible to get a reference to other fragments since you have method getActivity() then you call the fragment manager and find the fragment you want to manipulate or remove. However this is not desirable. The better solution would be to build a callback interface that the host activity has to implement so that it becomes a listener to your fragment events and then you let the activity add/remove the desired fragments. A good example of this implementation is the newsreader app in the android developer reference http://developer.android.com/training/multiscreen/index.html .

Categories

Resources