Full Source Code Download Here:
https://github.com/sanjaymangaroliya/NavigationDrawerInAndroidExample.git
BaseNavigationActivity
package com.zt.navigationdrawer;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class BaseNavigationActivity extends AppCompatActivity {
protected static final int NAV_DRAWER_ITEM_INVALID = -1;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private Toolbar toolbar;
private View view;
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN |
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
getSetActionBarToolbar();
setupNavDrawer();
}
private void setupNavDrawer() {
drawerLayout = findViewById(R.id.drawer_layout);
if (drawerLayout == null) {
return;
}
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
changeInfo(view);
}
};
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerSelectListener(navigationView);
setSelectedItem(navigationView);
view = navigationView.getHeaderView(0);
changeInfo(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (BaseNavigationActivity.this instanceof MainActivity) {
closeDrawer();
return;
}
goToNavDrawerItem(R.id.nav_android);
}
});
}
}
private void setSelectedItem(NavigationView navigationView) {
int selectedItem = getSelfNavDrawerItem();
navigationView.setCheckedItem(selectedItem);
}
private void setupDrawerSelectListener(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawerLayout.closeDrawers();
onNavigationItemClicked(menuItem.getItemId());
return true;
}
});
}
private void onNavigationItemClicked(final int itemId) {
if (itemId == getSelfNavDrawerItem()) {
closeDrawer();
return;
}
goToNavDrawerItem(itemId);
}
private void goToNavDrawerItem(int item) {
switch (item) {
case R.id.nav_android:
Intent intent_android = new Intent(this, MainActivity.class);
intent_android.putExtra("title", getString(R.string.android));
startActivity(intent_android);
this.finish();
break;
case R.id.nav_ios:
Intent intent_ios = new Intent(this, MainActivity.class);
intent_ios.putExtra("title", getString(R.string.ios));
startActivity(intent_ios);
this.finish();
break;
case R.id.nav_java:
Intent intent_java = new Intent(this, MainActivity.class);
intent_java.putExtra("title", getString(R.string.java));
startActivity(intent_java);
this.finish();
break;
case R.id.nav_php:
Intent intent_php = new Intent(this, MainActivity.class);
intent_php.putExtra("title", getString(R.string.php));
startActivity(intent_php);
this.finish();
break;
case R.id.nav_asp_net:
Intent intent_asp_net = new Intent(this, MainActivity.class);
intent_asp_net.putExtra("title", getString(R.string.asp_net));
startActivity(intent_asp_net);
this.finish();
break;
}
}
protected ActionBar getSetActionBarToolbar() {
if (toolbar == null) {
toolbar = findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
return getSupportActionBar();
}
protected int getSelfNavDrawerItem() {
return NAV_DRAWER_ITEM_INVALID;
}
protected void closeDrawer() {
if (drawerLayout == null)
return;
drawerLayout.closeDrawer(GravityCompat.START);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
drawerLayout.openDrawer(Gravity.START);
return true;
}
return super.onOptionsItemSelected(item);
}
public void changeInfo(View view) {
ImageView img_profile = view.findViewById(R.id.img_profile);
TextView tv_name = view.findViewById(R.id.tv_name);
TextView tv_email = view.findViewById(R.id.tv_email);
//tv_name.setText("");
//tv_email.setText("");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exitByBackKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void exitByBackKey() {
new AlertDialog.Builder(this)
.setTitle("Exit?")
.setMessage("Sure, You want exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
finishAffinity();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
})
.show();
}
}
MainActivity
package com.zt.navigationdrawer;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
public class MainActivity extends BaseNavigationActivity {
private TextView tvTitle, tvLanguages;
private String strTitle = "ANDROID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("");
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
strTitle = bundle.getString("title");
}
initUI();
}
private void initUI() {
tvTitle = findViewById(R.id.tv_title);
tvLanguages = findViewById(R.id.tv_languages);
if (strTitle.equals(getResources().getString(R.string.android))) {
tvTitle.setText(getString(R.string.android));
tvLanguages.setText(getString(R.string.android));
} else if (strTitle.equals(getResources().getString(R.string.ios))) {
tvTitle.setText(getString(R.string.ios));
tvLanguages.setText(getString(R.string.ios));
} else if (strTitle.equals(getResources().getString(R.string.java))) {
tvTitle.setText(getString(R.string.java));
tvLanguages.setText(getString(R.string.java));
} else if (strTitle.equals(getResources().getString(R.string.php))) {
tvTitle.setText(getString(R.string.php));
tvLanguages.setText(getString(R.string.php));
} else {
tvTitle.setText(getString(R.string.asp_net));
tvLanguages.setText(getString(R.string.asp_net));
}
}
@Override
protected int getSelfNavDrawerItem() {
if (strTitle.equals(getResources().getString(R.string.android))) {
return R.id.nav_android;
} else if (strTitle.equals(getResources().getString(R.string.ios))) {
return R.id.nav_ios;
} else if (strTitle.equals(getResources().getString(R.string.java))) {
return R.id.nav_java;
} else if (strTitle.equals(getResources().getString(R.string.php))) {
return R.id.nav_php;
} else {
return R.id.nav_asp_net;
}
}
}
res/menu/activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_android"
android:icon="@drawable/ic_android_black_24dp"
android:title="@string/android" />
<item
android:id="@+id/nav_ios"
android:icon="@drawable/ic_android_black_24dp"
android:title="@string/ios" />
<item
android:id="@+id/nav_java"
android:icon="@drawable/ic_android_black_24dp"
android:title="@string/java" />
<item
android:id="@+id/nav_php"
android:icon="@drawable/ic_web_black_24dp"
android:title="@string/php" />
<item
android:id="@+id/nav_asp_net"
android:icon="@drawable/ic_web_black_24dp"
android:title="@string/asp_net" />
</group>
</menu>
drawer_item_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_checked="true" />
<item android:color="@color/colorPrimary" />
</selector>
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemBackground="@color/white"
app:itemIconTint="@drawable/drawer_item_color"
app:itemTextAppearance="@style/tv_navigation"
app:itemTextColor="@drawable/drawer_item_color"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true">
<include layout="@layout/toolbar" />
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:id="@+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_languages"
style="@style/tv_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_200"
android:text="@string/app_name"
android:textSize="25dp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_user_info"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="@color/colorPrimary"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/img_profile"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="@dimen/margin_20"
android:src="@drawable/default_user"
app:civ_border_color="@color/white"
app:civ_border_width="3dp" />
<TextView
android:id="@+id/tv_name"
style="@style/tv_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_15"
android:layout_marginTop="@dimen/margin_60"
android:layout_toRightOf="@+id/img_profile"
android:text="@string/app_name"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_email"
style="@style/tv_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_name"
android:layout_below="@+id/tv_name"
android:text="@string/email"
android:textSize="14dp" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/gray" />
</RelativeLayout>
toolbar.xml
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/tv_title"
style="@style/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zt.navigationdrawer">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment