Skip to content

Commit

Permalink
Change theme and fix bottom navigation issues (#2894)
Browse files Browse the repository at this point in the history
* WIP: Fixed menu highlights for the main bottom navigation

The current fix uses setSelectedItemId to make sure that the correct
menu item is selected. This API was introduced in SDK 25.3.0.

* Changed the bottom navigation color scheme to see clearly what item is selected

* Removed redundant bottom navigation declaration

* Fixed active/inactive size + deselected the fist item for activities not mentioned in the bottom navigation bar

* Make sure that the Home navigation item is highlighted on activity resume

* Made the Comparison activity to redirect to Home like all activities

A problem with this was that onOptionItemSelected returned true by
default which blocked the call from completing. In our case, we don't
have to override the call.

Also, the manifest needed to be modified to redirect you to the home
activity.

* Reworked bottom navigation logic + added compatibility code

* Fixed trucated navigation item when the text was to large

Fixed the issue described into this material fix:
material-components/material-components-android#139
  • Loading branch information
Steinhagen authored and VaiTon committed Dec 27, 2019
1 parent 59aa2a5 commit 02a776c
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 48 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ dependencies {
implementation "androidx.cardview:cardview:$rootProject.supportLibraryVersion"
implementation "androidx.legacy:legacy-support-v4:$rootProject.supportLibraryVersion"
implementation "androidx.legacy:legacy-preference-v14:$rootProject.supportLibraryVersion"
implementation "com.google.android.material:material:$rootProject.supportLibraryVersion"
implementation "com.google.android.material:material:$rootProject.materialVersion"
implementation "androidx.annotation:annotation:$rootProject.supportLibraryVersion"
implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayout"
implementation "androidx.multidex:multidex:$rootProject.multidexVersion"
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".views.MainActivity" />
</activity>
<activity
android:name=".views.ProductComparisonActivity"
android:parentActivityName=".views.MainActivity" />
<activity
android:name=".views.category.activity.CategoryActivity"
android:launchMode="singleTop"
Expand Down Expand Up @@ -188,8 +191,6 @@
android:name=".views.ProductListsActivity"
android:parentActivityName=".views.MainActivity" />
<activity android:name=".views.YourListedProducts" />
<activity android:name=".views.ProductComparisonActivity" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstan
productRepository = ProductRepository.getInstance();
mDataObserver = new DataObserver();
bottomNavigationView = view.findViewById((R.id.bottom_navigation));
BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, 0);
BottomNavigationListenerInstaller.install(bottomNavigationView,getActivity(),getContext());
productRepository.getAllergensByEnabledAndLanguageCode(true, Locale.getDefault().getLanguage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
searchBarcode(barCode);
}
}

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, 0);
BottomNavigationListenerInstaller.install(bottomNavigationView, getActivity(), getContext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
apiClient = new OpenFoodAPIClient(getActivity()).getAPIService();
checkUserCredentials();
sp = PreferenceManager.getDefaultSharedPreferences(getContext());
BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, R.id.home_page);
BottomNavigationListenerInstaller.install(bottomNavigationView, getActivity(), getContext());
}

Expand Down Expand Up @@ -151,6 +152,7 @@ public void onPause() {
public void onResume() {

super.onResume();
BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, R.id.home_page);

int productCount = sp.getInt("productCount", 0);
apiClient.getTotalProductCount(Utils.getUserAgent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ protected void onCreate(Bundle savedInstanceState) {
recyclerView.addItemDecoration(new DividerItemDecoration(AdditivesExplorer.this, DividerItemDecoration.VERTICAL));
});

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, 0);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, getBaseContext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,8 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
return false;
});

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, R.id.scan_bottom_nav);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
new FillAdapter(HistoryScanActivity.this).execute(context);
swipeRefreshLayout.setRefreshing(false);
});

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, R.id.history_bottom_nav);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ protected void onCreate(Bundle savedInstanceState) {
if (scanOnShake) {
mShakeDetector.setOnShakeListener(count -> Utils.scan(ProductBrowsingListActivity.this));
}

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, 0);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, getBaseContext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

Expand All @@ -30,15 +29,23 @@
import openfoodfacts.github.scrachx.openfood.views.listeners.BottomNavigationListenerInstaller;

public class ProductComparisonActivity extends BaseActivity implements PhotoReceiver {
@BindView(R.id.toolbar)
Toolbar toolbar;
private PhotoReceiverHandler photoReceiverHandler;
private RecyclerView.Adapter productComparisonAdapter;
private ArrayList<Product> products = new ArrayList<>();
@BindView(R.id.bottom_navigation)
BottomNavigationView bottomNavigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_comparison);
setTitle(getString(R.string.compare_products));

setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

photoReceiverHandler=new PhotoReceiverHandler(this);

if (getIntent().getExtras() != null && getIntent().getBooleanExtra("product_found", false)) {
Expand Down Expand Up @@ -79,21 +86,9 @@ protected void onCreate(Bundle savedInstanceState) {
}
}
});
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, getBaseContext());
setTitle(getString(R.string.compare_products));

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
finish();
return true;
}
return false;
BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, R.id.compare_products);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, getBaseContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, R.id.my_lists);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, getBaseContext());
fabAdd.setCompoundDrawablesWithIntrinsicBounds(R.drawable.plus_blue, 0, 0, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ protected void onCreate(Bundle savedInstanceState) {
ItemTouchHelper itemTouchhelper = new ItemTouchHelper(swipeController);
itemTouchhelper.attachToRecyclerView(recyclerView);
}

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, 0);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, getBaseContext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ protected void onCreate(Bundle savedInstanceState) {
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(CategoryActivity.this, Uri.parse(url));
});

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, 0);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, getBaseContext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import com.google.android.material.bottomnavigation.BottomNavigationView;
import android.util.Log;

import java.util.HashSet;
import java.util.Set;

import openfoodfacts.github.scrachx.openfood.R;

public class BottomNavigationListenerInstaller {
/*
public method in order to disable shift mode in the bottom navigation bar
Expand Down Expand Up @@ -34,6 +39,24 @@ private static void disableShiftMode(BottomNavigationView view) {
// }
}

public static void selectNavigationItem(BottomNavigationView bottomNavigationView, int itemId) {
Set<Integer> navItems = new HashSet<Integer>();
navItems.add(R.id.scan_bottom_nav);
navItems.add(R.id.compare_products);
navItems.add(R.id.home_page);
navItems.add(R.id.history_bottom_nav);
navItems.add(R.id.my_lists);

if (navItems.contains(itemId)) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1){
bottomNavigationView.setSelectedItemId(itemId);
} else{
bottomNavigationView.getMenu().findItem(itemId).setChecked(true);
}
} else {
bottomNavigationView.getMenu().getItem(0).setCheckable(false);
}
}

public static void install(BottomNavigationView bottomNavigationView, Activity activity, Context context) {
try {
Expand All @@ -42,7 +65,7 @@ public static void install(BottomNavigationView bottomNavigationView, Activity a
Log.i(BottomNavigationListenerInstaller.class.getSimpleName(),"install",e);
}

bottomNavigationView.setOnNavigationItemSelectedListener(new CommonBottomListener(activity,context));
bottomNavigationView.setOnNavigationItemSelectedListener(new CommonBottomListener(activity, context));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ protected void onCreate(Bundle savedInstanceState) {
Utils.scan(ProductActivity.this);
}
});

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, 0);
BottomNavigationListenerInstaller.install(bottomNavigationView, this, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
Utils.scan(getActivity());
}
});

BottomNavigationListenerInstaller.selectNavigationItem(bottomNavigationView, 0);
BottomNavigationListenerInstaller.install(bottomNavigationView, getActivity(), getContext());
return view;
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/color/navigation_bottom_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/button_link" />
<item android:color="@color/gray" />
</selector>
12 changes: 3 additions & 9 deletions app/src/main/res/layout/activity_continuous_scan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,11 @@

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
<include
layout="@layout/navigation_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:itemBackground="@color/blue_500"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/quick_view_layout"
app:menu="@menu/navigation_bottom" />
app:layout_constraintTop_toBottomOf="@id/quick_view_layout" />

</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 3 additions & 3 deletions app/src/main/res/layout/navigation_bottom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:itemBackground="@color/blue_500"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
android:background="?android:attr/windowBackground"
app:itemIconTint="@color/navigation_bottom_item"
app:itemTextColor="@color/navigation_bottom_item"
app:labelVisibilityMode="labeled"
app:menu="@menu/navigation_bottom"/>
21 changes: 5 additions & 16 deletions app/src/main/res/menu/navigation_bottom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,28 @@
app:showAsAction="always|withText">
<item
android:id="@+id/scan_bottom_nav"
android:enabled="true"
android:icon="@drawable/barcode_white_24dp"
android:title="@string/scan_shortcut_short_label"
/>
android:title="@string/scan_shortcut_short_label"/>

<item
android:id="@+id/compare_products"
android:enabled="true"
android:icon="@drawable/ic_compare_arrows_black_18dp"
android:title="@string/product_summary_action_compare_button_label"
/>
android:title="@string/product_summary_action_compare_button_label"/>


<item
android:id="@+id/home_page"
android:enabled="true"
android:icon="@drawable/baseline_home_black_36dp"
android:title="@string/home_drawer"
/>
android:title="@string/home_drawer"/>


<item
android:id="@+id/history_bottom_nav"
android:enabled="true"
android:icon="@drawable/ic_history_grey_24dp"
android:title="@string/bottomNav_history"
/>
android:title="@string/bottomNav_history"/>

<item
android:id="@+id/my_lists"
android:enabled="true"
android:icon="@drawable/playlist_add"
android:title="@string/your_lists" />


android:title="@string/your_lists"/>
</menu>
6 changes: 5 additions & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">

<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
Expand Down Expand Up @@ -64,4 +64,8 @@

<dimen name="scan_summary_peek_large">198dp</dimen>
<dimen name="scan_summary_peek_small">120dp</dimen>

<!-- Override the navigation dimensions to be identical in both active and inactive states -->
<dimen name="design_bottom_navigation_text_size" tools:override="true">12sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">12sp</dimen>
</resources>
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ allprojects {

ext {
supportLibraryVersion = '1.0.0'

materialVersion = '1.1.0-beta01'

multidexVersion = '2.0.1'

workRuntime = '2.2.0'
Expand Down

0 comments on commit 02a776c

Please sign in to comment.