Skip to content

Commit 7a6f7ea

Browse files
committed
Introduce item text customizations
1 parent 2cd1d84 commit 7a6f7ea

File tree

5 files changed

+52
-58
lines changed

5 files changed

+52
-58
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ or in your `pom.xml` if you are using Maven
3737
</dependency>
3838
```
3939

40-
## Usage
40+
## Usage example
4141

4242
```xml
4343
<?xml version="1.0" encoding="utf-8"?>
@@ -59,6 +59,7 @@ or in your `pom.xml` if you are using Maven
5959
android:layout_width="wrap_content"
6060
android:layout_height="wrap_content"
6161
android:layout_margin="@dimen/fab_margin"
62+
android:theme="@style/AppTheme.AppBarOverlay"
6263
app:layout_anchor="@id/toolbar"
6364
app:layout_anchorGravity="bottom|end"
6465
app:drawMenuBelowFab="true"
@@ -75,7 +76,13 @@ or in your `pom.xml` if you are using Maven
7576

7677
- Layout resource to inflate as the header
7778
`<attr name="headerLayout" format="reference" />`
78-
79+
80+
- Item text customizations
81+
`<attr name="itemIconTint" format="color"/>`
82+
`<attr name="itemTextColor" format="color"/>`
83+
`<attr name="itemBackground" format="reference"/>`
84+
`<attr name="itemTextAppearance" format="reference"/>`
85+
7986
- If menu must be drawn below the FAB
8087
`<attr name="drawMenuBelowFab" format="boolean" />`
8188

@@ -93,6 +100,16 @@ The recommended way to customize the background color is by using the `app:backg
93100
mNavigationView.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#009688")));
94101
```
95102

103+
You can also set the button icon color according to the theme by setting the `android:tint` to a theme attribute:
104+
105+
```xml
106+
<com.andremion.floatingnavigationview.FloatingNavigationView
107+
android:id="@+id/floating_navigation_view"
108+
android:layout_width="wrap_content"
109+
android:layout_height="wrap_content"
110+
android:tint="?android:textColorPrimary" />
111+
```
112+
96113
See more at the [sample](https://github.com/andremion/Floating-Navigation-View/tree/master/sample)
97114

98115
## Libraries and tools used in the project

library/src/main/java/com/andremion/floatingnavigationview/FloatingNavigationView.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.animation.AnimatorListenerAdapter;
2121
import android.animation.AnimatorSet;
2222
import android.animation.ObjectAnimator;
23+
import android.annotation.SuppressLint;
2324
import android.content.Context;
2425
import android.content.res.ColorStateList;
2526
import android.content.res.TypedArray;
@@ -32,7 +33,6 @@
3233
import android.util.AttributeSet;
3334
import android.util.SparseArray;
3435
import android.view.Gravity;
35-
import android.view.LayoutInflater;
3636
import android.view.Menu;
3737
import android.view.MotionEvent;
3838
import android.view.View;
@@ -41,11 +41,6 @@
4141
import android.widget.FrameLayout;
4242
import android.widget.ImageView;
4343

44-
import com.google.android.material.circularreveal.CircularRevealCompat;
45-
import com.google.android.material.floatingactionbutton.FloatingActionButton;
46-
import com.google.android.material.internal.NavigationMenuView;
47-
import com.google.android.material.navigation.NavigationView;
48-
4944
import androidx.annotation.ColorInt;
5045
import androidx.annotation.IdRes;
5146
import androidx.annotation.LayoutRes;
@@ -61,6 +56,11 @@
6156
import androidx.customview.view.AbsSavedState;
6257
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat;
6358

59+
import com.google.android.material.circularreveal.CircularRevealCompat;
60+
import com.google.android.material.floatingactionbutton.FloatingActionButton;
61+
import com.google.android.material.internal.NavigationMenuView;
62+
import com.google.android.material.navigation.NavigationView;
63+
6464
@SuppressWarnings({"FieldCanBeLocal", "InflateParams", "RtlHardcoded", "unused", "WeakerAccess"})
6565
public class FloatingNavigationView extends FloatingActionButton {
6666

@@ -113,26 +113,38 @@ public FloatingNavigationView(Context context, AttributeSet attrs, int defStyleA
113113

114114
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
115115

116-
mNavigationView = (CircularRevealNavigationView) LayoutInflater.from(context).inflate(R.layout.navigation_view, null);
116+
mNavigationView = new CircularRevealNavigationView(context, attrs, defStyleAttr);
117+
setupNavigationView(context, attrs);
118+
mNavigationMenuView = mNavigationView.findViewById(R.id.design_navigation_view);
119+
120+
mFabView = new ImageView(context, attrs, defStyleAttr);
121+
setupFabView(context, attrs);
122+
123+
// Custom attributes
124+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatingNavigationView);
125+
mDrawMenuBelowFab = a.getBoolean(R.styleable.FloatingNavigationView_drawMenuBelowFab, false);
126+
a.recycle();
127+
}
128+
129+
@SuppressLint("ClickableViewAccessibility")
130+
private void setupNavigationView(Context context, AttributeSet attrs) {
117131
mNavigationView.setBackground(new ColorDrawable(getBackgroundColor()));
118132
mNavigationView.setOnTouchListener(mNavigationTouchListener);
119-
mNavigationMenuView = (NavigationMenuView) mNavigationView.findViewById(R.id.design_navigation_view);
133+
}
120134

121-
mFabView = (ImageView) mNavigationView.findViewById(R.id.fab_view);
135+
@SuppressLint("PrivateResource")
136+
private void setupFabView(Context context, AttributeSet attrs) {
137+
TypedArray a = context.obtainStyledAttributes(attrs, new int[]{R.attr.selectableItemBackgroundBorderless});
138+
mFabView.setScaleType(ScaleType.CENTER);
139+
mFabView.setBackground(a.getDrawable(0));
122140
mFabView.setOnClickListener(mFabClickListener);
123141
mFabView.setContentDescription(getContentDescription());
124142
mFabView.bringToFront();
125-
126-
// Custom attributes
127-
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MenuView, defStyleAttr, R.style.Widget_Design_NavigationView);
128-
if (a.hasValue(R.styleable.MenuView_menu)) {
129-
mNavigationView.inflateMenu(a.getResourceId(R.styleable.MenuView_menu, 0));
130-
}
131-
if (a.hasValue(R.styleable.MenuView_headerLayout)) {
132-
mNavigationView.inflateHeaderView(a.getResourceId(R.styleable.MenuView_headerLayout, 0));
133-
}
134-
mDrawMenuBelowFab = a.getBoolean(R.styleable.MenuView_drawMenuBelowFab, false);
135143
a.recycle();
144+
mNavigationView.addView(mFabView, new FrameLayout.LayoutParams(
145+
getResources().getDimensionPixelSize(R.dimen.design_fab_size_normal),
146+
getResources().getDimensionPixelSize(R.dimen.design_fab_size_normal)
147+
));
136148
}
137149

138150
private @ColorInt int getBackgroundColor() {

library/src/main/res/layout/navigation_view.xml

Lines changed: 0 additions & 32 deletions
This file was deleted.

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
-->
1616
<resources>
1717

18-
<declare-styleable name="MenuView">
19-
<!-- The menu resource to inflate and populate items from -->
20-
<attr name="menu" format="reference" />
21-
<!-- Layout resource to inflate as the header -->
22-
<attr name="headerLayout" format="reference" />
18+
<declare-styleable name="FloatingNavigationView">
2319
<!-- If menu must be drawn below the FAB -->
2420
<attr name="drawMenuBelowFab" format="boolean" />
2521
</declare-styleable>

sample/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
android:layout_width="wrap_content"
2929
android:layout_height="wrap_content"
3030
android:layout_margin="@dimen/fab_margin"
31+
android:theme="@style/AppTheme.AppBarOverlay"
3132
app:drawMenuBelowFab="false"
3233
app:headerLayout="@layout/navigation_view_header"
3334
app:layout_anchor="@id/toolbar"

0 commit comments

Comments
 (0)