My toolbar button is not showing in fragments i tried with setHasOptionsMenu(true); . when i click on navigation drawer item it loads new fragment for the each fragment i want to show different action bar button(notification or save). Following is my code,thanks in advance.
Following is my Fragment code: (It is same for all fragments in navigation drawer)
public class Company1 extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_company_profile,container, false);
init(view);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddDialog();
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.company_profile, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.notification:
break;
}
return super.onOptionsItemSelected(item);
}
}
This is company_profile.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="@+id/notification"
android:icon="@drawable/ic_action_notification"
android:title="@string/mnuNotificationText"
app:showAsAction="always"/>
</menu>
Please check this code snippet
public class Company1 extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_company_profile,container, false);
setHasOptionsMenu(true);
init(view);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddDialog();
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.company_profile, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.notification:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
//You can change menuitem property
//menu.findItem(R.id.notification).setVisible(true);
super.onPrepareOptionsMenu(menu);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.