Skip to content

Commit

Permalink
Merge d8d9c56 into 3fa28ae
Browse files Browse the repository at this point in the history
  • Loading branch information
roughike committed Mar 28, 2017
2 parents 3fa28ae + d8d9c56 commit 6db2321
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

(or: y u no add shiny new things?!)

### 2.2.0

* Ability to change icons when the tabs are selected, using drawable selectors
* Overriding tab selections is now supported, by using [TabSelectionInterceptor](https://github.com/roughike/BottomBar/blob/master/bottom-bar/src/main/java/com/roughike/bottombar/TabSelectionInterceptor.java)
* Internal code quality improvements and small changes

### 2.1.2

* Merged [#703](https://github.com/roughike/BottomBar/pull/703) that allows controlling badge visibility for tabs that are active.
Expand Down
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,49 @@ bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
});
```

### Intercepting tab selections

If you want to conditionally cancel selection of any tab, you absolutely can. Just assign a ```TabSelectionInterceptor``` to the BottomBar, and return true from the ```shouldInterceptTabSelection()``` method.

```java
bottomBar.setTabSelectionInterceptor(new TabSelectionInterceptor() {
@Override
public boolean shouldInterceptTabSelection(@IdRes int oldTabId, @IdRes int newTabId) {
if (newTabId == R.id.tab_pro_feature && !userHasProVersion()) {
startProVersionPurchaseFlow();
return true;
}

return false;
}
});
```

### Changing icons based on selection state

If you want to have different icon when a specific tab is selected, just use state list drawables.

**res/drawable/my_tab_icon.xml**

```xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_myicon_selected" android:state_selected="true" />
<item android:drawable="@drawable/ic_myicon_default" android:state_selected="false" />
</selector>
```

**res/xml/bottombar_tabs.xml**

```xml
...
<tab
id="@+id/tab_favorites"
icon="@drawable/my_tab_icon"
title="Favorites" />
<!-- You can use @color resources too! -->
...
```

### Those color changing tabs look dope. Howdoidodat?

Just add ```barColorWhenSelected``` to each tab. When that tab is selected, the whole BottomBar background color is changed with a nice animation.
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</intent-filter>
</activity>
<activity android:name=".ThreeTabsActivity" />
<activity android:name=".IconsOnlyActivity" />
<activity android:name=".FiveColorChangingTabsActivity" android:theme="@style/AppTheme.TransNav" />
<activity android:name=".ThreeTabsQRActivity" android:theme="@style/AppTheme.TransNav" />
<activity android:name=".BadgeActivity" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.bottombar.sample;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.widget.TextView;
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabReselectListener;
import com.roughike.bottombar.OnTabSelectListener;

public class IconsOnlyActivity extends Activity {
private TextView messageView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icons_only);

messageView = (TextView) findViewById(R.id.messageView);

BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
messageView.setText(TabMessage.get(tabId, false));
}
});

bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
@Override
public void onTabReSelected(@IdRes int tabId) {
Toast.makeText(getApplicationContext(), TabMessage.get(tabId, true), Toast.LENGTH_LONG).show();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

findViewById(R.id.simple_three_tabs).setOnClickListener(this);
findViewById(R.id.icons_only).setOnClickListener(this);
findViewById(R.id.five_tabs_changing_colors).setOnClickListener(this);
findViewById(R.id.three_tabs_quick_return).setOnClickListener(this);
findViewById(R.id.five_tabs_custom_colors).setOnClickListener(this);
Expand All @@ -27,6 +28,9 @@ public void onClick(View v) {
case R.id.simple_three_tabs:
clazz = ThreeTabsActivity.class;
break;
case R.id.icons_only:
clazz = IconsOnlyActivity.class;
break;
case R.id.five_tabs_changing_colors:
clazz = FiveColorChangingTabsActivity.class;
break;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_color_changing_tabs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_height="64dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="@xml/bottombar_tabs_color_changing"
app:bb_behavior="shifting|underNavbar" />
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/res/layout/activity_icons_only.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
android:id="@+id/messageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomBar"
android:gravity="center"
android:text="Hi mom!" />

<com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="@xml/bottombar_tabs_five"
app:bb_behavior="iconsOnly" />

</RelativeLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
android:layout_height="wrap_content"
android:text="Simple example with three tabs" />

<Button
android:id="@+id/icons_only"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tabs with icons only" />

<Button
android:id="@+id/five_tabs_custom_colors"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

import static org.junit.Assert.assertEquals;

/**
* Created by iiro on 22.8.2016.
*/
@RunWith(AndroidJUnit4.class)
public class BottomBarTabTest {
private FrameLayout tabContainer;
Expand All @@ -28,19 +25,25 @@ public void setUp() {
}

@Test
public void correctLayoutReturned_ForFixedTab() {
public void correctLayoutReturnedForFixedTab() {
tab.setType(BottomBarTab.Type.FIXED);
assertEquals(R.layout.bb_bottom_bar_item_fixed, tab.getLayoutResource());
}

@Test(expected = IllegalStateException.class)
public void setIsTitleless_WhenTrueAndIconDoesNotExist_ThrowsException() {
tab.setIsTitleless(true);
assertEquals(R.layout.bb_bottom_bar_item_titleless, tab.getLayoutResource());
}

@Test
public void correctLayoutReturned_ForShiftingTab() {
public void correctLayoutForShiftingTab() {
tab.setType(BottomBarTab.Type.SHIFTING);
assertEquals(R.layout.bb_bottom_bar_item_shifting, tab.getLayoutResource());
}

@Test
public void correctLayoutReturned_ForTabletTab() {
public void correctLayoutForTabletTab() {
tab.setType(BottomBarTab.Type.TABLET);
assertEquals(R.layout.bb_bottom_bar_item_fixed_tablet, tab.getLayoutResource());
}
Expand Down
12 changes: 12 additions & 0 deletions bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie
private static final int BEHAVIOR_SHIFTING = 1;
private static final int BEHAVIOR_SHY = 2;
private static final int BEHAVIOR_DRAW_UNDER_NAV = 4;
private static final int BEHAVIOR_ICONS_ONLY = 8;

private BatchTabPropertyApplier batchPropertyApplier;
private int primaryColor;
private int screenWidth;
Expand Down Expand Up @@ -220,6 +222,8 @@ private boolean isShy() {
return !isTabletMode && hasBehavior(BEHAVIOR_SHY);
}

private boolean isIconsOnlyMode() { return !isTabletMode && hasBehavior(BEHAVIOR_ICONS_ONLY); }

private boolean hasBehavior(int behavior) {
return (behaviors | behavior) == behaviors;
}
Expand Down Expand Up @@ -323,6 +327,10 @@ private void updateItems(final List<BottomBarTab> bottomBarItems) {
type = BottomBarTab.Type.FIXED;
}

if (isIconsOnlyMode()) {
bottomBarTab.setIsTitleless(true);
}

bottomBarTab.setType(type);
bottomBarTab.prepareLayout();

Expand Down Expand Up @@ -718,6 +726,10 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
}

private void updateTitleBottomPadding() {
if (isIconsOnlyMode()) {
return;
}

int tabCount = getTabCount();

if (tabContainer == null || tabCount == 0 || !isShiftingMode()) {
Expand Down

0 comments on commit 6db2321

Please sign in to comment.