Menu options does not respond when clicked - android

I had a menu in my app and it works correctly, now I add a TabLayout and the menu options does not respond to user click now no more.
Here is my activity_main.xml code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="#layout/toolbar"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPaper"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
My Activity onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
itemAdapter = new ClientAdapter(this, null);
tab = (TabLayout)findViewById(R.id.tab);
paper = (ViewPager)findViewById(R.id.viewPaper);
paperAdapter = new ViewPaperAdapter(getSupportFragmentManager());
HomeFragment homeFragment = new HomeFragment();
DoneFragment doneFragment = new DoneFragment();
paperAdapter.addFrament(homeFragment, "Home");
paperAdapter.addFrament(doneFragment, "Done");
paper.setAdapter(paperAdapter);
tab.setupWithViewPager(paper);
}
onCreateOptionMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
searchView = (SearchView)MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
And onOptionItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.refresh:
//Check the internet connection
if(Util.isConnected(this)){
// showProgress();
//Check if there is not pending item
sincronize(this);
dbManager.borrar();
new HomeFragment().getTarjetas();
}else{
Toast.makeText(this, "No hay conexion a internet.", Toast.LENGTH_LONG).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
The menu.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"
tools:context="developer.technologies.agilisa.acardmobile.MainActivity">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="100"
android:title="#string/Search"
app:showAsAction="always|collapseActionView"
android:elevation="8dp"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/refresh"
android:title="#string/refresh"
android:icon="#drawable/refresh"
android:orderInCategory="200"
app:showAsAction="always|collapseActionView"/>
<item
android:id="#+id/cerrar"
android:orderInCategory="10"
app:showAsAction="never"
android:title="#string/cerrar_session"/>
Can some one help me to solve that?

Try this changes:
Change Your Root layout To LinearLayout or Coordinator Layout-- As framelayout sometimes consume the click event of items under it.
1.in onCreateOptionsMenu()
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Your code
return true;
}
2.And in onOptionsItemSelected() add return true
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.refresh:
// your code
return true; //add this
default:
return super.onOptionsItemSelected(item);
}
}

Related

NullPointerException while invoking method BottomNavigationView.setOnNavigationItemSelectedListener

When I'm trying to switch between activies using intent in "switch case" the following error I got:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation2);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_home:
return true;
case R.id.ic_wallet:
Intent intent_wallet = new Intent(MainActivity.this,Wallet.class);
startActivity(intent_wallet);
return true;
case R.id.ic_status:
Intent intent_status = new Intent(MainActivity.this,Status.class);
startActivity(intent_status);
return true;
case R.id.ic_history:
Intent intent_history = new Intent(MainActivity.this,History.class);
startActivity(intent_history);
return true;
}
return false;
}
};
}
activity_main.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.praveen.gupta.frontpage.MainActivity">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigation2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#drawable/white_grey_border_top"
app:labelVisibilityMode="labeled"
app:menu="#menu/navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/ic_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="Home"
/>
<item
android:id="#+id/ic_wallet"
android:icon="#drawable/ic_account_balance_wallet_black_24dp"
android:title="Wallet"
/>
<item
android:id="#+id/ic_status"
android:icon="#drawable/ic_star_border_black_24dp"
android:title="Status"
/>
<item
android:id="#+id/ic_history"
android:icon="#drawable/ic_history_black_24dp"
android:title="History"
/>
</menu>
mOnNavigationItemSelectedListener is null thats why you are getting error.You are initializing outside on oncreate method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//move `mOnNavigationItemSelectedListener` code here .
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_home:
return true;
case R.id.ic_wallet:
Intent intent_wallet = new Intent(MainActivity.this,Wallet.class);
startActivity(intent_wallet);
return true;
case R.id.ic_status:
Intent intent_status = new Intent(MainActivity.this,Status.class);
startActivity(intent_status);
return true;
case R.id.ic_history:
Intent intent_history = new Intent(MainActivity.this,History.class);
startActivity(intent_history);
return true;
}
return false;
}
};
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation2);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

Switch onCheckedChangedListner not registering clicks inside toolbar menu

I have a switch in my app toolbar to provide an option to the user to change the language.
When I am using the switch, the onCheckedChanged method is not working.
Further, it looks to me that onOptionsItemSelected is also not being hit as I don't see any log message on the screen.
Please note that the app is not crashing when I use the switch.
The clicks on options present inside the overflow menu are working properly. I am getting the log in those cases.
Switch aSwitch;
RelativeLayout langSwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = this;
// Removed non relevant code here.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Log.d("tag_id",String.valueOf(id));
langSwitch = findViewById(R.id.app_bar_switch);
aSwitch = langSwitch.findViewById(R.id.langSwitch);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b){
//Language1 code
Toast.makeText(context,"Language1 Selected",Toast.LENGTH_SHORT).show();
}else{
//Language2 code
Toast.makeText(context,"Language2 Selected",Toast.LENGTH_SHORT).show();
}
}
});
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.savedPost){
Intent intent = new Intent(this,SavedPost.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Below is my menu_main.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"
tools:context="com.app_name.android.app_name.MainActivity">
<item
android:id="#+id/app_bar_switch"
android:orderInCategory="2"
android:title=""
app:actionLayout="#layout/switch_item"
app:showAsAction="always" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/savedPost"
android:checkable="false"
android:orderInCategory="4"
android:title="#string/saved_post" />
</menu>
This is my layout file for switch item.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="#+id/langSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textOff="#string/switch_text_off"
android:textOn="#string/switch_text_on"
android:focusable="true"
android:clickable="true"/>
</RelativeLayout>
Additional information.
If I create another menu item (code below) with showAsAction 'always' and click on this once, now when I use the switch the Toast message comes. I am clueless here as to why is it not happening for the first time. Also, how do I make it work without this menu item.
<item
android:id="#+id/english"
android:orderInCategory="3"
android:title="#string/switch_text_on"
app:showAsAction="always" />
To inflate an external layout over ActionBar that seems to first creating a view that holds your ChildView for your layout
like Example
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
Try this in your code
Switch aSwitch;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
View sw = menu.findItem(R.id.app_bar_switch).getActionView();
aSwitch = (Switch) sw.findViewById(R.id.langSwitch);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b){
//Language1 code
Toast.makeText(context,"Language1 Selected",Toast.LENGTH_SHORT).show();
}else{
//Language2 code
Toast.makeText(context,"Language2 Selected",Toast.LENGTH_SHORT).show();
}
}
});
return true;
}
Try
(compoundButton.isChecked())
In boolean
Or
just try another version of setonCheckedChangedListner
Example:
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){#Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (!compoundButton.isSelected()) { Log.i("Yeah" , "Is Not Selected"); invertLock(-1); } else { if (Utilities.isLockEnabled(context)) { Log.i("Yeah" , "Is Locked"); Utilities.showLockEnabled(context); } else { Log.i("Yeah" , "Is Not Locked"); invertLock(1); } } }

Navigation drawer, listen to item clicked events

I have this menu layout "navigation_menu.xml":
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item1"
android:title="#string/item1"/>
<item
android:id="#+id/item2"
android:title="#string/item2"/>
<item
android:id="#+id/item3"
android:title="#string/item3"/>
</menu>
My main activity layout "activity_main.xml" is this:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And this is the Java code of the main activity where I initialize the drawer:
private DrawerLayout drawer_layout;
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
drawer_layout = (DrawerLayout)findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer_layout, R
.string.open_drawer, R.string.close_drawer);
drawer_layout.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
The drawer opens and closes correctly, but I don't know how to do something when an item is clicked, for example showing a log.
Does anyone know?
Thanks
Use onNavigationItemSelected() method to handle Navigation Drawer Item Click. See Below Code.
private NavigationView mNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// ...
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
mNavigationView.setNavigationItemSelectedListener(this);// you need to set Listener.
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item)
{
// mDrawer.closeDrawer(GravityCompat.START);
switch (item.getItemId()) {
case R.id.item1: {
}
break;
case R.id.item2: {
}
break;
case R.id.item3: {
}
break;
}
return true;
}

Why setOnQueryTextListener() gets "on a null object reference" error?

I'm trying to add search icon to toolbar which pressed would open search bar.
But every method I tried didn't work and no icon appears.
Now I get this error and just can't find solution to it:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference
at pl.michalz.hideonscrollexample.activity.partthree.PartThreeActivity.onCreateOptionsMenu(PartThreeActivity.java:62)
This is my main Activity:
public class PartThreeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppThemeBlue);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_part_three);
initToolbar();
initViewPagerAndTabs();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem seachItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)
MenuItemCompat.getActionView(seachItem);
searchView.setOnQueryTextListener(
new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
Log.d("myApp", "onQueryTextSubmit ");
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.d("myApp", "onQueryTextChange ");
return false;
}
});
return true;
}
private void initToolbar() {
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
setTitle(getString(R.string.app_name));
mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
}
private void initViewPagerAndTabs() {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(PartThreeFragment.createInstance(), getString(R.string.tab_1));
pagerAdapter.addFragment(BlankFragment.createInstance("F word"), getString(R.string.tab_2));
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}
//kai paskaudzia toolbaro iconas
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;
case R.id.action_search:
// User chose the "Favorite" action, mark the current item
// as a favorite...
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
}
main Activity.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap" />
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#android:color/white"
app:tabIndicatorHeight="6dp"
app:tabSelectedTextColor="#android:color/white"
app:tabTextColor="#android:color/white" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Also searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/hint"
android:label="#string/app_name" />
And res/menu/menu_main.xml, added ic_search myself to drawable
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Could someone help?
I fixed error by changing namespace in menu_main.xml from app to my own myapp and it fixed automaticly by pressing "Alt+Enter".
res/menu/menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
//----------- it created automaticly --------------
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="#string/action_search"
//-------------- H E R E --------------------
myapp:showAsAction="always|collapseActionView"
myapp:actionViewClass="android.support.v7.widget.SearchView" />
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Main Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
Log.w("myApp", "onCreateOptionsMenu -started- ");
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.hint));
searchView.setOnQueryTextListener(
new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
Log.w("myApp", "onQueryTextSubmit ");
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.w("myApp", "onQueryTextChange ");
return false;
}
});
return true;
}
I faced the same problem. Solution is Simple.
Always check imports when doing alt+enter.
I did alt+enter and android.widget.SearchView got imported, whereas my implementation expected android.support.v7.widget.SearchView

Android menu of toolbar response to different fragment

Now my mainActivity.xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/tool_bar" />
<FrameLayout android:id="#+id/tab_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<include layout="#layout/bottom_bar" />
</LinearLayout>
This page has four tabs on the bottom,each tab is associated with a different fragment which will be added to android:id="#+id/tab_content".
The menu_main.xml like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_calendar"
android:icon="#drawable/ic_title_date"
android:orderInCategory="100"
android:title="#string/calendar"
app:showAsAction="always" />
<item
android:id="#+id/action_notification"
android:icon="#drawable/ic_title_notification"
android:orderInCategory="200"
android:title="#string/notification"
app:showAsAction="always" />
</menu>
I have these code in my MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
return super.onOptionsItemSelected(item);
}
Fragment2 and Fragment3 have these code:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_calendar)
{
showDatePicker();
return true;
}
return super.onOptionsItemSelected(item);
}
android:id="#+id/action_calendar" menuItem is a datePicker,and i want to use this menuItem to be filter to load data for fragment2 and fragment3.
Here is my question,when I navigate to the fragment3 and click the calendar menu and then change to fragment2,click the calendar,the system call onOptionsItemSelected method in fragment 3 ,not in fragment2 ?
I want calendar menu response to different fragment separately .

Categories

Resources