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.
Related
The structure of the app is one Activity and multiple Fragments(+ 20).
The Activity has a Toolbar with a title and back button that i used in multiple Fragments.
Then there is two more types of Toolbars that i use inside multiple Fragments, one contains an EditText and the other one is an Expandable Toolbar.
An example would be:
Fragment A: Uses Toolbar from Activity. Show that one, hide if another is showing.
Fragment B: Has a Toolbar of it own, now i need to hide the Activity toolbar.
What would be the correct approach to show/ hide the correct toolbar?
Things i tried:
Using onDestinationChangedListener checking the id of the fragment and show/ hide the correct toolbar. The problem here is that it cause the screen to flicker and i have to check for every id.
toolbar.isVisible = destination.id in listOf(id1, id2...)
Hide or show the correct Toolbar from the Fragment. But then i need to set the toolbar on every Fragment.
(requireActivity() as MainActivity).showToolbar(true)
Removing the Activity Toolbar and creating a new one inside every Fragment. But i don't know if the best way and if it is expensive to constantly inflate the Toolbar.
After calling NavigationUI.setupActionBarWithNavController, the title of the toolbar automatically changes to the android:label of the fragment defined in the navigation graph. But what if I want to show different buttons on the right side of the toolbar depending on the fragment? I used to create separate activities so I did not have to change the toolbar buttons dynamically, but now we use multiple logical screens (fragments) within one Activity, the buttons need to be changed just like the title.
For example, suppose my navigation has two fragments, f1 and f2. I want to show an "About" button for f1, and "Save" for f2. Is there an automatic way as the title, or do I have to manually do that in the Activity by using such methods as in (How can I dynamically create menu items?)?
I am trying to make an App on android. I have made the slide in menu bar like the one shown in the the picture below. The blue bar. Now what I want is that my every screen should show the same menu options. Not those with the back button. How do I do that? Should I make one header and call that in every class? Right now I have an Activity and everything else is a fragment.
I can post my code here as well.
make a Parent Activity and make this acion bar in it.inherit your all activities from this parent activity and just remove setContentView(R.layout.layoutname) from your child activity.
this works in your scenario when you are using fragments so you don't need the layout for activity. so your fragment container would be your parent activity..
ideally i'd like my main activity to have 2 views rather than starting a new activity; currently i'm using a view switcher to switch from map view and list view.
on my map view i want a transparent action bar.
on the list view i want a solid one (so the list items aren't inaccessible under the action bar).
is this something that is possible?
or am i forced to create 2 seperate activities and pass a list of custom objects through a bundle to make the transition between views as simple as possible.
the final goal is to have a iPhone style flipper animation when switching the views so it's like you're viewing the current stats/details on the back of the map view.
apparently such a transition animation might not be possible between activities, but i'm still looking into it.....
You can use actionBar.show() and actionBar.hide() to toggle ActionBar feature.
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.