Simple library for bottom tab layout
Download via Gradle:
compile 'com.github.stfalcon:bottomtablayout:0.3'or Maven:
<dependency>
<groupId>com.github.stfalcon</groupId>
<artifactId>bottomtablayout</artifactId>
<version>0.3</version>
<type>pom</type>
</dependency>Create text selector:
<?xml version="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:color="@color/black"android:state_pressed="true" /> <!-- pressed -->
<itemandroid:color="@color/black"android:state_selected="true" /> <!-- selected -->
<itemandroid:color="@color/white" /> <!-- default -->
</selector>Create drawable selector for each button icon:
<?xml version="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:drawable="@drawable/ic_button1_dark"android:state_pressed="true" /> <!-- pressed -->
<itemandroid:drawable="@drawable/ic_button1_dark"android:state_selected="true" /> <!-- selected -->
<itemandroid:drawable="@drawable/ic_button1" /> <!-- default -->
</selector>Create menu resource file with items. Where title is title of button and icon is button drawable.
<?xml version="1.0" encoding="utf-8"?>
<menuxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:id="@+id/menu_button1"android:icon="@drawable/tab_button1_selector"android:title="Button1" />
<itemandroid:id="@+id/menu_button2"android:icon="@drawable/tab_button1_selector"android:title="Button2" />
<itemandroid:id="@+id/menu_button3"android:icon="@drawable/tab_button1_selector"android:title="Button3" />
<itemandroid:id="@+id/menu_button4"android:icon="@drawable/tab_button1_selector"android:title="Button4" />
<itemandroid:id="@+id/menu_button5"android:icon="@drawable/tab_button1_selector"android:title="Button5" />
</menu>Create style for button
<stylename="TabButtonTextStyle"parent="android:Widget.Button">
<itemname="android:textSize">12sp</item>
<itemname="android:textColor">@drawable/tab_button_text_selector</item>
</style>Add bottomtablayout view to activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.stfalcon.bottomtablayout_sample.MainActivity">
<FrameLayoutandroid:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/bottomTabLayout" />
<com.stfalcon.bottomtablayout.BottomTabLayout
android:id="@+id/bottomTabLayout"android:layout_width="match_parent"android:layout_height="58dp"android:layout_alignParentBottom="true"android:background="@color/dark" />
</RelativeLayout>In activity class:
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = R.id.container; //Container for fragments//Setup button tab layoutbottomTabLayout = (BottomTabLayout) findViewById(R.id.bottomTabLayout);
//set button text stylebottomTabLayout.setButtonTextStyle(R.style.TextGray16);
// set buttons from menu resourcebottomTabLayout.setItems(R.menu.menu_bottom_layout);
//set on selected tab listener.bottomTabLayout.setListener(newBottomTabLayout.OnItemSelectedListener() {
@OverridepublicvoidonItemSelected(intid) {
switchFragment(id);
}
});
//set button that will be select on start activitybottomTabLayout.setSelectedTab(R.id.menu_button1);
//enable indicatorbottomTabLayout.setIndicatorVisible(true);
//indicator heightbottomTabLayout.setIndicatorHeight(getResources().getDimension(R.dimen.indicator_height));
//indicator colorbottomTabLayout.setIndicatorColor(R.color.green);
//indicator line colorbottomTabLayout.setIndicatorLineColor(R.color.dark);
}For example we can switch fragments in container:
/** * Show fragment in container * @param id Menu item res id */publicvoidswitchFragment(intid) {
Fragmentfragment = null;
switch (id) {
caseR.id.menu_button1:
fragment = ColoredFragment.newInstance(R.color.blue, "Fragment 1");
break;
caseR.id.menu_button2:
fragment = ColoredFragment.newInstance(R.color.green, "Fragment 2");
break;
caseR.id.menu_button3:
fragment = ColoredFragment.newInstance(R.color.pink, "Fragment 3");
break;
caseR.id.menu_button4:
fragment = ColoredFragment.newInstance(R.color.blueDark, "Fragment 4");
break;
caseR.id.menu_button5:
fragment = ColoredFragment.newInstance(R.color.white, "Fragment 5");
break;
}
if (fragment != null) {
FragmentTransactiontransaction = getSupportFragmentManager().beginTransaction();
transaction.replace(container, fragment);
transaction.commit();
}
}For showing bubble count on tab.
bottomTabLayout.showTabBubbleCount(R.id.menu_button1, 3);You can style bubble with methods:
bottomTabLayout.setTabBubbleColor(ContextCompat.getColor(this, R.color.blue));
bottomTabLayout.setTabBubblePadding(0, 0, 0, 0);
bottomTabLayout.setTabBubbleTextStyle(R.style.TextWhite12);Take a look at the sample project for more information.
Copyright 2017 stfalcon.com
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.
