Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android App template with Drawer + Toolbar + Fragments

I'm a veteran Android developer and I'm facing some trouble with the new lollipop infrastructures and I would like to ignite a pretty generic discussion that will lead to a nice powerful and working project template that I think many will benefit from. (I tought it will be trivial but every approach I take I find plenty of pitfalls)
Imagine I have an app with a navigation drawer (A fragment) that allows me to select one of 5 top level fragments.

3 of the them share a common toolbar behavior, for example something like the Google Calendar app, when the user clicks on the spinner in the the the toolbar slides the content down and shows a date selection above the content.

And yet the 3 fragment need to provide different button in the toolbar ( that should be set as the ActionBar )

The 2 other don't need that common behavior but I'm reminding you that they do need to provide access to the navigation drawer

I want to keep the question short and not pollute the reading minds with the different approaches I tired, I feel this is a pretty common app skeleton.

If you have a suggestion it will probably be best if you try it first.

Note: I insist on using a toolbar because it's allow higher dev freedom

like image 670
Shai Levy Avatar asked Dec 09 '25 05:12

Shai Levy


1 Answers

@Mldc found a nice 'example' project for how to do this. Also, it is not difficult to dynamically add action buttons to a Toolbar from fragments.

See the example project on github (https://github.com/danigonlinea/NavDra-material)

Screenshot

There is a good tutorial on how to use Toolbar here.

From the link: (You can use a different menu resource file based on which fragment is showing)

Finally, we call inflateMenu() to inflate a menu resource file. This will display the action item in the Toolbar.

We need to include the Toolbar class’s setOnMenuItemClickListener() method to listen for when the menu’s Action item is selected:

Toolbar Action item click listener

We attach the listener to our standalone Toolbar. It listens for when a menu item is selected, triggering its onClick() method. Here we include a Toast message to display when the Action item is selected.

like image 147
JonasCz Avatar answered Dec 11 '25 20:12

JonasCz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!