public class CustomBottomNavigationBar extends FrameLayout { // These would be names and associated positions of the tabs specific to your app public static final int POSITION_HOME_TAB = 0; public static final int POSITION_UPDATES_TAB = 2; public static final int POSITION_FINISHED_STORIES_TAB = 1; public static final int POSITION_BOOKMARKS_TAB = 3; public static final int POSITION_PROFILE_TAB = 4; // Passed in from the activities using this, in the setter below private OnTabSelectionListener mTabSelectionListener; // For some first-time arrangements of the layout private boolean drawn; private LayoutBottomBarBinding binding; // To store resource ids of the drawable for the icons, the order of tabs above should be the same private Integer[] tabResId; // Used in initial layout private boolean shadowDrawn; public CustomBottomNavigationBar(Context context) { super(context); initLayout(context); } public CustomBottomNavigationBar(Context context, AttributeSet attrs) { super(context, attrs); initLayout(context); } public CustomBottomNavigationBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initLayout(context); } public CustomBottomNavigationBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initLayout(context); } private void initLayout(Context context) { LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); binding = DataBindingUtil.inflate(layoutInflater, R.layout.layout_bottom_bar, this, true); tabResId = new Integer[]{R.id.home_tab_btn, R.id.leaderboard_tab_btn, R.id.activity_tab_btn, R.id.bookmarks_tab_btn, R.id.profile_tab_btn}; binding.homeTabBtn.setOnClickListener(new InternalTabSelectionListener(POSITION_HOME_TAB)); binding.activityTabBtn.setOnClickListener(new InternalTabSelectionListener(POSITION_UPDATES_TAB)); binding.leaderboardTabBtn.setOnClickListener(new InternalTabSelectionListener(POSITION_FINISHED_STORIES_TAB)); binding.bookmarksTabBtn.setOnClickListener(new InternalTabSelectionListener(POSITION_BOOKMARKS_TAB)); binding.profileTabBtn.setOnClickListener(new InternalTabSelectionListener(POSITION_PROFILE_TAB)); setWillNotDraw(false); setClipToPadding(false); setClipToOutline(false); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!drawn) { drawn = true; Rect rect = new Rect(); ((View) getParent()).getGlobalVisibleRect(rect); setY(rect.bottom - rect.top - getContext().getResources().getDimension(R.dimen.bottom_bar_with_shadow_height)); invalidate(); } else if (!shadowDrawn) { shadowDrawn = true; binding.shadowGradient.draw(canvas); } } public int getBottomBarHeight() { return binding.bottomBarLayout.getHeight(); } public void setOnTabSelectionListener(OnTabSelectionListener tabSelectionListener) { this.mTabSelectionListener = tabSelectionListener; } public void setSelectedTab(int position) { binding.bottomBarLayout.check(tabResId[position]); } public interface OnTabSelectionListener { void onTabSelected(int position); } private class InternalTabSelectionListener implements OnClickListener { private final int mCurrentPosition; public InternalTabSelectionListener(int currentTabPosition) { mCurrentPosition = currentTabPosition; } @Override public void onClick(View view) { if (mTabSelectionListener != null) { mTabSelectionListener.onTabSelected(mCurrentPosition); } } } }