Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for shadow #716

Merged
merged 3 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 54 additions & 18 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 @@ -76,11 +78,11 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie
private int titleTextAppearance;
private Typeface titleTypeFace;
private boolean showShadow;
private float shadowElevation;

private View backgroundOverlay;
private ViewGroup outerContainer;
private ViewGroup tabContainer;
private View shadowView;

private int defaultBackgroundColor = Color.WHITE;
private int currentBackgroundColor;
Expand All @@ -107,35 +109,77 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie
private BottomBarTab[] currentTabs;

public BottomBar(Context context) {
super(context);
init(context, null);
this(context, null);
}

public BottomBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
this(context, attrs, 0);
}

private void init(Context context, AttributeSet attrs) {
public BottomBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public BottomBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
batchPropertyApplier = new BatchTabPropertyApplier(this);

populateAttributes(context, attrs);
populateAttributes(context, attrs, defStyleAttr, defStyleRes);
initializeViews();
determineInitialBackgroundColor();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
init21(context);
}

if (tabXmlResource != 0) {
setItems(tabXmlResource);
}
}

private void populateAttributes(Context context, AttributeSet attrs) {
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

// This is so that in Pre-Lollipop devices there is a shadow BUT without pushing the content
if (showShadow) {
ViewGroup.LayoutParams params = getLayoutParams();
if (params instanceof MarginLayoutParams) {
MarginLayoutParams layoutParams = (MarginLayoutParams) params;
final int shadowHeight = getResources().getDimensionPixelSize(R.dimen.bb_fake_shadow_height);

layoutParams.setMargins(layoutParams.leftMargin,
layoutParams.topMargin - shadowHeight,
layoutParams.rightMargin,
layoutParams.bottomMargin);
setLayoutParams(params);
}
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private void init21(Context context) {
shadowElevation = getElevation();
shadowElevation = shadowElevation > 0
? shadowElevation
: getResources().getDimensionPixelSize(R.dimen.bb_default_elevation);
setElevation(MiscUtils.dpToPixel(context, shadowElevation));
setOutlineProvider(ViewOutlineProvider.BOUNDS);
}

private void populateAttributes(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
primaryColor = MiscUtils.getColor(getContext(), R.attr.colorPrimary);
screenWidth = MiscUtils.getScreenWidth(getContext());
tenDp = MiscUtils.dpToPixel(getContext(), 10);
maxFixedItemWidth = MiscUtils.dpToPixel(getContext(), 168);

TypedArray ta = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.BottomBar, 0, 0);
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BottomBar, defStyleAttr, defStyleRes);

try {
tabXmlResource = ta.getResourceId(R.styleable.BottomBar_bb_tabXmlResource, 0);
Expand Down Expand Up @@ -196,7 +240,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 +248,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 +389,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:layout_height="@dimen/bb_fake_shadow_height"
android:background="@drawable/bb_bottom_bar_top_shadow" />

<FrameLayout
android:id="@+id/bb_bottom_bar_outer_container"
Expand Down
2 changes: 2 additions & 0 deletions bottom-bar/src/main/res/values/dimensions.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="bb_height">56dp</dimen>
<dimen name="bb_default_elevation">8dp</dimen>
<dimen name="bb_fake_shadow_height">4dp</dimen>
</resources>