Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Add default selected item for bottom navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandros Koufatzis authored and Alexandros Koufatzis committed Nov 23, 2016
1 parent 8838229 commit 575dd00
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.akoufatzis.weatherapp.cityweathermain;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
Expand Down Expand Up @@ -38,6 +39,8 @@ protected int getLayoutResourceId() {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
// Set default selected item
selectFragmentForMenuItemId(R.id.action_search);
}

@Override
Expand All @@ -49,10 +52,16 @@ protected void onDestroy() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

selectFragmentForMenuItemId(item.getItemId());
return true;
}

public void selectFragmentForMenuItemId(@IdRes int itemId) {

FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment;

switch (item.getItemId()) {
switch (itemId) {

case R.id.action_search:
fragment = CityWeatherSearchFragment.newInstance();
Expand All @@ -72,7 +81,5 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {

fragmentManager.beginTransaction().replace(R.id.bottom_bar_fragment_container,
fragment, BOTTOM_BAR_TAG).commit();

return true;
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_citiyweathermain.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
app:itemBackground="@color/primary_dark"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:layout_scrollFlags="scroll|enterAlways"
app:menu="@menu/bottom_navigation_main" />
</android.support.design.widget.CoordinatorLayout>

0 comments on commit 575dd00

Please sign in to comment.