Michael Alexander Montero Michael Alexander Montero - 25 days ago 9
Android Question

Menu options does not respond when clicked

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?

Answer Source

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);
    }
}