roughike / BottomBar

(Deprecated) A custom view component that mimics the new Material Design Bottom Navigation pattern.
Apache License 2.0
8.42k stars 1.5k forks source link

Example on how to switch activity or fragment #389

Open luzianscherrer opened 8 years ago

luzianscherrer commented 8 years ago

Can this component be used to switch between different activities? Or should I rather use fragments? Is there any sample code for either of those cases?

jayhack7 commented 8 years ago

I used fragments. This worked for me. I used the sample. ` public class CustomColorActivity extends AppCompatActivity {

private BottomBar mBottomBar;
private TextView mMessageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_basic);
    RelativeLayout fraglayout = (RelativeLayout) findViewById(R.id.rl_Container);
    //fraglayout.addView(TextView);
    mMessageView = (TextView) findViewById(R.id.messageView);

    // Customize the colors here
    mBottomBar = BottomBar.attach(this, savedInstanceState,
            Color.parseColor("#FFFFFF"), // Background Color
            ContextCompat.getColor(this, R.color.colorAccent), // Tab Item Color
            0.25f); // Tab Item Alpha

    mBottomBar.setItems(R.menu.bottombar_menu);

    mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
        @Override
        public void onMenuTabSelected(@IdRes int menuItemId) {
            switch (menuItemId) {
                case R.id.bb_menu_recents:
                    commitFragment(new OneFragment());
                    break;
                case R.id.bb_menu_favorites:
                    commitFragment(new TestFragment2());

                    break;
                case R.id.bb_menu_nearby:
                    mMessageView.setText(TabMessage.get(menuItemId, false));

                    break;
                case R.id.bb_menu_friends:
                    mMessageView.setText(TabMessage.get(menuItemId, false));

                    break;
                case R.id.bb_menu_food:
                    mMessageView.setText(TabMessage.get(menuItemId, false));

                    break;
            }
        }

        @Override
        public void onMenuTabReSelected(@IdRes int menuItemId) {
            Toast.makeText(getApplicationContext(), TabMessage.get(menuItemId, true), Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // Necessary to restore the BottomBar's state, otherwise we would
    // lose the current tab on orientation change.
    mBottomBar.onSaveInstanceState(outState);
}
private void commitFragment(Fragment fragment){
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.layout, fragment);
    fragmentTransaction.commit();
}

} ` I put a Relative Layout (called layout) in activity_basic.xml.

SebastianBO commented 8 years ago

Whats TabMessage?

smoothdvd commented 8 years ago

@pcodemang Looks BottomBar 2.0 is different.

shashwat-kalpvaig commented 7 years ago

@SebastianBO This is the best way to implement bottomView with view pager http://droidmentor.com/bottomnavigationview-with-viewpager-android/