16

I'm writing an Android app for tablets. I've gone with the action bar to create my icons. However, I need to open a custom view when one of the menu items is clicked.

I don't want a custom action bar - I need to inflate a custom view when the "Browse Subjects" action bar item is clicked. This view will need to appear like a dropdown but be using my own custom layout as it will not be used for navigation.

Drop down menu

Here is my menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_browse"
        android:title="Browse Subjects"
        android:showAsAction="always"
        android:actionLayout="@layout/action_layout_browse"
        android:actionProviderClass="au.com.pearson.f12catalogue.action_providers.BrowseProvider"
            />
    <item android:id="@+id/menu_settings"
        android:title="Settings"
        android:orderInCategory="100"
        android:showAsAction="never" />
</menu>

I assumed the ActionProviderClass would allow me to instantiate a custom view when the action bar item is clicked but I can't work out a way - perhaps I'm going down the wrong path.

Any help on this would be MUCH appreciated! Thanks!

UPDATE: Thanks for links to action bar styling but I don't want to simply style a dropdown. I want to inflate a custom view. The view will perform DB queries etc aswell.

JackMahoney
  • 3,423
  • 7
  • 32
  • 50
  • 1
    i am looking for a sample which you have posted as image over here, can you please provide me any sample example or hint how to create custom dropdown and dynamically add menu items over there. – Naruto Mar 04 '13 at 02:34
  • 1
    the UI looks good. did you complete the app? is it up for download? – Zen Feb 20 '14 at 17:50

1 Answers1

13

Ok I worked out a solution myself. Basically the actionProviderClass is used to instantiate an actionView in the actionBar. In this class you can attach an onClick listener to the view you inflate. I used this listener to inflate a dropdown view in the main frame when clicked.

For instance

public class BaseProvider extends ActionProvider {

    protected final Context context;
    protected final int layout;
    protected final BaseProvider self;
    protected View view;
    protected int positionLeft = 0;
    protected Dropdown dropdown;

    public BaseProvider(Context context, int layout, Dropdown dropdown) {
        super(context);
        this.layout = layout;
        this.context = context;
        this.self = this;
        this.dropdown = dropdown;
    }

    @Override
    public View onCreateActionView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        View view = inflater.inflate(this.layout, null);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                self.onItemClick();
            }
        });
        this.view = view;
        return view;
    }

    public boolean onItemClick(){
        toggleDropdown();
        return true;
    }

    protected void toggleDropdown(){
        this.positionLeft = getRelativeLeft(view);
        DropdownInflater.getInstance().toggleDropdown(this.dropdown,this.positionLeft);
    }

    protected int getRelativeLeft(View view) {
        int[] loc = new int[2];
        view.getLocationOnScreen(loc);
        return loc[0];
    }
}
JackMahoney
  • 3,423
  • 7
  • 32
  • 50
  • 11
    Would be awesome if you could provide info how you did the Dropdown class. Is it based on some other Widget or is it just a ListView somehow? – jelgh Aug 13 '13 at 06:26
  • How and where did you attach onClick listeners for the items in the ActionProvider? – lschlessinger Jul 02 '14 at 15:24