6

I use the actionbarsherklock library with custom action bar look like this:

enter image description here

My custom implement:

  ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    // Do any other config to the action bar
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // set custom view
    View actionBarView = getLayoutInflater().inflate(
            R.layout.action_bar_default, null);

    View btnMenuLeft= actionBarView.findViewById(R.id.btnMenuLeft);
    btnMenuLeft.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            toggle();
        }
    });

    View btnMenuShare= actionBarView.findViewById(R.id.btnMenuShare);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    actionBar.setCustomView(actionBarView, params);

    // Hide the home icon
    actionBar.setIcon(android.R.color.transparent);
    actionBar.setLogo(android.R.color.transparent);

And here is the custom layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nav_bar_bg"
android:gravity="center"
android:orientation="horizontal" >

<!-- menu button -->
    <ImageButton
        android:id="@+id/btnMenuLeft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/list_btn"
        android:clickable="false"
        android:duplicateParentState="true"
        android:focusable="false" />

    <!-- logo -->
    <ImageView       
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:src="@drawable/app_logo" />

    <!-- share button -->
    <ImageButton
         android:id="@+id/btnMenuShare"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/action_btn"
        android:clickable="false"
        android:duplicateParentState="true"
        android:focusable="false" />

The problem is that I want add an over flow menu to share button like this one:

enter image description here

Please tell me how can I do that with the custom action bar layout.

ductran
  • 10,043
  • 19
  • 82
  • 165
  • does your device have a hardware menu button? – Raghunandan Oct 14 '13 at 04:22
  • I support many devices from android 2.3 so I think yes. – ductran Oct 14 '13 at 04:24
  • http://android-developers.blogspot.in/2012/01/say-goodbye-to-menu-button.html. this could help – Raghunandan Oct 14 '13 at 04:30
  • @Raghunandan Sorry, I don't know what you mean. I say again: I support many devices with or without menu button. And I don't care when user press menu button. I just want to show overflow menu when they press share button. Is that make sense? – ductran Oct 14 '13 at 04:40
  • check this if it helps http://blog.vogella.com/2013/08/06/android-always-show-the-overflow-menu-even-if-the-phone-as-a-menu/ – Raghunandan Oct 14 '13 at 05:11
  • forcibly setting the overflow menu feature was removed from sherlock actionbar, because it had issues when pressing the menu button on some devices. The overflow will only be caused when room on the action bar is full. – Rat-a-tat-a-tat Ratatouille Oct 14 '13 at 05:55

2 Answers2

5

Seem there is no right way to solve this problem, so I tried a hack by using popup windows with list adapter to fake action bar overflow menu.
It look like this example: http://rajeshandroiddeveloper.blogspot.com/2013/07/android-popupwindow-example-in-listview.html

ductran
  • 10,043
  • 19
  • 82
  • 165
3

Just override this two methods to call overflow menu in your activity which one is extending ActionBarActivity :

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.your_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Chirag Shah
  • 2,058
  • 2
  • 20
  • 30
  • you don't get the point. Because I use my own custom layout for actionbar, so I can't add menu into share button like image above. – ductran Aug 11 '14 at 02:14
  • 2
    @R4j I also used my custom actionbar and opening the same dropdown as above image,so give answer to your own question with different solution and make it as a right solution is not a proper way!!! – Chirag Shah Aug 12 '14 at 05:19