2

How can I create the ActionBar with NavigationDrawer and CustomView. Like this: enter image description here

any ideas, suggestion and solution, please

Amon Olimov
  • 657
  • 1
  • 8
  • 18

1 Answers1

3

The Following code shows how to change color and text, subtext color of ActionBar. Put this file in values/ folder and name it as theme.xml

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

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/actionBarBG</item>
    <item name="android:backgroundSplit">@color/actionBarBG</item>
    <item name="android:subtitleTextStyle">@style/Theme.TitleTextStyle</item>
    <!-- Support library compatibility -->
    <item name="background">@color/actionBarBG</item>
</style>

<!-- STYLE TO CHAGE THE TITLE COLOR OF ACTION BAR -->
<style name="Theme.TitleTextStyle" parent="@android:style/Widget.TextView">
    <item name="android:textColor">#BDD6E0</item>
</style>

In you manifest apply to the activity as below in activity tag :

android:theme="@style/CustomActionBarTheme" 

For ActionItems in ActionBar you can use following code : eg. action_items.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_home"
        android:icon="@drawable/action_home"
        android:showAsAction="ifRoom"
        android:title="@string/home"
        yourapp:showAsAction="ifRoom"/>

    <item
        android:id="@+id/action_logout"
        android:icon="@drawable/action_logout"
        android:showAsAction="ifRoom"
        yourapp:showAsAction="ifRoom"
        android:title="@string/logout"/>

</menu>

Put this XML file in /Menu folder.

Then in activity:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_items, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Take appropriate action for each action item click
        switch (item.getItemId()) {
        case R.id.action_home:
            Intent intent = new Intent(this, CustomerMenuActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            return true;
        case R.id.action_logout:
            // location found
            Intent intent2 = new Intent(this, LoginActivity.class);
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent2);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
Dhaval
  • 1,597
  • 11
  • 15
  • 1
    Yes. I use it in minimum API level 8 – Dhaval Nov 16 '13 at 13:25
  • 1
    For NavigationDrawer check this : http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ – Dhaval Nov 16 '13 at 13:27
  • Sorry, which line of code changes home button background image(color)? – Amon Olimov Nov 16 '13 at 13:28
  • 1
    I'll edit my answer and put some code. ALso check that link i gave you. It has everything – Dhaval Nov 16 '13 at 13:28
  • 1
    For lower API support utilize SherlockActionBar or AppCompat. This Blog has all the examples : http://wptrafficanalyzer.in/blog/ – Dhaval Nov 16 '13 at 13:34
  • 1
    You're welcome. I had wasted a week to achieve this. – Dhaval Nov 16 '13 at 16:49
  • What do you think, which library is the best ActionBarSherlock or v4,v7, android libraries? Can I make application without ActionBarSherlock excellent or ActionBarSherlock is comfortable? – Amon Olimov Nov 19 '13 at 14:39
  • 1
    Haven't used ActionBarSherlock, but people seem to recommend it more. ActionBarSherlock is ragarded as easy to use with more help available online. Nothing wrong with either of the choices, it's just a matter of personal preference & liking. Refer this : http://stackoverflow.com/questions/7844517/difference-between-actionbarsherlock-and-actionbar-compatibility – Dhaval Nov 20 '13 at 05:10