Toolbar with Gradient Background set title background transparent - android

I defined a Gradient Drawable:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:endColor="#2F3F59"
android:startColor="#4078A7"
android:type="linear"
android:angle="90" />
</shape>
And I set it on my Toolbar:
<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">#color/textcolorsecundary</item>
<item name="actionMenuTextColor">#color/textcolorsecundary</item>
<item name="android:textColorSecondary">#color/textcolorsecundary</item>
<item name="android:background">#drawable/custom_background_blue</item>
</style>
This is working! BUT:
This is what happens with the title. It gets the same gradient. This looks really ugly so I have to change this. How can I set the background of that info text to be transparent?

You can also solve this by setting the background of the toolbar to your drawable.
mToolbar().setDrawable(R.drawable.your_gradient)

link - Set transparent background of an imageview on Android
use this color in your color xml file and then give this color to your startcolor and end color

The solution is to disable the title of the toolbar:
this.getSupportActionBar().setDisplayShowTitleEnabled(false);
and to add a custom TextView:
<?xml version="1.0" encoding="utf-8"?>
<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="?android:attr/actionBarSize"
android:elevation="2dp"
android:focusable="false"
android:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
app:theme="#style/AppTheme.Toolbar" >
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#null"
android:text="Toolbar Title"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>

Changing the background color to transparent in the style section should fix the issue
<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:background">#00000000</item>
</style>
The question How to make title background transparent on android app had similar issue above solution did the trick. The android app was for Renesas RX130 and Texas Instrument CC2650 BLE solution. Bellow is solution after the fix.

Related

ButtomNavigationView doesn't show ripple effect in Items Background

UPDATE:
The other question uses a white background and since it uses white background there is no ripple on the BottomNavigationBar (because the ripple itself is white). But in my case the ButtomNavigationBar is blue, and it reflects the ripple. In fact all of the colors except the white(that the other question uses) reflect the incomplete ripple.
ORIGINAL:
I am using ButtomNavigationView, and the problem is that it doesn't show ripple effect in the background of it's Items.
Or it doesn't care about app:itemBackground it just uses android:background value without any ripple effect.
I've tried using a ripple instead of selector in drawable-21 and changing the app:itemBackground color values but it is not working.
ButtomNavigationView:
<android.support.design.widget.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="56dp"
android:theme="#style/BottomNavigationStyle"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
ButtomNavigationStyle:
<style name="BottomNavigationStyle">
<item name="itemBackground">#drawable/navigation_bar_item_bg</item>
<item name="itemIconTint">?attr/bottom_nav_colors</item>
<item name="itemTextColor">?attr/bottom_nav_colors</item>
<item name="android:background">?attr/colorPrimary</item>
<item name="android:fontFamily">#font/iransans_mobile</item>
</style>
bottom_nav_menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_mylibrary"
android:icon="#drawable/ic_library_books_black_24dp"
android:title="#string/title_mylibrary" />
<item
android:id="#+id/navigation_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/title_search" />
.
.
.
</menu>
navigation_bar_item_bg:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/black_color"
android:state_checked="true"/>
<item android:drawable="#drawable/black_color"/>
</selector>
ScreenShot:
https://firebasestorage.googleapis.com/v0/b/library-1696f.appspot.com/o/Screenshot%20from%202019-06-17%2005-39-23.png?alt=media&token=20efbe56-3234-47ab-b6cc-898124b77762
I've searched and I've found that it is a library bug that is not fixed yet.
But I've found a solution for this.
1) Since the android:background and android:itemBackground don't work correctly delete both of them from BottomNavigationView.
2) Create a new FrameLayout and put your BottomNavigationView inside FrameLayout.
3) change these properties of the FrameLayout:
android:layout_width="match_parent"
android:layout_height="wrap_content"
4)Finally Add your desired color for ButtomNavigationView into FrameLayout as android:background.
Example:
<FrameLayout
android:id="#+id/buttomnavigation_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue"><!--Background color for BNV-->
<android.support.design.widget.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="56dp"
app:itemIconTint="#color/bottom_navigation_colors"
app:itemTextColor="#color/bottom_navigation_colors"
app:labelVisibilityMode="labeled"
app:menu="#menu/bottom_nav_menu"/>
</FrameLayout>
bottom_navigation_colors.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="#FFFFFF" />
<item
android:state_checked="false"
android:color="#C7FFFFFF" />
</selector>

No toolbar elevation shadow on Android 9

I'm struggling to make toolbar shadow appear on Android 28+. I tried multiple solutions suggested, but none worked. Following styles work well for all devices below 28:
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
style="#style/Widget.App.Toolbar"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:layout_height="?attr/actionBarSize"
/>
styles.xml
<style name="Widget.App.Toolbar.V1" parent="Widget.AppCompat.Toolbar">
<item name="android:layout_height">?attr/actionBarSize</item>
<item name="android:layout_width">match_parent</item>
<item name="android:background">?attr/colorPrimary</item>
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="android:theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
styles.xml (v21)
<style name="Widget.App.Toolbar" parent="Widget.App.Toolbar.V1">
<item name="android:elevation">4dp</item>
</style>
I also tried to set elevation programmatically, using ViewCompat.setElevation with no success.
Does anyone has an idea how to make it work?
UPD:
I know you can create custom shadow drawable, but I would like to avoid this solution if possible. Just can't understand why such basic functionality requires additional drawable and views in layout. Also, I wonder why it works on previous Android versions. Didn't found any proofs that this behavior has changed.
I ended up setting my own drop shadow for the toolbar, thought it might helpful for anyone looking for it:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_alizarin"
android:titleTextAppearance="#color/White"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- **** Place Your Content Here **** -->
<View android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#drawable/toolbar_dropshadow"/>
</FrameLayout>
</LinearLayout>
#drawable/toolbar_dropshadow:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#android:color/transparent"
android:endColor="#88333333"
android:angle="90"/>
</shape>
#color/color_alizarin
<color name="color_alizarin">#e74c3c</color>
I don't know why it doesn't work for many of the people, adding:
android:elevation="5dp"
Directly to the toolbar works fine for me.

Gradient background on material themed toolbar

I have a photo preview activity that has an overlay actionbar. I don't want this view to have a full opaque color because it would obstruct the image when zoomed. I created a gradient to set as the background:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="linear"
android:startColor="#color/theme_main_color_40pct"
android:endColor="#color/theme_main_color_0pct"
android:angle="270"/>
</shape>
I assign this background to my toolbar via the following theme:
<style name="my_toolbar" >
<item name="android:background">#drawable/ab_gradient_purple</item>
<item name="android:minHeight">?attr/actionBarSize</item>
<item name="android:textColorPrimary">?attr/colorAccent</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
In my layout:
<android.support.v7.widget.Toolbar
android:id="#id/toolbar"
app:theme="#style/my_toolbar"
android:elevation="5dp"
android:layout_gravity="top"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
When I open this activity, it looks like the toolbar is taking that background and applies it to all subviews, resulting in the picture below. How do I prevent this overlay?
I've tried playing with backgroundTint and backgroundTintMode, but it seems like they're not available for pre-lollipop.
It seems that applying the background through the style applies it too all nested items. I set the style background to #android:color/transparent and in my layout:
<android.support.v7.widget.Toolbar
android:id="#id/toolbar"
app:theme="#style/my_toolbar"
android:background="#drawable/ab_gradient_purple"
android:elevation="5dp"
android:layout_gravity="top"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
The result is exactly what I was looking for:

How to create ripple effect in simple layout

How can I have a ripple effect in a simple linear/relative layout when touching the layout?
I've tried setting the background of a layout to something like:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight" >
<item android:drawable="#android:color/white"/>
</ripple>
However I'm only seeing a plain white background and no ripple effect when touching the layout. What am I doing wrong?
Edit:
For reference, here is my layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#drawable/test"
android:clickable="true">
</RelativeLayout>
Set these properties on your layout (if your layout has the default white/light background):
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
You don't need a custom drawable in this case.
However, if your layout has a black/dark background, you'd have to create your own ripple drawable like this one:
<?xml version="1.0" encoding="utf-8"?>
<!-- An white rectangle ripple. -->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/white">
<item
android:id="#android:id/mask"
android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
</shape>
</item>
</ripple>
And then set this ripple drawable as your android:background property.
You can see many examples of these types of ripples in AOSP: in here
I'm adding this in addition to Igor Ganapolsky's answer in case anyone wants to have a layout with different color and also have a ripple effect. You simply create a ripple effect in drawables like this:
ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/rippleColor"
tools:targetApi="lollipop"> <!-- ripple effect color -->
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<solid android:color="#color/backgroundColor" /> <!-- background color -->
</shape>
</item>
</ripple>
then give it as your layout's background or any button as Igor Ganapolsky mentioned in his answer.
android:clickable="true"
android:background="#drawable/ripple"
You have to alter following code snippet in any widget(TextView/Button) and you can implement Ripple Effect:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
And you are good to go.
It turns out this is working as intended. The standard Material theme colorControlHighlight value is #40ffffff. So on a white background, this will not show up. Changing the highlight color to something else works, and/or changing the background color of the object.
Thanks all (especially Alanv for pointing me in the right direction).
This is an example for clicking on a sample layout with ripple effect :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical">
Set android:clickable="true" on your layout.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#cfd8dc"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#d1c4e9" />
</shape>
</item>
</ripple>
Use this ripple.xml in Drawable folder, then assign it as View's
android:background="#drawable/ripple"
android:clickable="true"
Simply add default ripple effect with android:background="?selectableItemBackground"
Like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?selectableItemBackground"
android:clickable="true"
>
...
</LinearLayout>
Put this into your layout:
android:clickable="true"
android:background="?attr/selectableItemBackground"
Note: There is a bug in API documentation. It will work only on API >= 23
For all API levels use this Solution
I tries all those answers and nothing worked to me, so, I solved creating the following style:
<style name="Button.Borderless" parent="Base.Widget.AppCompat.Button.Borderless">
<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>
<item name="android:layout_marginLeft">-6dp</item>
<item name="android:layout_marginRight">-6dp</item>
<item name="android:paddingTop">0dp</item>
<item name="android:paddingBottom">0dp</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item>
</style>
And then, I applied to my linear layout:
<LinearLayout
android:id="#+id/ll_my_ripple"
style="#style/Button.Borderless">
</LinearLayout>
To make a circlar effect use the below code:
android:clickable="true"
android:background="?attr/selectableItemBackgroundBorderless"

Android menu item define style for enabled/disabled status

Is it possible to define different style for menu item depending on its enabled/disabled status?
For example, I want the text color of menu item to be gray in disabled mode and white in enabled mode.
I didn't have success with changing color dinamycally just as many people didn't on stackoverflow.
It really depends on the item you wish to customize.
Basically you can create a custom color that changes depending on its state:
colors/custom_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FF0000" android:state_enabled="false" />
<item android:color="#CCCCCC"/>
</selector>
Then set it to your menu item like this:
menu.findItem(R.id.action_search).getActionView().
setBackgroundResource(R.colors/custom_color.xml);
Or perhaps in the xml if available:
android:textColor="#color/custom_color"
You can use a drawable as the text colour, and in drawable you can use selector to select the colour according to enabled status. Using following drawable definition as colour will make your disabled menu items grey and the rest black.
In e.g. res/drawable/default_text_colour.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#android:color/darker_gray"/>
<item android:color="#android:color/white"/>
</selector>
Then, using the drawable:
<item name="android:textColor">#drawable/default_text_colour</item>
For some one else may be needed: use ToolBar instead of ActionBar, add TextView and set style as MenuItem:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_explorer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="#style/CustomActionBar.Theme"
android:background="#mipmap/bg_naviber">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/title_activity_explorer2"
android:layout_gravity="start"
android:padding="#dimen/dimen_8"
style="#style/CustomActionBar.Tittle"
/>
<TextView
android:id="#+id/toolbar_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="#string/action_delete"
android:textColor="#drawable/selector_text_view"
android:padding="#dimen/dimen_16"
style="#style/CustomActionBar.Menu"
/>
<TextView
android:id="#+id/toolbar_do_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="#string/action_do_delete"
android:textColor="#drawable/selector_text_view"
android:padding="#dimen/dimen_16"
style="#style/CustomActionBar.Menu"
android:visibility="gone"
/>
</android.support.v7.widget.Toolbar>
set textColor by selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- order is important -->
<item android:state_enabled="false" android:color="#color/white_disabled"/>
<item android:state_pressed="true" android:color="#color/white"/>
<item android:color="#color/white"/>
</selector>
Using:
<style name="Theme.WordsTrainer" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
I've added the line:
<item name="android:textColor">?android:attr/textColorPrimary</item>
to themes.xml and night\themes.xml, and this works fine with day and nigth mode both.
I've spend a day diggin it, and finally found the answer in obvious place:
https://developer.android.com/guide/topics/ui/look-and-feel/darktheme

Categories

Resources