How can i add custom background in BottomNavigationView - android

This is my xml code for bottommnavigationview
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:elevation="15dp"
android:fitsSystemWindows="true"
android:foreground="?attr/selectableItemBackground"
android:visibility="visible"
app:elevation="#dimen/margin_10dp"
app:itemBackground="?attr/backgroundColor"
app:itemIconSize="22dp"
app:itemIconTint="#color/bottom_navigation_text_selector"
app:itemTextAppearanceInactive="#style/BottomNavigation.InActiveItemTextAppearance"
app:itemTextAppearanceActive="#style/BottomNavigation.ActiveItemTextAppearance"
app:itemTextColor="#color/bottom_navigation_text_selector"
app:labelVisibilityMode="labeled"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/container"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/home_bottom_menu" />
See the red arrow i want to develop this background below my each selected item

Firstly you need to create a background drawable in drawable folder:
selected_item_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="12dp"
android:insetTop="12dp"
android:insetRight="12dp"
android:insetBottom="12dp"
android:visible="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview_background_shape">
<corners android:radius="8dp" />
<solid android:color="#eeeeee" />
</shape>
</inset>
then you should create a selector in the drawable folder based on this background:
bottom_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/selected_item_background" android:state_checked="true" />
<item android:drawable="#android:color/white" android:state_checked="false" />
</selector>
And then you just to need to add the attribute to your navigation view:
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemBackground="#drawable/bottom_item_selector"
...
app:menu="#menu/home_bottom_menu" />

Related

Custom indicator in Android

I need to do a custom tab indicator for my tab layout. It will be linked to a ViewPager with a TabLayoutMediator. The layout that I need is this:
And I am currently getting this:
Anyone knows why? You can see my XML code below.
Layout where TabLayout is:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/whiteCandy"
android:fitsSystemWindows="true">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/onBoardingViewPager"
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_0"
android:layout_marginStart="#dimen/tablet_default_margin"
android:layout_marginTop="#dimen/dimen_40"
android:layout_marginEnd="#dimen/tablet_default_margin"
android:layout_marginBottom="#dimen/dimen_75"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabDots"
android:layout_width="wrap_content"
android:layout_height="#dimen/dimen_6"
android:layout_marginStart="#dimen/dimen_30"
android:layout_marginBottom="#dimen/dimen_75"
android:background="#android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#id/onBoardingViewPager"
app:tabBackground="#drawable/tab_selector"
app:tabIndicatorHeight="#dimen/dimen_0"
app:tabPaddingEnd="#dimen/dimen_6" />
</androidx.constraintlayout.widget.ConstraintLayout>
Tab Selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/selected_dot" android:state_selected="true" />
<item android:drawable="#drawable/default_dot" />
</selector>
Selected state drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="#dimen/dimen_3"/>
<solid android:color="#FFFF0000"/>
<size android:height="#dimen/dimen_6" android:width="#dimen/dimen_20"/>
</shape>
Unselected state drawable:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="#dimen/dimen_0"
android:shape="ring"
android:thickness="#dimen/dimen_6"
android:useLevel="false">
<solid android:color="#80FF0000" />
</shape>
</item>
</layer-list>
for unselected:
<shape android:shape="oval">
<solid android:color="#80FF0000" />
<size
android:width="4dp"
android:height="4dp" />
</shape>

How to adjust ToggleButtons in Android such that they have a rim and change their color?

I have 3 ToggleButtons in a row as you can see in the screenshot:
I would like to have the following layout modifications:
There should be rims separating the Toggle Buttons
The fill in color of the unchecked Toggle Buttons should be white while the fill in color of the checked Toggle Button should be green sucht that you can see which of those is checked (I will make sure programatically that only one of those can be checked)
Here is my XML layout for the Toggle Buttons inside a Liner Layout:
<LinearLayout
android:id ="#+id/linearLayoutForButtons"
android:layout_width="0dp"
android:layout_height="#dimen/_25sdp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<ToggleButton
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAllCaps="false"
android:background="#95f9ad"
android:layout_weight="1"
android:checked="true"
android:textOn="Button 1"
android:textOff="Button 1"
android:textSize="#dimen/_8ssp"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#95f9ad"
android:layout_weight="1"
android:textAllCaps="false"
android:checked="false"
android:textSize="#dimen/_8ssp"
android:textOn="Button 2"
android:textOff="Button 2" />
<ToggleButton
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#95f9ad"
android:textAllCaps="false"
android:layout_weight="1"
android:checked="false"
android:textSize="#dimen/_8ssp"
android:textOn="Button 3"
android:textOff="Button 3"/>
</LinearLayout>
Any idea how I can do that? It should actually look similar to the ToggleButtons used hereenter link description here
Reminder: Does anyone have an idea how to do that? I'll appreciate every comment.
We can also create it like this
Perhaps this is more of what you are looking for. This does require some changes.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/root_view_background"
android:padding="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ToggleButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_1_selector"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_2_selector"
app:layout_constraintStart_toEndOf="#id/button1"
app:layout_constraintTop_toTopOf="#id/button1" />
<ToggleButton
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_3_selector"
app:layout_constraintStart_toEndOf="#id/button2"
app:layout_constraintTop_toTopOf="#id/button1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Drawable root_view_background.xml (This is the black outlining)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#color/black" android:width="1dp" />
<corners android:radius="3dp" />
</shape>
Drawable button_1_background_checked (This is the green background for the left button)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/holo_green_light" />
<corners android:topLeftRadius="3dp" android:bottomLeftRadius="3dp" />
</shape>
Drawable button_2_background_checked (This is the green background for the middle button)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/holo_green_light" />
</shape>
Drawable button_3_background checked (This is the green background for the right button)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/holo_green_light" />
<corners android:topRightRadius="3dp" android:bottomRightRadius="3dp" />
</shape>
Drawable button_1_selector (This changes the background of the first button when it's checked or not)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_1_background_checked" android:state_checked="true" />
<item android:drawable="#color/white" android:state_checked="false" />
</selector>
Drawable button_2_selector (This changes the background of the second button when it's checked or not)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_2_background_checked" android:state_checked="true" />
<item android:drawable="#color/white" android:state_checked="false" />
</selector>
Drawable button_3_selector (This changes the background of the last button when it's checked or not)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_3_background_checked" android:state_checked="true" />
<item android:drawable="#color/white" android:state_checked="false" />
</selector>

Not able to set gradient background of floating action button

I am trying to set the background of the floating action button as a gradient. I have already seen many solutions but none of them are working out for me. whatever I try it is showing me a black action button. I have tried setting the background to colour but that also didn't work. I am trying to create the bottom navigationBar.
My code
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
app:titleTextColor="#000000"
app:title="π•΄π–“π–˜π–™π–†π–Œπ–—π–†π–’"
app:elevation="8dp"
/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:backgroundTint="#color/colorPrimary"
android:layout_gravity="bottom"
app:elevation="8dp"
app:fabAlignmentMode="center"
app:fabCradleRoundedCornerRadius="32dp"
app:fabCradleMargin="10dp"
app:fabCradleVerticalOffset="18dp"
app:buttonGravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
app:menu="#menu/bottom_menu"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:itemIconTint="#color/bottom_nav_selector"
/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_button"
android:src="#drawable/fab_gradient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/bottomAppBar"
android:contentDescription="TODO" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
gradient
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient
android:angle="45"
android:endColor="#cd337b"
android:startColor="#f09448" />
</shape>
</item>
<item android:gravity="center">
<bitmap
android:gravity="fill_horizontal"
android:src="#drawable/ic_action_add" />
</item>
</layer-list>
In your theme.xml Create a style
<style name="Floating" parent="Widget.Design.FloatingActionButton">
<!---rest of your style-->
<item name="android:foreground">#drawable/gradient</item>
</style>
and in your FloatingActionButton apply this style :
<com.google.android.material.floatingactionbutton.FloatingActionButton
style="#style/Floating"
android:id="#+id/fab_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/bottomAppBar"
android:contentDescription="TODO" />
UPDATE:
to center the icon: in API 23 and above:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient
android:angle="45"
android:endColor="#cd337b"
android:startColor="#f09448" />
</shape>
</item>
<item
android:drawable="#drawable/common_full_open_on_phone"
android:gravity="center"
android:width="24dp"
android:height="24dp">
</item>
</layer-list>
and for API 23 and less :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient
android:angle="45"
android:endColor="#cd337b"
android:startColor="#f09448" />
</shape>
</item>
<item
android:drawable="#drawable/common_full_open_on_phone"
android:top="15dp"
android:right="15dp"
android:left="15dp"
android:bottom="15dp" />
</layer-list>

customize and android seekbar

I am trying to customize and android seekbar use images for background and progress.
but result is wrong,
multiple background
resource image width=351px,height=110px,
background,
progress
android:progressDrawable="#drawable/drawable_clip_seek_bar" content
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:width="351dp"
android:height="110dp">
<bitmap android:src="#mipmap/seek_bar_background" />
</item>
<item
android:id="#android:id/progress"
android:width="351dp"
android:height="110dp">
<clip>
<bitmap android:src="#mipmap/seek_bar_progress" />
</clip>
</item>
</layer-list>
activity contentView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/cardview_dark_background">
<SeekBar
android:layout_width="351dp"
android:layout_height="110dp"
android:background="#null"
android:progressDrawable="#drawable/drawable_clip_seek_bar"
android:thumb="#null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

how to color icon selected in bottomNavigationView with gradient color

I have bottomNavigationview with some icon and I want when anyone selected icon , color of the icon changed to gradient color .
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:startColor="#ee5f8a"
android:endColor="#ed8f6d"
android:angle="0"/>
</shape>
BottomNavigationview
<android.support.design.widget.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:elevation="5dp"
app:itemIconSize="35dp"
app:itemIconTint="#color/selector"
app:labelVisibilityMode="unlabeled"
app:menu="#menu/bottom_nav_menu" />
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorPrimaryDark"
android:state_checked="true" />
<item android:color="#c2bdbf" />
</selector>
Add this attribute to BottomNavigationView
app:itemIconTint="#drawable/bottom_selector"
Create one drawable named bottom_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ff0000"/>
<item android:color="#color/colorPrimaryDark"/>
</selector>
You need a drawable selector and not a color selector to create a gradient background.
drawable/selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/gradient" android:state_checked="true" />
</selector>
drawable/gradient.xml (same as question)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:startColor="#ee5f8a"
android:endColor="#ed8f6d"
android:angle="0"/>
</shape>
and finally your view
<android.support.design.widget.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:elevation="5dp"
app:itemIconSize="35dp"
app:itemBackground="#drawable/selector"
app:labelVisibilityMode="unlabeled"
app:menu="#menu/bottom_nav_menu" />

Categories

Resources