Mike
Mike

Reputation: 55

Toolbar Menu doesn't work with ViewPager

I'm learning android and currently trying to add toolbar from android.support:design to my app, but also I want to flip pages,after several research about this question in web, I write such code

1 ) activity_main.xml

<RelativeLayout
android:id="@+id/main_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
>

<include
    layout="@layout/toolbar"/>
<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="0dp"
    style="@style/MyCustomTabLayout"
    android:background="@drawable/bottom_menu"
    android:layout_alignParentBottom="true"
    android:paddingTop="?attr/actionBarSize"
     />

<com.example.misha.myapplication.utils.CustomViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/tab_layout"/>
</RelativeLayout>

2) toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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/my_toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/primary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
>
</android.support.v7.widget.Toolbar>

Next, in java code I have written code in MainActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_home_on));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_my_favorit_off));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_such_on));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setBackgroundResource(R.drawable.button_background_off);
    viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount(), getBaseContext());
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
                         }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}
 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
   return true;
 }

4 and my menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="ifRoom"
    android:orderInCategory="200"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>

<item
    android:id="@+id/action_user"
    android:orderInCategory="300"
    android:title="User"
    android:icon="@drawable/no_name_"
    app:showAsAction="ifRoom"/>
</menu>

Besides, in values/styles.xml

  <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>

</style>

In AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"

And I have trouble - when I click on toolbar menu nothing is happening...

I try to replace in main_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=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
</menu>

But here is the same effect... Could you help me please, where is my error ?

UPDATE When I have trying to solve the problem, I noticed, that if I remove

viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
        (getSupportFragmentManager(), tabLayout.getTabCount(),     getBaseContext());
    viewPager.setAdapter(adapter);
   tabLayout.setupWithViewPager(viewPager);
   viewPager.addOnPageChangeListener(new     TabLayout.TabLayoutOnPageChangeListener(tabLayout));

menu will be work.

I have forgotten to say that I am using smartphone and tablet with API - 17 for test , and another smartphone with api 15. But testing on emulator with api 23 - all good work.

Upvotes: 4

Views: 595

Answers (2)

Samuel Surya
Samuel Surya

Reputation: 455

Please change @style/AppTheme.PopupOverlay with @style/ThemeOverlay.AppCompat.ActionBar in activity_main.xml. It works for me.

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways">

Upvotes: 1

Elltz
Elltz

Reputation: 10869

And I have trouble - when I click on toolbar menu nothing is happening

Honestly, Sir, nothing should happen, because you have not implemented

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  // this is where you handle menu clicks etc etc.
  // so add a switch statement and handle clicks
  switch (item.getItemId()) {
case R.id.id:

    break;

default:
    break;
}
return true;
}

also add this to your manifest

application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" >//this guy

Upvotes: 1

Related Questions