Skip to content

Commit

Permalink
- Removing the view for API 21
Browse files Browse the repository at this point in the history
- Adding a negative margin for API 20 or less so that the content is overlapped with the shadow
- Fix the annotation for the MiscUtils
  • Loading branch information
yombunker committed Mar 27, 2017
1 parent dbad9bb commit 7b8f597
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
35 changes: 26 additions & 9 deletions bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.XmlRes;
import android.support.design.widget.CoordinatorLayout;
Expand All @@ -26,6 +27,7 @@
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.TextView;
Expand Down Expand Up @@ -80,7 +82,6 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie
private View backgroundOverlay;
private ViewGroup outerContainer;
private ViewGroup tabContainer;
private View shadowView;

private int defaultBackgroundColor = Color.WHITE;
private int currentBackgroundColor;
Expand Down Expand Up @@ -117,6 +118,10 @@ public BottomBar(Context context, AttributeSet attrs) {
}

private void init(Context context, AttributeSet attrs) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
init21(context);
}

batchPropertyApplier = new BatchTabPropertyApplier(this);

populateAttributes(context, attrs);
Expand All @@ -128,6 +133,26 @@ private void init(Context context, AttributeSet attrs) {
}
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

if (showShadow) {
ViewGroup.LayoutParams params = getLayoutParams();
if (params instanceof MarginLayoutParams) {
MarginLayoutParams layoutParams = (MarginLayoutParams) params;
layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin - MiscUtils.dpToPixel(getContext(), 4), layoutParams.rightMargin, layoutParams.bottomMargin);
setLayoutParams(params);
}
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private void init21(Context context) {
setElevation(MiscUtils.dpToPixel(context, 8));
setOutlineProvider(ViewOutlineProvider.BOUNDS);
}

private void populateAttributes(Context context, AttributeSet attrs) {
primaryColor = MiscUtils.getColor(getContext(), R.attr.colorPrimary);
screenWidth = MiscUtils.getScreenWidth(getContext());
Expand Down Expand Up @@ -196,7 +221,6 @@ private void initializeViews() {

setLayoutParams(params);
setOrientation(isTabletMode ? HORIZONTAL : VERTICAL);
ViewCompat.setElevation(this, MiscUtils.dpToPixel(getContext(), 8));

View rootView = inflate(getContext(),
isTabletMode ? R.layout.bb_bottom_bar_item_container_tablet : R.layout.bb_bottom_bar_item_container, this);
Expand All @@ -205,11 +229,6 @@ private void initializeViews() {
backgroundOverlay = rootView.findViewById(R.id.bb_bottom_bar_background_overlay);
outerContainer = (ViewGroup) rootView.findViewById(R.id.bb_bottom_bar_outer_container);
tabContainer = (ViewGroup) rootView.findViewById(R.id.bb_bottom_bar_item_container);
shadowView = rootView.findViewById(R.id.bb_bottom_bar_shadow);

if (!showShadow) {
shadowView.setVisibility(GONE);
}
}

private void determineInitialBackgroundColor() {
Expand Down Expand Up @@ -351,8 +370,6 @@ private void resizeTabsToCorrectSizes(BottomBarTab[] tabsToAdd) {
if (tabView.getParent() == null) {
tabContainer.addView(tabView);
}

tabView.requestLayout();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.Dimension;
import android.support.annotation.NonNull;
import android.support.annotation.Px;
Expand Down Expand Up @@ -35,7 +35,7 @@
class MiscUtils {

@ColorInt
protected static int getColor(@NonNull Context context, @ColorRes int color) {
protected static int getColor(@NonNull Context context, @AttrRes int color) {
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(color, tv, true);
return tv.data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<FrameLayout
android:id="@+id/bb_bottom_bar_outer_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<View
android:id="@+id/bb_bottom_bar_background_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />

<LinearLayout
android:id="@+id/bb_bottom_bar_item_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" />

</FrameLayout>

</merge>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
<View
android:id="@+id/bb_bottom_bar_shadow"
android:layout_width="match_parent"
android:layout_height="4dp"
android:src="@drawable/bb_bottom_bar_top_shadow"/>
android:background="@drawable/bb_bottom_bar_top_shadow" />

<FrameLayout
android:id="@+id/bb_bottom_bar_outer_container"
Expand Down

0 comments on commit 7b8f597

Please sign in to comment.