Adrian Coutsoftides
Adrian Coutsoftides

Reputation: 1293

Implementing search bar into tabFragment view

Hi there for some reason I can't seem to get my search widget to appear above my tab fragments despite perilously browsing stack overflow and other internet forums, I feel as though the fault lies in my XML althought I can't see where:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.lab1.ac01220.bloomv2.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_1" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_2" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_3" />

        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />



</android.support.design.widget.CoordinatorLayout>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

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.lab1.ac01220.bloomv2.MainActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />

<item android:id="@+id/action_search"
    android:title="@string/searchHint"
    android:icon="@drawable/ic_search"
    app:showAsAction="always"
    app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

I've inflated the action as to be expected in my main activity however it appears that it is still not showing up. I've tried modifying styles.xml however that keeps throwing build errors.

I'm aware that in styles.xml it says AppTheme.NoActionBar however I've tried removing this as well as changing the Boolean values it contains. Removing it throws build errors and change the values creates two action bars that sit ontop of eachother (one with the search widget the other with the tabs).

In the image I've attached I just want the search icon to appear on right as usual and expand left-outwards, can someone help me out?

n/a

I'll include my inflater incase I've done it wrong:

inflater in MainActivity.java

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);

        final MenuItem searchItem = menu.findItem(R.id.action_search);
        final SearchView searchDatabase = (SearchView) searchItem.getActionView();

        searchDatabase.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return searchTerm(query);
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

Upvotes: 2

Views: 61

Answers (1)

Mike M.
Mike M.

Reputation: 39191

Per the documentation for onCreateOptionsMenu():

You must return true for the menu to be displayed; if you return false it will not be shown.

The default super implementation is going to return false, so you must explicitly return true; there for your menu to show.

Also, if you're providing your own Toolbar in lieu of the decor-provided ActionBar, you'll need to set it as the support ActionBar for it to show an options menu in that manner. That is:

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Upvotes: 2

Related Questions