johannilsson / android-actionbar

DEPRECATED Android Action Bar Implementation
1.31k stars 564 forks source link

Not Actually an Issue but an Enhancements #36

Open crashenburne opened 13 years ago

crashenburne commented 13 years ago

First of all a big thanks to your component. I am currently developing my first android app with the help of your component.

I have added some enhancements and if you don't mind, you can review and probably add them

  1. Added home click listener

    public void setOnHomeClickListener(OnClickListener listener) { mHomeBtn.setOnClickListener(listener); }

  2. Added Action listener - a copy of your removeAction but this one is overriding the listener

    public void setActionOnClickListener(Action action, OnClickListener listener) { int childCount = mActionsView.getChildCount(); for (int i = 0; i < childCount; i++) { View view = mActionsView.getChildAt(i); if (view != null) { final Object tag = view.getTag(); if (tag instanceof Action && tag.equals(action)) { v.setOnClickListener(listener); } } } }

Example implementation

The setHomeOnClickListener will intercept the action before proceeding. Basically it will just validate if you want to discard changes made on the create screen.

    actionBar.setOnHomeClickListener(new OnClickListener() {        
        @Override
        public void onClick(final View v) {
            // TODO Auto-generated method stub
            AlertDialog.Builder builder = new AlertDialog.Builder(CreateActivity.this);
            builder.setMessage("Discard changes?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       actionBar.onClick(v);       
                       CreateActivity.this.finish();                            
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                   }
               });

            AlertDialog alert = builder.create();
            alert.show();
        }
    }) ;

The setActionOnClickListener will intercept the action by first saving the data before proceding with the request.

    actionBar.setActionOnClickListener(saveAction, new OnClickListener() {          
        @Override
        public void onClick(View v) {
            //save data
            actionBar.onClick(v);
        }
    });