Image Background
We can also use images as the background instead of colors. For that again we will be using the background attribute in activity_main.xml. The only thing we need to keep in our mind is that the image’s dimensions should be of the same size as that of the toolbar because the background attribute crops to fit the image in the space. See the below steps:
- Add the image to the drawable folder in resources.
- Set the image in the drawable attribute of the toolbar in the activity_main.xml file.
- Make sure the image and the toolbar have the same dimensions.
Below is the code for the activity_main.xml file:
XML
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < Toolbar android:layout_width = "match_parent" android:layout_height = "wrap_content" android:background = "@drawable/image" /> </ RelativeLayout > |
Output UI:
How to Add Custom Toolbar Background in Android?
A toolbar is basically a form action bar that contains many interactive items. A toolbar supports a more focused feature than an action bar. The toolbar was added in Android Lollipop (API 21) and is the successor of the ActionBar. The toolbar is a ViewGroup that can be put anyplace in the XML layouts. Toolbar’s looks and behavior could be more efficiently customized than the ActionBar. Toolbars are more flexible than ActionBar. One can simply change its color, size, and position. We can also add labels, logos, navigation icons, and other views to it. In this article, we will see how we can customize the toolbar background using various methods. We will be seeing the following methods:
- Solid Color Background
- Custom Gradient Background
- Image Background
Note: Before adding a custom Toolbar in your app remove the default Actionbar in your android app by following this link.