Skip to content

Commit 0e4756b

Browse files
author
Sylwester Sokołowski
committed
Add new project.
1 parent 13b34c2 commit 0e4756b

31 files changed

+592
-1
lines changed

.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Android template
3+
# Built application files
4+
*.apk
5+
*.ap_
6+
7+
# Files for the ART/Dalvik VM
8+
*.dex
9+
10+
# Java class files
11+
*.class
12+
13+
# Generated files
14+
bin/
15+
gen/
16+
out/
17+
18+
# Gradle files
19+
.gradle/
20+
build/
21+
22+
# Local configuration file (sdk path, etc)
23+
local.properties
24+
25+
# Proguard folder generated by Eclipse
26+
proguard/
27+
28+
# Log Files
29+
*.log
30+
31+
# Android Studio Navigation editor temp files
32+
.navigation/
33+
34+
# Android Studio captures folder
35+
captures/
36+
37+
# Intellij
38+
*.iml
39+
.idea/workspace.xml
40+
41+
# Keystore files
42+
*.jks
43+
44+
libs/
45+
src/androidTest/
46+
src/main/res/drawable/
47+
src/test/

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
# android-custom-navigationview
1+
Android custom NavigationView
2+
===============================================================
3+
4+
Some times ago I was looking for how to customize the [NavigationView](https://developer.android.com/reference/android/support/design/widget/NavigationView.html). Here I would like to share with my simple solution.
5+
6+
The ActionLayout in [Menu Resource](https://developer.android.com/guide/topics/resources/menu-resource.html) looks OK, but I needed something more flexible.
7+
8+
Screen
9+
--------------------------
10+
![Screen](screen_android.png)
11+
12+
13+

app/build.gradle

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'android-apt'
3+
4+
android {
5+
compileSdkVersion 24
6+
buildToolsVersion "24.0.0"
7+
8+
defaultConfig {
9+
applicationId "pl.sly.android.custom.navigationview"
10+
minSdkVersion 16
11+
targetSdkVersion 24
12+
versionCode 1
13+
versionName "1.0"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
compile fileTree(dir: 'libs', include: ['*.jar'])
25+
testCompile 'junit:junit:4.12'
26+
compile 'com.android.support:appcompat-v7:24.2.0'
27+
compile 'com.android.support:design:24.2.0'
28+
compile 'com.jakewharton:butterknife:8.3.0'
29+
compile 'com.vstechlab.easyfonts:easyfonts:1.0.0'
30+
apt 'com.jakewharton:butterknife-compiler:8.3.0'
31+
}

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/sly/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}

app/src/main/AndroidManifest.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="pl.sly.android.custom.navigation.view">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".activity.MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN" />
14+
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package pl.sly.android.custom.navigation.view.activity;
2+
3+
import android.content.res.Configuration;
4+
import android.os.Bundle;
5+
import android.support.design.widget.NavigationView;
6+
import android.support.v4.view.GravityCompat;
7+
import android.support.v4.widget.DrawerLayout;
8+
import android.support.v7.app.ActionBarDrawerToggle;
9+
import android.support.v7.app.AppCompatActivity;
10+
import android.support.v7.widget.Toolbar;
11+
import android.view.MenuItem;
12+
import android.widget.Toast;
13+
14+
import butterknife.BindView;
15+
import butterknife.ButterKnife;
16+
import pl.sly.android.custom.navigation.view.R;
17+
import pl.sly.android.custom.navigation.view.widget.CustomNavigationView;
18+
19+
public class MainActivity extends AppCompatActivity {
20+
21+
@BindView(R.id.drawerLayout)
22+
DrawerLayout mDrawerLayout;
23+
24+
@BindView(R.id.toolbar)
25+
Toolbar mToolbar;
26+
27+
@BindView(R.id.customNavigationView)
28+
CustomNavigationView mCustomNavigationView;
29+
30+
private ActionBarDrawerToggle mDrawerToggle;
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
setContentView(R.layout.activity_main);
36+
ButterKnife.bind(this);
37+
38+
setupToolbar();
39+
setupNavigationView();
40+
setupDrawerLayout();
41+
}
42+
43+
@Override
44+
protected void onPostCreate(Bundle savedInstanceState) {
45+
super.onPostCreate(savedInstanceState);
46+
mDrawerToggle.syncState();
47+
}
48+
49+
@Override
50+
public void onConfigurationChanged(Configuration newConfig) {
51+
super.onConfigurationChanged(newConfig);
52+
mDrawerToggle.onConfigurationChanged(newConfig);
53+
}
54+
55+
@Override
56+
public boolean onOptionsItemSelected(MenuItem item) {
57+
if (mDrawerToggle.onOptionsItemSelected(item)) {
58+
return true;
59+
}
60+
return super.onOptionsItemSelected(item);
61+
}
62+
63+
@Override
64+
public void onBackPressed() {
65+
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
66+
mDrawerLayout.closeDrawer(GravityCompat.START);
67+
} else {
68+
super.onBackPressed();
69+
}
70+
}
71+
72+
private void setupToolbar() {
73+
setSupportActionBar(mToolbar);
74+
getSupportActionBar().setTitle(R.string.app_title);
75+
}
76+
77+
private void setupNavigationView() {
78+
mCustomNavigationView.setNavigationItemSelectedListener(
79+
new NavigationView.OnNavigationItemSelectedListener() {
80+
@Override
81+
public boolean onNavigationItemSelected(MenuItem menuItem) {
82+
selectDrawerItem(menuItem);
83+
return false;
84+
}
85+
});
86+
}
87+
88+
private void setupDrawerLayout() {
89+
mDrawerToggle = setupDrawerToggle();
90+
mDrawerLayout.addDrawerListener(mDrawerToggle);
91+
}
92+
93+
public void selectDrawerItem(MenuItem menuItem) {
94+
Toast.makeText(this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
95+
mDrawerLayout.closeDrawers();
96+
}
97+
98+
private ActionBarDrawerToggle setupDrawerToggle() {
99+
return new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
100+
}
101+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package pl.sly.android.custom.navigation.view.widget;
2+
3+
import android.content.Context;
4+
import android.support.design.internal.NavigationMenuView;
5+
import android.support.design.widget.NavigationView;
6+
import android.util.AttributeSet;
7+
import android.view.View;
8+
import android.widget.FrameLayout;
9+
import android.widget.TextView;
10+
11+
import com.vstechlab.easyfonts.EasyFonts;
12+
13+
import pl.sly.android.custom.navigation.view.R;
14+
15+
public class CustomNavigationView extends FrameLayout {
16+
17+
private NavigationView mNavigationView;
18+
private TextView mTopTextView;
19+
private TextView mBottomTextView;
20+
21+
public CustomNavigationView(Context context) {
22+
this(context, null);
23+
}
24+
25+
public CustomNavigationView(Context context, AttributeSet attrs) {
26+
this(context, attrs, 0);
27+
}
28+
29+
public CustomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
30+
super(context, attrs, defStyleAttr);
31+
inflate(getContext(), R.layout.custom_navigation_view, this);
32+
mNavigationView = (NavigationView) findViewById(R.id.navigationView);
33+
mTopTextView = (TextView) findViewById(R.id.topTextView);
34+
mBottomTextView = (TextView) findViewById(R.id.bottomTextView);
35+
36+
mTopTextView.setTypeface(EasyFonts.captureIt(context));
37+
mBottomTextView.setTypeface(EasyFonts.captureIt(context));
38+
39+
removeOverScroll();
40+
}
41+
42+
public void setNavigationItemSelectedListener(NavigationView.OnNavigationItemSelectedListener listener) {
43+
mNavigationView.setNavigationItemSelectedListener(listener);
44+
}
45+
46+
private void removeOverScroll() {
47+
NavigationMenuView navigationMenuView = findNavigationMenuView();
48+
49+
if (navigationMenuView != null) {
50+
navigationMenuView.setOverScrollMode(View.OVER_SCROLL_NEVER);
51+
}
52+
}
53+
54+
private NavigationMenuView findNavigationMenuView() {
55+
for (int i = 0; i < mNavigationView.getChildCount(); i++) {
56+
if (mNavigationView.getChildAt(i) instanceof NavigationMenuView) {
57+
return (NavigationMenuView) mNavigationView.getChildAt(i);
58+
}
59+
}
60+
61+
return null;
62+
}
63+
}
167 Bytes
Loading
129 Bytes
Loading
189 Bytes
Loading
253 Bytes
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="128dp"
3+
android:height="128dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FFFFFFFF"
8+
android:pathData="M13,3h-2v10h2L13,3zM17.83,5.17l-1.42,1.42C17.99,7.86 19,9.81 19,12c0,3.87 -3.13,7 -7,7s-7,-3.13 -7,-7c0,-2.19 1.01,-4.14 2.58,-5.42L6.17,5.17C4.23,6.82 3,9.26 3,12c0,4.97 4.03,9 9,9s9,-4.03 9,-9c0,-2.74 -1.23,-5.18 -3.17,-6.83z" />
9+
</vector>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="wrap_content"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical">
7+
8+
<ScrollView
9+
android:layout_width="wrap_content"
10+
android:layout_height="match_parent"
11+
android:layout_weight="1"
12+
android:fillViewport="true">
13+
14+
<LinearLayout
15+
android:layout_width="wrap_content"
16+
android:layout_height="match_parent"
17+
android:orientation="vertical">
18+
19+
<TextView
20+
android:id="@+id/topTextView"
21+
android:layout_width="match_parent"
22+
android:layout_height="wrap_content"
23+
android:background="@color/colorPrimary"
24+
android:gravity="center_horizontal"
25+
android:paddingTop="15dp"
26+
android:text="@string/news"
27+
android:textAllCaps="true"
28+
android:textColor="@android:color/white"
29+
android:textSize="55sp" />
30+
31+
<android.support.design.widget.NavigationView
32+
android:id="@+id/navigationView"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:background="@color/colorPrimary"
36+
android:paddingLeft="5dp"
37+
android:paddingRight="5dp"
38+
app:elevation="0dp"
39+
app:itemIconTint="@android:color/white"
40+
app:itemTextColor="@android:color/white"
41+
app:menu="@menu/navigation_view_menu"
42+
app:theme="@style/CustomNavigationViewStyle" />
43+
44+
<RelativeLayout
45+
android:layout_width="match_parent"
46+
android:layout_height="match_parent"
47+
android:layout_above="@+id/bottomTextView"
48+
android:background="@color/colorPrimaryDark">
49+
50+
<ImageView
51+
android:layout_width="wrap_content"
52+
android:layout_height="wrap_content"
53+
android:layout_centerHorizontal="true"
54+
android:layout_centerVertical="true"
55+
android:src="@drawable/ic_power" />
56+
</RelativeLayout>
57+
58+
<TextView
59+
android:id="@+id/bottomTextView"
60+
android:layout_width="match_parent"
61+
android:layout_height="wrap_content"
62+
android:layout_alignParentBottom="true"
63+
android:background="@color/colorAccent"
64+
android:gravity="center_horizontal"
65+
android:padding="10dp"
66+
android:text="@string/bottom_news"
67+
android:textAllCaps="true"
68+
android:textColor="@android:color/white"
69+
android:textSize="20sp" />
70+
71+
</LinearLayout>
72+
73+
74+
</ScrollView>
75+
</LinearLayout>

0 commit comments

Comments
 (0)