Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBarSherlock Top Bar

enter image description here

I want to have an Action Bar like foursquare. What I want is tabs such as Friends, Explore and Me. Also, above the tabs I want to have a custom layout that includes some buttons such as foursquare logo, Refresh and Check-In in foursquare. I created tabs but I could not change the layout above tabs in ActionBarSherlock. How can I solve this problem?

like image 955
BCK Avatar asked Jul 10 '12 11:07

BCK


People also ask

What happened to actionbarsherlock?

ActionBarSherlock is deprecated. No more development will be taking place. For an up-to-date action bar backport use AppCompat. Thanks for all your support! ActionBarSherlock is an standalone library designed to facilitate the use of the action bar design pattern across all versions of Android through a single API.

What is Actionbar in Android?

Android ActionBar was launched by Google in 2013 with the release of Android 3.0 (API 11). Before that, the name of this top most visual element was AppBar. AppBar contains only the name of the application or current activity.

What is the best way to backport actionbarsherlock?

For an up-to-date action bar backport use AppCompat. Thanks for all your support! ActionBarSherlock is an standalone library designed to facilitate the use of the action bar design pattern across all versions of Android through a single API. The library will automatically use the native ActionBar implementation on Android 4.0 or later.

How to get application title from Actionbar in Android?

As mentioned earlier, every android app contains an ActionBar by default. This pre-included ActionBar display title for the current activity that is managed by the AncdroidManifest.xml file. The string value of the application’s title is provided by @string/app_name resource present under the application nodes.


1 Answers

To achieve a Foursquare look you do not need to be thinking of creating a layout above the tabs. When using Actionbar Sherlock you only need to worry about:

  1. Making a background for the top bar
  2. Making a logo for the top bar
  3. Adding items in a menu.xml file that ActionbarSherlock will use to populate the top section with Buttons (as long as a style using Actionbar Sherlock style is attached to thw activity).

So, for 1. and 2. it's all about using styles.xml file (should reside in the values folder in the res folder) like so:

<style name="Theme.Example" parent="Theme.Sherlock">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="absForceOverflow">true</item>       
</style>

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar.Solid">
    <item name="background">@drawable/actionbar_background</item> <-- the background for top bar
    <item name="icon">@drawable/actionbar_logo</item> <-- the logo that goes top left in the top bar
    <item name="backgroundSplit">@drawable/bg_striped_split</item> <-- the image used between the menu items
</style>

For 3. all you need to do is create menu items under menu.xml (should reside in the menu folder (if not there, create one in the res folder)):

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

<item android:id="@+id/menu_prefs"
      android:icon="@drawable/settings_icon"
      android:title="Preferences"
      android:showAsAction="ifRoom" /> 
</menu>

The last thing you have to do to see the menu items is use these functions in the activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

public boolean onOptionsItemSelected (MenuItem item) {

    Intent intent;

    switch (item.getItemId())
    {   
    case R.id.menu_prefs:

        // Launch Preference Activity
        intent = new Intent(getBaseContext(), Preferences.class);           
        startActivity(intent);
        break;
    }

    return false;
}
like image 198
Richard Lewin Avatar answered Oct 26 '22 15:10

Richard Lewin