Reputation: 51
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
?
Upvotes: 3
Views: 110
Reputation: 1348
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
}
});
}
Upvotes: 1
Reputation: 6319
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.
Upvotes: 4