Haryanto
Haryanto

Reputation: 355

Change background color Bottom Bar android

I have a problem in my Android project. I can't change the color of the bottombar.
This is how I want my bottombar to look: wanted Like this

This is my code

menu > tabhost_bottom.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"

>
<item
    android:id="@+id/home_item"
    android:icon="@drawable/ic_home"
    android:color="@color/colorPrimary"
    android:title="Home"
    />
<item
    android:id="@+id/setting_item"
    android:icon="@drawable/ic_setting"
    android:color="@color/colorPrimary"
    android:title="Setting" />

HomeActivity.java

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

    coordinatorLayout = (CoordinatorLayout) findViewById(R.id.tabhost_activity);

    BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
    bottomBar.setItemsFromMenu(R.menu.tabhost_bottom, new OnMenuTabSelectedListener() {
        @Override
        public void onMenuItemSelected(int itemId) {

        }
    });

    // Set the color for the active tab. Ignored on mobile when there are more than three tabs.
    bottomBar.setActiveTabColor("#55a8e5");

    // Use the dark theme. Ignored on mobile when there are more than three tabs.
    bottomBar.setBackgroundColor(Color.parseColor("#55a8e5"));
    // Use custom text appearance in tab titles.
    //bottomBar.setTextAppearance(R.style.MyTextAppearance);

    // Use custom typeface that's located at the "/src/main/assets" directory. If using with
    // custom text appearance, set the text appearance first.
    //bottomBar.setTypeFace("MyFont.ttf");
}

and this my reference

http://androidgifts.com/build-android-material-design-bottom-navigation/

Upvotes: 2

Views: 8976

Answers (4)

gundrabur
gundrabur

Reputation: 853

For all Kotlin fans: To set background color for all items in bottom bavigation bar, just add this lines in class MainActivity function onCreate:

// finds view in bottom_nav_menu.xml
val navView: BottomNavigationView = findViewById(R.id.nav_view)
// sets background color for the whole bar 
navView.setBackgroundColor(ContextCompat.getColor(this, R.color.yourColor))

Upvotes: 1

476rick
476rick

Reputation: 2795

When you are using this library (roughike's bottombar) you can try this.

You can set the backgroundcolor of a specific tab by using the following line:

bottomBar.getTabAtPosition(0).setBackgroundColor(backgroundColorInt);

When you use this line multiple times for each tab, you can change the backgroundcolor of the whole tab.

Upvotes: 0

Anytoe
Anytoe

Reputation: 1675

I've finally achieved to change it with the following code (Xamarin C#)

var bottomBarBackground = FindViewById(Resource.Id.bb_bottom_bar_background_view);
bottomBarBackground.SetBackgroundResource(Resource.Drawable.tabbar_background);

tabbar_background.axml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/YourColor"/>
        </shape>
    </item>
</layer-list>

Found also here, though the final solution did not work for me with the Xamarin version: Question about background color

Upvotes: 3

Is this the library?

Other Option:

seems like you need to set the Background color a different way:

    // Setting colors for different tabs when there's more than three of them.
    // You can set colors for tabs in three different ways as shown below.
    mBottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorAccent));
    mBottomBar.mapColorForTab(1, 0xFF5D4037);
    mBottomBar.mapColorForTab(2, "#7B1FA2");
    mBottomBar.mapColorForTab(3, "#FF5252");
    mBottomBar.mapColorForTab(4, "#FF9800");

For more inforation: Android new Bottom Navigation bar

Upvotes: 1

Related Questions