TabLayout transition state - android

I want to have a tablayout, in which the current tab would have a purple background and white text while the unselected would have white background and purple background. It is pretty simple to implement using a linear layout and textviews, and setting their background when onPageSelect is called.
I was looking for a way to shift the background when the viewpager is moving. Like this
For this I have two linearlayouts, one containing the tabs and one the background.I was moving the background using a empty view using a onPageChangeListener(). But I am not able to change the textColor in between the transition states.
How should I do it?

You can use the selector in drawable.Selector look like the below
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/your color" android:state_selected="true"/>
<item android:drawable="#color/your color"/>
</selector
and add selector to the background of_tablayout.
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#color/circular_progress_default_progress"
app:tabMode="fixed"
app:tabMaxWidth="0dp"
app:tabBackground="#drawable/tab_selector_color"
app:tabGravity="fill"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

Related

How to change a specific icon image from Bottom Navigation View

I need to implement a Bottom Navigation View in my android app. The middle icon needs to be an image, the company logo. But when I run the app it appears only a grey filled rounded icon. The images above show what I want and what I'm getting.
What I want:
What I get:
I already tried others questions in this website, but every answer tells to change the iconTintList from XML with a drawable, but the center icon is a vector with more than one color.
When I tried to set null to setIconTintList method, works for the middle icon but the others icons change to original color too.
//This doesn't work to other icons, only for the middle one
mBottomNav.setItemIconTintList(null);
I also tried to get the menu and set the icon tint list only for the middle one, like the code above, but doesn't work too.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBottomNav.getMenu().findItem(R.id.nav_buy).setIconTintList(null);
}
This is the XML implementation:
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/kmv_background"
app:itemIconTint="#drawable/bottom_nav_item_color"
app:itemTextColor="#drawable/bottom_nav_item_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_navigation" />
This is the java implementation:
mBottomNav = findViewById(R.id.bottomNavigationView);
mBottomNav.setOnNavigationItemSelectedListener(this);
Thanks for any help!
I don't think there's a short way. Use this first:
mBottomNav.setItemIconTintList(null);
Then do the designs yourself. Don't forget to separate the buttons as clicked and not clicked.
Example Home Button XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--Clicked-->
<item android:drawable="#drawable/homeclicked" android:state_checked="true" />
<!--Not Clicked-->
<item android:drawable="#drawable/homenotclicked" android:state_checked="false" />
</selector>
And add them to the view:
Example bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/homebuttons"
android:icon="#drawable/homebuttonxml />
<!--Other Buttons...-->
</menu>
And finally, Link view to bottomnavigationview
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:labelVisibilityMode="unlabeled"
app:elevation="0dp"
app:menu="#menu/bottom_navigation">
</com.google.android.material.bottomnavigation.BottomNavigationView>
For a particlar icon, this can be used:
MenuItemCompat.setIconTintMode(binding.bottomNav.menu.getItem(3), null);

How to change color of tab selected in tablayout

How do I change the color of the selected tab? I would like to each tab to have its own color attribute. So tab 1 selected would be Red. Tab 2 selected would be Blue. Tab 3 selected is Yellow. When not selected they go back to the tab original color.
Currently, I am using a selector to change the background of the selected tab. However, that only allows for one color. I would like to have multiple colors
This is the unselected_tab.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/colorPrimaryDark" />
</shape>
This is the selected_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/tabSelectedColor" />
</shape>
This is the selector I am using
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="50"
android:exitFadeDuration="50" >
<item android:drawable="#drawable/tab_selected"
android:state_selected="false" />
<item android:drawable="#drawable/tab_unselected"
android:state_selected="true"/>
</selector>
And I am applying it to the tablayout background
<com.google.android.material.tabs.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:elevation="5dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="#android:color/white"
app:tabMode="scrollable"
app:tabMaxWidth="100dp"
app:tabBackground="#drawable/tab_selector"
/>
Below here are some screen shots of how it looks currently.
Ideally, I would like for each tab to have its own separate color.
How can accomplish this?
EDIT: IDEALLY I WOULD LIKE TO CHANGE EACH TAB COLOR PROGRAMATICALLY
For anyone who is looking for a solution to this. I was able to find one. You can apply the color to the layout of the child of the tabLayout. For some reference code this is what I used a worked really well for me
final LinearLayout tabsContainerLayout = (LinearLayout)expenseTabs.getChildAt(0);
Linear LayouttempLinearLayout = (LinearLayout)tabsContainerLayout.getChildAt(selectedTabPosition);
tempLinearLayout.setBackgroundColor(Color.RED);

How to put pagertitlestrip in the center top

I am trying to update my app and wanted to make the toolbar look like the black player. As of now i am able to set the search and navigation but how to put the pager title strip in the middle as inside viewpager i cannot use gravity i tried the margin and padding not working at all.
this is my xml
<com.antonyt.infiniteviewpager.InfiniteViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<me.alexrs.fontpagertitlestrip.lib.FontPagerTitleStrip
android:id="#+id/titlestrip"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginEnd="50dp"
android:layout_marginStart="50dp"
android:background="#color/material_fragment_top"
app:fontFamily="#font/font"
app:theme="#style/AppTheme.PopupOverlay" />
</com.antonyt.infiniteviewpager.InfiniteViewPager>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:theme="#style/CustomActionBar" />
how i can put the pagertitlestrip in the middle
So I want to put the padding but not able to do it at all
Seems like you want to change StatusBarColor.
try setStatusBarColor(int)and set colour of your toolbar or you can make it transparent
All you need to do is set these properties in your theme:
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
Your activity / container layout you wish to have a transparent status bar needs this property set:
android:fitsSystemWindows="true"
but since you never specified API you're using I refer you to different question, you should find the answer.
How to change the status bar color in android
Android Completely transparent Status Bar?

Changing the color of selected tab [duplicate]

This question already has answers here:
Changing the background color of a Tab in TabLayout (Android design support library) doesn't occupy the entire tab space
(4 answers)
Closed 5 years ago.
I am using tabLayout to use tabs in my app. I want to change the background color & text color of selected tab. I am changing the background color of entire tabLayout by doing this
android:background="#color/colorAccent"
and changing textcolor and selectedtextcolor
app:tabTextColor="#000000"
app:tabSelectedTextColor="#color/colorAccent"
but all i want to do now is to change the background color of that specific tab when it is selected ? How to do that ?
Thanks in advance :)
you can do it like this in your tab layout widget
<android.support.design.widget.TabLayout
app:tabBackground="#drawable/selector"
/>
and define your selector.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/tab_selected_color"
android:state_selected="true"/>
<item
android:drawable="#color/tab_unselected_color"
android:state_selected="false"/>
</selector>
Change the background colour of tab in TabLayout is fairly simple
using the design support library that Android provides. You can simply
change the background of the whole TabLayout using the
app:tabBackground property and you can change the tab indicator colour
using the app:tabIndicatorColor property, but there are better ways if
you want more functionality. A better way to change the tab-layout
colour is using selectors, using selectors you can have different
background for different sates of tab i.e selected, unselected etc.
Please follow the below steps:
1. Create a drawable, tab_selected_background, that will be use as the background for the selected tab
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorPrimary" />
<corners android:radius="4dp" />
</shape>
2. Create a selector, tab_selector that will be used as the background for tab layout:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tab_selected_background" android:state_selected="true"/>
<item android:drawable="#color/tab_background_unselected"/>
</selector>
3. Now finally create the tab layout and use the selector that we've just create as the background of the tabLayout.
<android.support.design.widget.TabLayout
android:id="#+id/subChordTabs"
android:layout_width="match_parent"
android:layout_height="56dp"
app:tabGravity="center"
app:tabMode="scrollable"
app:tabBackground="#drawable/tab_selector"
app:tabIndicatorColor="#color/tabIndicator"
android:padding="8dp"
app:tabIndicatorHeight="2dp"/>
You gotta the result like below,

Elevation issue when coloring BottomNavigationView

I want to implement colored BottomNavigationView with elevation (current design lib version 25.2.0, test device 7.1.2). Some code for start:
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:elevation="8dp"
app:elevation="8dp"
app:menu="#menu/bottom_navigation" />
elevation (shadow) is not drawn, because there is no background. and there is known issue that this background must be white... ok, so I'm adding this line
android:background="#android:color/white"
but I wanted to set some color for background and white icons and text... with below lines
app:itemIconTint="#color/white"
app:itemTextColor="#color/white"
whole bar is solid white, including content, obviusly... so if background must be white also then lets change every item background with
app:itemBackground="#drawable/bottom_navigation_item_background"
and for drawable/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/mycolor_dark"/>
<item android:drawable="#color/mycolor"/>
</selector>
for drawable-v21/ (ripple)
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/white">
<item android:drawable="#color/mycolor"/>
</ripple>
and now we have colored items and... no elevation again! and it will work when mycolor will be white (elevation shown/drawn)... so when I want elevation for BottomNavigationView both background and itemBackground must be white...
interesting part (proof of issue?) - lets check elevation with only this selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/white"/>
<item android:drawable="#color/mycolor"/>
</selector>
bar is drawn without elevation at all, but when I press any menu item then its background change to white and... there is elevation above this item, ONLY this item!
when I remove <item android:drawable="#color/mycolor"/> line from drawable-v21/ XML file (or set mycolor to white), then elevation is shown. ripple color have no impact, it might be white or any other (working).
the question is: how to style BottomNavigationView with custom color for background keeping elevation working?
I used app:theme="#style/AppTheme.PopupOverlay" in order to use my application colors. They are automatically set to them.
For example, if your primary color is green, icons and text will be displayed in green.
You need to keep white background and elevation...

Categories

Resources