How to change the color of Action Bar in an Android App?
In this article, you will learn how to change the colour of the Action Bar in an Android App.
There are two ways to change color.
- By changing styles.xml file:
- Just go to res/values/styles.xml file
- edit the xml file to change the color of action bar.
- Code for styles.xml is given below
styles.xml
<
resources
>
<!-- Base application theme. -->
<
style
name
=
"AppTheme"
parent
=
"Theme.AppCompat.Light.DarkActionBar"
>
<!-- Customize your theme here. -->
<!-- This code is for changing the color of the bar. -->
<!-- Type your colour code which you want to set in colorPrimary item -->
<
item
name
=
"colorPrimary"
>#0F9D58</
item
>
<
item
name
=
"colorPrimaryDark"
>@color/colorPrimaryDark</
item
>
<
item
name
=
"colorAccent"
>@color/colorAccent</
item
>
</
style
>
<
style
name
=
"AppTheme.NoActionBar"
>
<
item
name
=
"windowActionBar"
>false</
item
>
<
item
name
=
"windowNoTitle"
>true</
item
>
</
style
>
<!-- Define other styles to fix theme -->
<
style
name
=
"AppTheme.AppBarOverlay"
parent
=
"ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<
style
name
=
"AppTheme.PopupOverlay"
parent
=
"ThemeOverlay.AppCompat.Light"
/>
</
resources
>
activity_main.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<!--Relative Layout-->
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:id
=
"@+id/relativelayout"
>
<!--Text View-->
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:id
=
"@+id/textview"
android:textColor
=
"#0F9D58"
android:textSize
=
"32dp"
android:layout_centerInParent
=
"true"
/>
</
RelativeLayout
>
MainActivity.java
package
com.geeksforgeeks.changecolor;
import
android.widget.TextView;
import
android.support.v7.app.AppCompatActivity;
public
class
MainActivity
extends
AppCompatActivity {
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Define text View
TextView t = findViewById(R.id.textview);
t.setText(
"Geeks for Geeks"
);
}
}
- Through Java file by defining ActionBar object:
- Define object for ActionBar and colorDrawable class
- set color using setBackgroundDrawable function with colorDrawable object as its parameter.
- Here is complete code for MainActivity.java
MainActivity.java
package
com.geeksforgeeks.changecolor;
import
android.support.v7.app.ActionBar;
import
android.graphics.Color;
import
android.graphics.drawable.ColorDrawable;
import
android.support.v7.app.AppCompatActivity;
public
class
MainActivity
extends
AppCompatActivity {
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Define ActionBar object
ActionBar actionBar;
actionBar = getSupportActionBar();
// Define ColorDrawable object and parse color
// using parseColor method
// with color hash code as its parameter
ColorDrawable colorDrawable
=
new
ColorDrawable(Color.parseColor(
"#0F9D58"
));
// Set BackgroundDrawable
actionBar.setBackgroundDrawable(colorDrawable);
}
}
activity_main.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<!--Relative Layout-->
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:id
=
"@+id/relativelayout"
>
<!--Text View-->
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textColor
=
"#0F9D58"
android:textSize
=
"30dp"
android:text
=
"Geeks for Geeks"
android:layout_centerInParent
=
"true"
/>
</
RelativeLayout
>
Output:
- Default color of action Bar:
- In Main Activity color of Action Bar is changed to hash code defined in above code.
Please Login to comment...