Problem:
Update the bottomNavBar icon's colour 'programmatically'. There are several ways to update if thru XML (out of scope here as the requirement is to update on the fly).
tried code:
bottomNavigationView.itemIconTintList = getBackgroundColorStates()
// colour state list based on states
private fun getBackgroundColorStates() = ColorStateList(
arrayOf(
intArrayOf(android.R.attr.state_selected),intArrayOf(-android.R.attr.state_selected)),
intArrayOf(ContextCompat.getColor(this, selectedColor), ContextCompat.getColor(this, disabledColour)
)
)
results as below:
whereas the expectation is to change the colour of the icons, not to fill (as below):
Any thoughts on how to update the colour of bottomNavBar menu icon? thanks.
Related
I made a full screen dialog using Jetpack compose but every time i try to change status bar and navigation bar color, i get a strong gray overlay that makes every color i chose, almost dark grey.
I've already tried to set custom style in styles.xml file and used the systemUi controller to change colors with the following code:
val systemUiController = rememberSystemUiController().apply {
setNavigationBarColor(navigationBarColor)
setStatusBarColor(statusBarColor)
}
Nothing really worked. What can i do?
I had the similar issue, and have find the solution:
By default statusBar has a layer with 30% opaque black color even if color has been set to transparent.
private val BlackScrim = Color(0f, 0f, 0f, 0.3f) // 30% opaque black
private val BlackScrimmed: (Color) -> Color = { original ->
BlackScrim.compositeOver(original)
}
It the setNavigationBarColor you need to change transformColorForLightContent.
Set Transperent color or another if needed instead black.
private val noScrimmed: (Color) -> Color = { original ->
Color.Transperent.compositeOver(original)
}
setNavigationBarColor( transformColorForLightContent = noScrimmed)
How can a color resource be used to change the background colour for a MD3 top app bar in Jetpack Compose?
Understandably, a colors property is available but it's not clear what to use for the above.
Color.kt
val MyColor = Color(0,5,5,255)
MainActivity.kt
MediumTopAppBar(title = {Text(text = "")})
The colors parameter is supposed to be used like so.
There's usually a Default Companion for these things, which provides a convenience function for modifying colors. For example, the default companion for Top bar colors is just TopAppBarDefaults.
Since you are referring to medium bars, we'll use the following
TopAppBarDefaults.mediumTopAppBarColors(
containerColor = Color(...) //Add your own color here, just to clarify.
)
These functions usually provide a containerColor and a contentColor parameter by default.
Solving your problems is... Super-easy, barely an inconvenience.
I have a CardView which is getting colored after a button click ("reveal right answer" functionality). The card needs to get back to it's original color for the next question. I am using the default Android color, but I couldn't find the value I need to reset to (R.attr.cardBackgroundColor turns my card blue...), so I used tint like so:
#BindingAdapter("isRight", "showResult")
fun CardView.setCardTint(
isRight: Boolean,
showResult: Boolean,
) {
background.setTintList(null)
if (showResult) {
val colorRes = if (isRight) R.color.result_right
else R.color.result_wrong
background.setTint(ContextCompat.getColor(context, colorRes))
}
}
This works great, but now I need to change the colors to gradients. For that I need to go back to the original solution and change the background itself (to the gradient drawables) - and to my original problem: How to reset the CardView background to it's Android default value?
I have a TabLayout which has icons. The idea is to change the colors runtime.
I have and xml drawable file with states: state_pressed, state_selected and default with the same white picture so I can put color later.
I take the drawables for different states:
Drawable[] drawables = stateListDrawable.getConstantState();
and the for every drawable state I put color from another array:
drawables[i].setColorFilter(colors[i], PorterDuff.Mode.MULTIPLY);
The issue is that the color is visible in the beginning, but when I start to click on the icons all the icons becomes white again and I lose the tint.
Everything is working as expected on lollipop and above.
Use the tint method from the v4 Support library
drawables[i] = DrawableCompat.wrap(drawables[i])
DrawableCompat.setTint(drawables[i], colors[i])
I have found my solution, which does not look clean at all, but at least it is working :)
I have created CustomStateListDrawable which extends from StateListDrawable and added the drawables for the different states. Then I have overridden all the methods in the class to see which ones are called and tried to change the colors there. The called late enough(my changes will not be overridden after I make them) was getState(). I have created also a ColorStateList object to hold my colors so the code will look like this:
private ColorStateList colorStateList;
public int[] getState() {
if (colorStateList != null) {
// Resolve the color for the current state
int color = colorStateList.getColorForState(super.getState(), 0);
// Get the current drawable and changed its color.
if (getCurrent() != null) {
getCurrent().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
}
return super.getState();
}
Basically every time when there is a change in the state I get the current drawable and change its color.
I need to disable a tint color for some icons in NavigationView because their color define category type. How can I do it?
Below picture shows my problem:
navview.setItemIconTintList(null);
Good luck!
If you want change color of icon on seletion the below is the possible answer:
Change Navigation View Item Color Dynamicly Android
Otherwise you can set
navview.setItemIconTintList(null);
this will give the original colors of icons. and you can use colored and grey icons as per your requirements.
For those who are using Kotlin, That's how it is done
val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottomnavigationhome)
// * THIS ONE
bottomNavigationView.itemIconTintList = null
In case this is still relevant for somebody, we found a solution for a similar issue recently.
Although it is not possible (at least on API levels < 26) to set a custom tint list on individual items, you can set the tint mode individually. This worked for us:
val itemsWithoutTint: List<Int> = listOf(12345)
for (i in 0 until getMenu().size()) {
val item = getMenu().getItem(i)
if (item.itemId in itemsWithoutTint) {
MenuItemCompat.setIconTintMode(item, PorterDuff.Mode.DST)
}
}
By setting the TintMode to DST (https://developer.android.com/reference/android/graphics/PorterDuff.Mode), the source (in this case the tint color) is ignored and the destination (the icon to be tinted) is left untouched.
Menu menuNav = navigationView.getMenu();
MenuItem menuItem = menuNav.findItem(R.id.nav_subjects);
// Disable a tint color
menuItem.setChecked(false);
I hope it answers your question.