I have 4 swipe tab
in my app. I have successfully to create a home icon on action bar
. Now I wanted to have a pen icon beside the home icon when comes to work details tab. How can I achieve this ? Thanks.
TabsFragmentAdapter
public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {
public TabsFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
if(index == 0) {
Fragment fragment=new EditInformation();
Bundle bundle = new Bundle();
bundle.putString("ID", Edit.ID);
fragment.setArguments(bundle);
return fragment;
}
if(index == 1) {
Fragment fragment = new Edit_WorkForce();
Bundle bundle = new Bundle();
bundle.putString("ID", Edit.ID);
fragment.setArguments(bundle);
return fragment;
}
if(index == 2) {
Fragment fragment = new Edit_WorkDetails();
Bundle bundle = new Bundle();
bundle.putString("ID", Edit.ID);
fragment.setArguments(bundle);
return fragment;
}
if(index == 3) {
Fragment fragment = new Edit_Staff_Benefit_ListView();
Bundle bundle = new Bundle();
bundle.putString("ID", Edit.ID);
fragment.setArguments(bundle);
return fragment;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
}
Activity B
public class ActivityB extends ActionBarActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabsFragmentPagerAdapter tabsAdapter;
private String[] item = new String[]{"Information","Work Force","Work Details","Staff Benefit"};
private String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
// id=getIntent().getExtras().getString("ID");
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsAdapter);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(int i=0; i<4; i++){
actionBar.addTab(actionBar.newTab().setText(item[i]).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tab_1_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.homePage: // create homePage icon here ?
Intent i=new Intent(getApplicationContext(),HomePage.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabReselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
tab_1_menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="@mipmap/home"
android:id="@+id/homePage"
android:orderInCategory="100"
android:title="Tab"
app:showAsAction="always" />
</menu>
Should I add the icon in Edit_WorkDetails fragment
or inside Activity B ?
First, add the pen icon with
visible=false
in your menu.Then, create a global
MenuItem
variable.and in
onPrepareOptionMenu
methodwhen the tab you want the pen icon to appear is selected,
when the tab is unselected,
Hope this helps :)
Edited
The first method I posted isn't the actual way of implementing that.
Here's the actual method.
Put
hasOptioinMenu(true)
inonCreateView
ofEdit_WorkFragment
and create menu for that fragment
As I got your point, you want to show specific menu option for specific fragment only. Here I have one solution for you.
1.Add your menu item in menu xml file ex. action_edit
2.Add this in onCreateView() method of each fragment.
3.Than override onPrepareOptionsMenu() in each fragment like this:
Hope this will help you :)