BaseActivity to implement Navigation Drawer and Toolbar in every activity - android

I created a base activity and base layout to extend those to get same nav drawer and toolbar in all my activities. However, I want to add sliding tab layout in one of my activity. Should I create a new layout for this? Or is there a way I can add this sliding tab layout to my extended base activity dynamically?
activity_base.xml
<include android:id="#+id/app_bar" layout="#layout/app_bar"></include>
<android.support.v4.widget.DrawerLayout
android:layout_below="#+id/app_bar"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#color/itembackground"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>

You can implement the sliding tab in the Fragment which you are loading in the FrameLayout. The sliding tab can be achieved using a ViewPager and the ActionBar.TabListener interface. The ViewPager can hold the tab Fragments in a FragmentStateAdapter and access them using getChildFragmentManager(). This way, there is no need to create a separate layout for the tabbed screen, just extend from BaseActivity and use a Fragment with tabs.
For more on this, see the Creating Swipe Views with Tabs example.

Related

How can i use navigation side menu in entire app, even activity class?

I'm using android navigation drawer menu. I want to show navigation drawer in my all activities class. If i want to use it what i need to do actually. If anyone give tips it'll very helpful.
Make a BaseActivity Activity, which will be extended by every other Activity.
The layout of the BaseActivity will be a DrawerLayout, that contains a FrameLayout and the Navigation Drawer. It will look something like this (In this case, the Navigation Drawer is a RecyclerView) -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/base_frame"/>
<!-- Side navigation drawer UI -->
<android.support.v7.widget.RecyclerView
android:id="#+id/nav_drawer_rv"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
Make the FrameLayout a protected class field, and in every Activity that extends the BaseActivity, inflate the layout as such -
getLayoutInflater().inflate(R.layout.your_activity, baseFrameLayout);
Make the BaseActivity handle all interactions with the Navigation Drawer (selecting items, switching Activities, etc...).

Correctly implementing Toolbar and NavigationDrawer

I have an application with one activity that contains the navigation drawer. This activity also houses all the fragments that are associated with the navigation drawer.
What I'd like to do is have each fragment implement its own toolbar since some of these fragments might have a different theme or background for each toolbar in each fragment/screen.
The problem is once I have implemented a toolbar for each fragment, the hamburger icon disappears and is replaced with the back arrow. The navigation drawer still works, but the back arrow is misleading since the arrow should be a burger icon for each of those fragment connected with the navigation drawer.
What would be the correct way to implement a toolbar with the navigation drawer and have the freedom to change any property of the toolbar and keep the hamburger icon as well.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="#layout/standard_toolbar" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/my_toolbar" >
</FrameLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical" >
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/white"
android:choiceMode="singleChoice"
android:divider="#color/navigation_drawer_line_color"
android:dividerHeight="0.4dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
This is my code in the activity that houses the navigation drawer with child fragments as well as the toolbar.
getActivity().findViewById(R.id.toolbar) will give you reference to your Toolbar in your Fragments.
Hence, from this point you can control the background, inflate menus, change title, usually as you would with old ActionBar.
Regarding the theming of the toolbar programmatically, it is not possible.

gmail like Progress bar functionality

I am creating a gmail like progress bar functionality. There seems to be some space between the actionbar and the progress bar.
Following is the code
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminateOnly="true"
android:layout_width="wrap_content"
android:layout_height="?android:attr/actionBarSize" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
DrawerLayout should contain only two children: FrameLayout for your content and fragment for drawer. Thus ProgressBar should be located inside dynamically created content.
To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer. for more Creating a Navigation Drawer.

how to use multiple fragments in the main content view of navigation drawer

I want to show two fragments (one listfragment and other is details fragment) in the main contentview of navigation drawer like below:
But AFAIK, there can be only one view in the main contentview of drawerLayout. So how can achieve this?
Anything you put inside the FrameLayout will be in the main content view.
You can put whatever you want (including multiple Fragments) inside this FrameLayout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- YOUR CONTENT HERE -->
<!-- Could be layout with multiple views or fragments -->
</FrameLayout
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

How to add two fragments together in a frameLayout in runtime

I am developing an andorid application with navigation drawer and I have a layout for contentview like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:divider="#android:color/transparent"
android:background="#color/default_color"/>
</android.support.v4.widget.DrawerLayout>
So I can add and replace fragment on navigation drawer item click. Everything okay till now.
But in one case, on clicking an item on navigation drawer I need to show two fragments instead of one ( just like a listFragment and details fragment ). How can I do it? Should I add two fragments into the framelayout then? But how? or should I change the contentView with different drawerLayout ? If yes, how can I do this?
You may just use nested fragments. Create another fragment that will dynamically create both your fragments and add them to its layout.
For more information read this: http://developer.android.com/about/versions/android-4.2.html#NestedFragments

Categories

Resources