0

I've got an Android app where in each activity I've got the same custom ActionBar (just a RelativeLayout), which holds the logo and some buttons.

The problem is that for each activity, the buttons are different and interact with the views inside the current activity.

I've started by creating a custom view which I can put in every activity layout, but it seems that this is not the best solution - as the custom ActionBar will have to hold all the interactions with all the views and will have to access the layout itself.

What would be the right way to go about this? To create a custom Actionbar which has some common design on all activities and some different functionality on each activity.

Thanks

Roman
  • 4,443
  • 14
  • 56
  • 81
  • The only way I can see to do this is to use the same `layout` in each `Activity`'s xml and change the buttons on it to how you need for each. I have a custom layout for an ActionBar but it's pretty much the same for each `Activity` which each extends a custom `BaseActivity`. When I need a little different functionality then I `override` the function in my `BaseActivity` – codeMagic Jun 28 '13 at 00:07

1 Answers1

0

As following DRY guidelines I would suggest creating custom view for the action bar. Also make it configurable so you could specify how many buttons you need in which activity.

You can set up buttons either dynamically or in xml file by passing custom attributes.

In order to allow buttons to interact with the rest on the layout you should set on click listeners in activity:

public class MyActivity extends Activity {

    MyActionBar actionBar;

    public void onCreate(Bundle bundle) {
        actionBar = (MyActionBar) findViewById(R.id.action_bar);
        Button firstButton = actionBar.getButton(1);

        firstButton.setOnClickListener(new OnClickListener(){
            public void onCLick() {
                // interact with main activity
            }
        }
    }
}

If you are true xml fan you can do something similar to what Android done with onClick attribute. It uses reflections to invoke provided parents method.Info about that and source code. Maybe you can reuse what is already implemented or copy and adapt from Android.

If you need any help give me a shout.

Community
  • 1
  • 1
Tautvydas
  • 2,027
  • 3
  • 25
  • 38
  • Thanks - That's what I've thought. Hoped for something easier though :) – Roman Jun 28 '13 at 15:00
  • Its not as bad as it sounds. I had to do something similar and you only have to do it once. – Tautvydas Jun 28 '13 at 15:25
  • Yes, but this way it means the ActionBar will actually hold views for numerous layouts. Maybe... is there a way to inherit a base ActionBar which holds the base UI and add the needed buttons for each inherited class? – Roman Jun 28 '13 at 15:32
  • I am not sure how customisable the ActionBar should be for you. But if you only need to change the number of buttons and some icon it shouldn't be a problem to have the references (or you could add buttons dynamically). If the Bar has to be really different in some cases than I would suggest inheritance. – Tautvydas Jun 28 '13 at 15:35