3

I have a button placed in an ActionBar. How can I make a button listener which is shared between different layouts that includes the ActionBar?

2 Answers2

4

Create a 'BaseActivity' or 'ParentActivity' or whatever you want to call it, which extends Activity. This simply does all things you do in every Activity.

Then all your other Activities, extend this ParentActivity, instead of the normal Activity.

Implement your actionbar creation and buttons which are always in it, and their actions in this ParentActivity.

Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
1

You can use a class like this:

public class Actionbar_BtnHandler extends Activity {
    Context context;
    public  Actionbar_BtnHandler (Context context)
    {
        this.context=context;
    }
    public void btn_handler (Button btn_mic,Button btn_post)
    {
        btn_mic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context,"MIKE",Toast.LENGTH_LONG).show();
            }
        });

        btn_post.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
    }
Mohamed Gamal
  • 1,348
  • 2
  • 13
  • 19