Use ActionBar Tabs to Switch Views - android

Is there a way to use ActionBar tabs, but with the tabs switching ContentViews instead of fragments? The issue is that I have dual-pane layouts on large screens that have a fragment in each of two panes and I'd want to switch out the whole container view when that tab is selected.
Currently I'm using my own tabbing mechanism, but I'd like to integrate better into the built-in features and behavior of the ActionBar, and to save space by not having to have a separate ActionBar and tab bar when possible.

From the Action Bar docs --
To get started, your layout must include a ViewGroup in which you
place each Fragment associated with a tab. Be sure the ViewGroup has a
resource ID so you can reference it from your tab-swapping code.
Alternatively, if the tab content will fill the activity layout
(excluding the action bar), then your activity doesn't need a layout
at all (you don't even need to call setContentView()). Instead, you
can place each fragment in the default root ViewGroup, which you can
refer to with the android.R.id.content ID (you can see this ID used in
the sample code below, during fragment transactions).
If you specify a layout for your activity with slots for two fragments, you can replace them both (or even just one if that makes sense your app) in the ActionBar.TabListener callback. The callback still leaves you in control of the layout.

Related

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.

In Action Bar set custom view within fragment

I have an Activity that holds some fragments.
This Activity is associated with a view pager, this view pager uses FragmentPagerAdapter, so every page of view pager is treated as a fragment.
Now, suppose I have customize the action bar view in any one of the fragment, and that custom view could be seen in other fragments also.
getActivity().getSupportActionBar().setCustomView(R.layout.custom_view_home);
getActivity().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
This is because, we are customising the view, using the activity context.
My Question :
Q. Cant we set the Custom View of action bar within a fragment ? So, it would not get reflected to other fragment.
Short answer: Yes.
You should only allow currently visible fragment to add custom view to the ActionBar. Of course, you can do it right from the Fragment, with whichever context.
Requesting invalidation of the options menu will remove current custom view with the new one, i.e invalidating entire ActionBar. Similar approach you can use from the link above.

Activity with variable content

I need to create an activity with an ActionBar, so that if you press the action bar buttons, the contents of the activity display the same information in different layout (one is a pie chart and the other a listview).
I don't want to use tabs or a viewpager, so what would be the best way to do this?
Build each view in a separate fragment, and use the actionbar buttons to set the current visible fragment.

Layout setup with fragments

I've just started converting my apps to use fragments. I have a ListView activity and a "Details" activity, which the user goes to after selecting an item in the Listview. I've converted them to fragments successfully, however, I have a header in the layout of each activity. The ListView has a "reload" icon in the header, but the header in the details does not.
Everything works fine when I'm viewing the app in portrait, but I have a layout in the "layout-land" folder which contains both fragments, so, when viewing in landscape, you see both the ListView and the "Details" view. The problem is the header layout is shown in both fragments.
My question is what is the best practice for covering my layouts to the new fragment setup? I'm thinking, instead of having the header in each fragment's layout, I just add it to the layout that contains both fragments?
Sorry if this is just obvious question, I'm still trying to wrap my head around the whole fragments paradigm.
That's why the use of ActionBar is recommended. If you include an action bar in your app with a refresh button , users will know (and learn) to use that button. The onClick implementation is all yours and you could refresh the list adapters accordingly. There is no need to then build these UI elements in all your controls.

Right way to switch between tabs and change layout of the application?

I'm currently developing an application that has a tab bar, and 3
different views: the first is a master-detail, the second one a
gallery, the third is a simple webview.
I was using the TabActivity, but since Android 3.0, this class is
deprecated and Android reference
suggests to use Fragments.
I switched then to an ActionBar, with Tabs and Action
Items. Inseide the first tab item I have a layout with 2 fragments (my
master-detail view). When I switch through tabs I want that my layout
change as I described above, so I thought to hide the left fragment
(the master listview) and work only in the detail fragment.. but with
this solution I have only one main activity with a lot of fragments
attached to it, and for each fragment displayed I need to modify the
Action Item shown and handle different actions in
OnOptionItemSelected.
Is this a good way to implement this kind of
application or should I consider different solutions?
You should have a single fragment container where the fragments are replaced depending on the tab selected.
One activity and multiple fragments is the right approach.

Categories

Resources