From 14f38d5e6e43a28583fcd685477ba9b0405a56b9 Mon Sep 17 00:00:00 2001 From: Christophe Beyls Date: Fri, 1 Jan 2016 20:51:50 +0100 Subject: [PATCH] Updated Navigation Drawer in MainActivity to comply with latest material design guidelines. --- app/src/main/AndroidManifest.xml | 2 +- .../fosdem/activities/MainActivity.java | 44 ++---- .../widgets/ScrimInsetsFrameLayout.java | 116 ++++++++++++++++ .../res/layout/include_navigation_drawer.xml | 67 ++++++++++ app/src/main/res/layout/main.xml | 126 +++++------------- app/src/main/res/values-v21/styles.xml | 5 + app/src/main/res/values/attrs.xml | 4 + app/src/main/res/values/colors.xml | 3 +- app/src/main/res/values/dimens.xml | 3 + app/src/main/res/values/styles.xml | 8 +- 10 files changed, 250 insertions(+), 128 deletions(-) create mode 100644 app/src/main/java/be/digitalia/fosdem/widgets/ScrimInsetsFrameLayout.java create mode 100644 app/src/main/res/layout/include_navigation_drawer.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dfa4fe4..809241a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,7 +27,7 @@ + android:theme="@style/AppTheme.NoActionBar.WindowDrawsSystemBarBackgrounds"> diff --git a/app/src/main/java/be/digitalia/fosdem/activities/MainActivity.java b/app/src/main/java/be/digitalia/fosdem/activities/MainActivity.java index d8074fa..115e3da 100644 --- a/app/src/main/java/be/digitalia/fosdem/activities/MainActivity.java +++ b/app/src/main/java/be/digitalia/fosdem/activities/MainActivity.java @@ -81,7 +81,7 @@ public class MainActivity extends AppCompatActivity { private final int iconResId; private final boolean keep; - private Section(Class fragmentClass, int titleResId, int iconResId, boolean keep) { + Section(Class fragmentClass, int titleResId, int iconResId, boolean keep) { this.fragmentClassName = fragmentClass.getName(); this.titleResId = titleResId; this.iconResId = iconResId; @@ -176,7 +176,9 @@ public class MainActivity extends AppCompatActivity { @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - return new AlertDialog.Builder(getActivity()).setTitle(R.string.download_reminder_title).setMessage(R.string.download_reminder_message) + return new AlertDialog.Builder(getActivity()) + .setTitle(R.string.download_reminder_title) + .setMessage(R.string.download_reminder_message) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override @@ -184,7 +186,8 @@ public class MainActivity extends AppCompatActivity { ((MainActivity) getActivity()).startDownloadSchedule(); } - }).setNegativeButton(android.R.string.cancel, null).create(); + }).setNegativeButton(android.R.string.cancel, null) + .create(); } } @@ -204,17 +207,9 @@ public class MainActivity extends AppCompatActivity { @Override public void onDrawerOpened(View drawerView) { - updateActionBar(); - supportInvalidateOptionsMenu(); // Make keypad navigation easier mainMenu.requestFocus(); } - - @Override - public void onDrawerClosed(View drawerView) { - updateActionBar(); - supportInvalidateOptionsMenu(); - } }; drawerToggle.setDrawerIndicatorEnabled(true); drawerLayout.setDrawerListener(drawerToggle); @@ -262,11 +257,7 @@ public class MainActivity extends AppCompatActivity { } private void updateActionBar() { - if (drawerLayout.isDrawerOpen(mainMenu)) { - getSupportActionBar().setTitle(null); - } else { - getSupportActionBar().setTitle(currentSection.getTitleResId()); - } + getSupportActionBar().setTitle(currentSection.getTitleResId()); } private void updateLastUpdateTime() { @@ -278,10 +269,6 @@ public class MainActivity extends AppCompatActivity { @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); - - if (drawerLayout.isDrawerOpen(mainMenu)) { - updateActionBar(); - } drawerToggle.syncState(); } @@ -369,22 +356,6 @@ public class MainActivity extends AppCompatActivity { return true; } - @Override - public boolean onPrepareOptionsMenu(Menu menu) { - // Hide & disable primary (contextual) action items when the main menu is opened - if (drawerLayout.isDrawerOpen(mainMenu)) { - final int size = menu.size(); - for (int i = 0; i < size; ++i) { - MenuItem item = menu.getItem(i); - if ((item.getOrder() & 0xFFFF0000) == 0) { - item.setVisible(false).setEnabled(false); - } - } - } - - return super.onPrepareOptionsMenu(menu); - } - @Override public boolean onOptionsItemSelected(MenuItem item) { // Will close the drawer if the home button is pressed @@ -535,6 +506,7 @@ public class MainActivity extends AppCompatActivity { ft.commit(); currentSection = section; + updateActionBar(); menuAdapter.notifyDataSetChanged(); } diff --git a/app/src/main/java/be/digitalia/fosdem/widgets/ScrimInsetsFrameLayout.java b/app/src/main/java/be/digitalia/fosdem/widgets/ScrimInsetsFrameLayout.java new file mode 100644 index 0000000..230e45b --- /dev/null +++ b/app/src/main/java/be/digitalia/fosdem/widgets/ScrimInsetsFrameLayout.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package be.digitalia.fosdem.widgets; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.support.v4.view.ViewCompat; +import android.support.v4.view.WindowInsetsCompat; +import android.util.AttributeSet; +import android.view.View; +import android.widget.FrameLayout; + +import be.digitalia.fosdem.R; + +public class ScrimInsetsFrameLayout extends FrameLayout { + private Drawable mInsetForeground; + private Rect mInsets; + private Rect mTempRect = new Rect(); + + public ScrimInsetsFrameLayout(Context context) { + this(context, null); + } + + public ScrimInsetsFrameLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public ScrimInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + final TypedArray a = context.obtainStyledAttributes(attrs, + R.styleable.ScrimInsetsFrameLayout, defStyleAttr, + R.style.Widget_Design_ScrimInsetsFrameLayout); + mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsFrameLayout_insetForeground); + a.recycle(); + setWillNotDraw(true); // No need to draw until the insets are adjusted + ViewCompat.setOnApplyWindowInsetsListener(this, + new android.support.v4.view.OnApplyWindowInsetsListener() { + @Override + public WindowInsetsCompat onApplyWindowInsets(View v, + WindowInsetsCompat insets) { + if (null == mInsets) { + mInsets = new Rect(); + } + mInsets.set(insets.getSystemWindowInsetLeft(), + insets.getSystemWindowInsetTop(), + insets.getSystemWindowInsetRight(), + insets.getSystemWindowInsetBottom()); + setWillNotDraw(mInsets.isEmpty() || mInsetForeground == null); + ViewCompat.postInvalidateOnAnimation(ScrimInsetsFrameLayout.this); + return insets.consumeSystemWindowInsets(); + } + }); + } + + @Override + public void draw(@NonNull Canvas canvas) { + super.draw(canvas); + int width = getWidth(); + int height = getHeight(); + if (mInsets != null && mInsetForeground != null) { + int sc = canvas.save(); + canvas.translate(getScrollX(), getScrollY()); + // Top + mTempRect.set(0, 0, width, mInsets.top); + mInsetForeground.setBounds(mTempRect); + mInsetForeground.draw(canvas); + // Bottom + mTempRect.set(0, height - mInsets.bottom, width, height); + mInsetForeground.setBounds(mTempRect); + mInsetForeground.draw(canvas); + // Left + mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom); + mInsetForeground.setBounds(mTempRect); + mInsetForeground.draw(canvas); + // Right + mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom); + mInsetForeground.setBounds(mTempRect); + mInsetForeground.draw(canvas); + canvas.restoreToCount(sc); + } + } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + if (mInsetForeground != null) { + mInsetForeground.setCallback(this); + } + } + + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + if (mInsetForeground != null) { + mInsetForeground.setCallback(null); + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/include_navigation_drawer.xml b/app/src/main/res/layout/include_navigation_drawer.xml new file mode 100644 index 0000000..33eacd9 --- /dev/null +++ b/app/src/main/res/layout/include_navigation_drawer.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/main.xml b/app/src/main/res/layout/main.xml index 5558acf..8f096a8 100644 --- a/app/src/main/res/layout/main.xml +++ b/app/src/main/res/layout/main.xml @@ -1,105 +1,53 @@ - + - + + + android:layout_height="match_parent"> - + android:layout_height="match_parent" + android:orientation="vertical"> - - - + + android:layout_height="0dp" + android:layout_weight="1"/> - + - + - + - + - + - - - - - - - - - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml index 9d28e32..b5ad257 100644 --- a/app/src/main/res/values-v21/styles.xml +++ b/app/src/main/res/values-v21/styles.xml @@ -5,6 +5,11 @@ + + +