Search code examples
androidandroid-actionbarandroid-appcompatandroid-styles

Styling the actionbar in android


i'am following the documentation on [developers.android][1] site to customize my action bar.

the problem is that the action bar doesn't accept the style i made. this is the file 14/style (i'am testing on 4.3 android version device):

<resources>

    <!-- the theme applied to the application or activity -->
    <style name="AppTheme"
        parent="Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/actionbar_color</item>
        <item name="android:icon">@drawable/ic_launcher</item>
    </style>

</resources> 

and this is a part of my manifest :

<application

        android:name="com.fakher.actualitefoot.controller.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

what must i do ? note : i'm using the support library, the application support api-9 [1]: https://developer.android.com/training/basics/actionbar/styling.html#AndroidThemes


Solution

  • I designed my Action Bar like the below: I created an custom_action_bar.xml

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#526e83"
        android:descendantFocusability="blocksDescendants">
    
        <ImageView
            android:id="@+id/ContactImageId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo"
            android:paddingLeft="100dp"
            />
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="10dp">
    
            <TextView
                android:id="@+id/CTS"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:textColor="#ffffff"
                android:text="CTS"
                android:textStyle="bold"/>
    
            <TextView
                android:id="@+id/UserName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10dp"
                android:textColor="#ffffff"
                android:text="Welcome: HKH"/>
    
        </LinearLayout>
    
    </LinearLayout>
    

    then in onCreate in my activity i called this method:

    public void customActionBar() {
    
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
        actionBar.setIcon(android.R.color.transparent);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#526e83")));
        View cView = getLayoutInflater().inflate(R.layout.custom_action_bar, null);
        actionBar.setCustomView(cView);
    }
    

    I hope this may help you in finding what you want