Open top menu

In this tutorial explain how to set Actionbar background color and title color in android programmatically .

first ,you have create xml class inside drawable folder...




actionbar_background.xml.

in this class set three color and set on actionbar background.

..................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <gradient
        android:startColor="#000000"
        android:centerColor="#000000"
        android:endColor="#000000"
        android:angle="90"/>
    <corners
        android:radius="0dp"/>
</shape>


MainActivity.java

in this class set a actionbar_background.xml inside onCreate().
................................................................................................................................................



import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;


public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tracking);

 // get Actionbar and set the tilte name and color

getActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>Tracking</font>"));

              //   set actionbar background color xml .
Drawable drawable = getResources().getDrawable(R.drawable.actionbar_background);
getActionBar().setBackgroundDrawable(drawable);
}

}

try this code


 ActionBar actionbar = getActionBar();
 actionbar.setBackgroundDrawable(new ColorDrawable("COLOR"));

second ,you have implement style theme

styles.xml
...................................................................................................................................................
<resources>

     
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
          <item name="android:actionBarStyle">@style/MyActionBar</item>
          <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    </style>
      
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@android:color/black</item>
    </style>
    
    <style name="MyTheme.ActionBar.TitleTextStyle"                                                                                    parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@android:color/white</item>
    </style>
     
     
</resources>






0 comments