Skip to main content

Navigation drawer, material design theme

banner
Aim:
To make an android app with navigation drawer, material design theme
Feat:
Toolbar is used instead of the ActionBar, When the user runs the app for the time , he/she can see the drawer opened on the launch of the app, also rotation doesn't effect the drawer (whose layout is a fragment).
Used:
Fragments in both
Code:
 
MainActivity.java______________________________________________________________________________________________________
DrawerList.Java______________________________________________________________________________________________
package cts.fastester;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
* A simple {@link Fragment} subclass.
*/
public class DrawerList extends Fragment {

ActionBarDrawerToggle toggle;
DrawerLayout drawer;
boolean userlearned,fromsavedstate;
View containerview;
public DrawerList() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_drawer_list, container, false);
}


public void setup(int id,DrawerLayout drawerLayout, final Toolbar toolbar) {
containerview=getActivity().findViewById(id);
drawer=drawerLayout;
toggle= new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,R.string.app_name,R.string.app_name)
{
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(!userlearned)
{
userlearned=true;
savepref(getActivity(),"data",userlearned+"");
}
getActivity().invalidateOptionsMenu();
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};

if (!userlearned && !fromsavedstate)
{
drawer.openDrawer(containerview);
}
drawer.setDrawerListener(toggle);
drawer.post(new Runnable() {
@Override
public void run() {
toggle.syncState();
}
});

}

void savepref(Context context,String key, String val)
{
SharedPreferences preferences=context.getSharedPreferences("data",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putString(key,val);
editor.commit();
}

String getpref(Context context,String key, String defval)
{
SharedPreferences preferences=context.getSharedPreferences("data",Context.MODE_PRIVATE);
return preferences.getString(key,defval);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
userlearned=Boolean.valueOf(getpref(getActivity(),"data","false"));
if(savedInstanceState!=null)
{
fromsavedstate=true;
}
}
}
 
 
activity_main.xml_______________________________________________________________________________________________________
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >

<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"

android:orientation="vertical"
>
<include layout="@layout/appbar"
android:id="@+id/appbar"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Txt"/>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"

style="@android:style/Widget.ProgressBar.Large.Inverse"
android:indeterminate="true"/>
</LinearLayout>

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="cts.fastester.DrawerList"
android:layout_gravity="start"
android:id="@+id/fragmentlist" />

</android.support.v4.widget.DrawerLayout>
 
 
appbar.xml________________________________________________________________________________________________________________________
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primarydark"
app:theme="@style/Toolbarstyle"
app:popupTheme="@style/ThemeOverlay.AppCompat"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


</android.support.v7.widget.Toolbar>
 
 

fragment_drawer_list.xml________________________________________________________________________________________________________
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#fff"
xmlns:android="http://schemas.android.com/apk/res/android" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="list"/>


</LinearLayout>
 
 

styles.xml___________________________________________________________________________________________________________________________
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->


</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primarydark</item>
<item name="colorAccent">@color/accent</item>
</style>

<style name="Toolbarstyle" parent="ThemeOverlay.AppCompat.Dark"
>
<item name="android:textColorPrimary">@color/white</item>
<item name="android:textColorSecondary">@color/white</item>
</style>
</resources>































































































































































































































































Comments