- How to set background color for android layout pragmatically using java and through xml
- How to set Background Color to Android Layout XML file!
- How to Change Background Color of Action Bar in Android?
- Kotlin Android – Change Background Color of Action Bar
- Change Background of Action Bar via themes.xml
- Step 1 – Open themes.xml
- Step 2 – Change colors
- Step 3 – Run
- Change Background of Action Bar via Kotlin Activity File
- Conclusion
How to set background color for android layout pragmatically using java and through xml
Every Android Activity class file has an associated layout.xml file to design the view. Every layout file has a parent View i.e. RelativeLayout, LinearLayout, TableLayout, TableRow, GridLayout e.t.c. that holds the subviews like Buttons, TextView, EditText, ImageView e.t.c together.
Let’s see how we can set background color’s to these Layouts with various options that we have,
How to set Background Color to Android Layout XML file!
android:background=»» is the attribute used to set background for any Layout file.
You can directly specify the value as HEX color code as we do for CSS files in HTML.
Example 1 : android:background=»#FFFFCC»
You can also add transparency to the color by adding 2 more hex numbers after the # (hash) symbol.
Example 2 : android:background=»#FFFFFFCC»
Example 3 : android:background=»#00FFFFCC»
FF => Completely Opaque and 00 => Completely transparent.
You can also assign a color from color.xml resource file using @color/color
Example 4 : android:background=»@color/lime_yellow»
Programmatically using Java code.
There are situations when you may want to change the background color of a layout using java code in your Activity.java file, then you can do it by using setBackgroundColor() method on your layout.
To your Parent View Layout add an attribute @id/id_name and map it to a variable in your java file.
Example 1: currentLayout.setBackgroundColor(Color.RED);
Options for color available in Color class :
You can set rgb color code using method : Color.rgb(int red,int green,int blue);
Example 2 : currentLayout.setBackgroundColor(Color.rgb(200, 200, 200));
We can add Alpha to color as we do use XML attribute in java using the function : argb(int alpha, int red, int green, int blue);
Источник
How to Change Background Color of Action Bar in Android?
Kotlin Android – Change Background Color of Action Bar
To change background color of Action Bar in Kotlin Android, set the colorPrimary in themes.xml, with a required color. We can also dynamically change the background color of Action Bar programmatically by setting background drawable for support action bar with the required color drawable.
In this tutorial, we will go through the two processes: via themes.xml and via Kotlin file, to change the background color of Action Bar in Android Application.
Change Background of Action Bar via themes.xml
The following is a step by step process to change the background color of Action Bar in Android Application.
Step 1 – Open themes.xml
1. Click on Resource Manager present on the left side of the Android Studio window.
2. In this Resource Manager window, click on Style tab.
3. Double click on the theme for our application. In this case, Theme.MyApplication.
Theme items present for this theme will be displayed as shown in the following screenshot.
Double click on the default theme.
The contents of this style file, themes.xml, will be displayed in the editor.
Step 2 – Change colors
Now, we have to change color value for the item with the attribute colorPrimary.
Let us assign black color to this item.
Step 3 – Run
Now, let us run this Android Application and observe the background color of Action Bar.
The background color of our Android Application is changed to the set color.
Change Background of Action Bar via Kotlin Activity File
To change the background color of Action Bar programmatically or dynamically, set background drawable for support action bar with the required Color drawable.
In the following example, we set the background color for Action Bar with the color represented by hex value “#146775” in RGB format.
MainActivity.kt
Run the Android Application, and the background color of Action Bar will be set to the color «#146775» , as shown in the following screenshot.
Conclusion
In this Kotlin Android Tutorial, we learned how to change the background color of Floating Action Button (FAB) widget via layout file or programmatically in Kotlin Activity file, with examples.
Источник