Reputation: 12803
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 ?
Upvotes: 0
Views: 100
Reputation: 2423
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.
setHasOptionsMenu(true);
3.Than override onPrepareOptionsMenu() in each fragment like this:
@Override
public void onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.action_edit);
if(item != null) {
item.setVisible(true);
//in fragment where you want to hide this menu item
//item.setVisible(false);
}
}
Hope this will help you :)
Upvotes: 1
Reputation: 2904
First, add the pen icon with visible=false
in your menu.
Then, create a global MenuItem
variable.
MenuItem mPenIcon;
and in onPrepareOptionMenu
method
public boolean onPrepareOptionsMenu(Menu menu)
{
mPenIcon = menu.findItem(R.id.penicon);
return true;
}
when the tab you want the pen icon to appear is selected,
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition()==1)//Replace 1 with your tab position
pen.setVisible(true);
}
when the tab is unselected,
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
if(tab.getPosition()==1)//Replace 1 with your tab position
pen.setVisible(false);
}
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)
in onCreateView
of Edit_WorkFragment
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
//other codes
setHasOptionMenu(true);
//other codes
}
and create menu for that fragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.edit_work, menu);
//You should create new menu for Edit_work fragment with both home and pen icons
super.onCreateOptionsMenu(menu, inflater);
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
}
Upvotes: 1