Reputation: 87
I am developing an application in which i m having four tabs.I want that on click of each tab,action bar should change.Right now I have given action bar title,search bar and overflow icon.But I want to change action bar on selection of each tab so that I can add icon in action bar according to requirement on selection of corresponding tab.
Here is my code of ontabselection,please suggest what can I do.Any help is acceptable.
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
FragmentTransaction fragMentTra;
if (tab.getText().equals("DEALS")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Deals");
FragmentDeals Fram1 = new FragmentDeals();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), Fram1);
fragMentTra.commit();
}
else if (tab.getText().equals("ORDER")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Order");
FragmentOrder Fram2 = new FragmentOrder();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), Fram2);
fragMentTra.commit();
}
else if(tab.getText().equals("CART")){
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Shopping Cart");
FragmentCart fram3 = new FragmentCart();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), fram3);
fragMentTra.commit();
}
else if(tab.getText().equals("HISTORY")){
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("History");
FragmentHistory fram4 = new FragmentHistory();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), fram4);
fragMentTra.commit();
}
}
Thanks.
Upvotes: 1
Views: 1441
Reputation: 1071
Something similar to Poutrathor, but clearer for me. I got it from here: https://stackoverflow.com/a/15218201/1887635
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_xxx, menu);
}
Upvotes: 0
Reputation: 2059
I see you are using fragments to do your tabs. QUite easily you can declare a menu.xml file for each fragment. Using sethasOptionmenu(true) in the oncreate of your fragment. Then you can adjust stuff if needed in onCreate Option menu().
Edit : here an example :
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
setHasOptionsMenu(true);
actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
}
@Override
public void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
if (!hideOptionsMenu) {
Log.e(TAG, " - onCreateOPTIONMenu");
inflater.inflate(R.menu.booking_accepted, menu);
MenuItem cancel = menu.findItem(R.id.cancel_booking_accepted);
cancel.getIcon().setAlpha(Constants.ENABLED_OPACITY);
menu.findItem(R.id.my_trips_switch).getIcon().setAlpha(Constants.ENABLED_OPACITY);
}
// (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection
}
Upvotes: 2