0

I want to make the entire red circled area (which includes the title, the action bar's background, and the search icon) clickable, but my codes only allows only the search icon clickable.

enter image description here

I found the similar question here: I found the same problem in this question: How to increase the menuitem width in android while using Actionbar but the solution is not working for my codes.

Here is my menu 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"
    android:id="@+id/search_view_menu">

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_dark"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="withText|ifRoom|collapseActionView"
        android:title="Enter your item"
        app:collapseIcon="@drawable/ic_close_dark"/>

</menu>

And here is my search java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.example_menu, menu);

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

        searchView.setMaxWidth(Integer.MAX_VALUE);

        searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });
        return true;
    }
gsk
  • 107
  • 11

2 Answers2

0

you can set clickable true in your SearchView

android:clickable="true"

or change `setIconified to false

searchView.setIconified(false);
Farzad Kamali
  • 751
  • 2
  • 6
  • 24
  • Thank you for your comment. Unfortunately, none of them worked for me. I tried (1) added `android:clickable="true"` in `menu xml`, (2) added `searchView.setIconified(false);` in `search java` , and (3) added `searchView.setClickable(true);` in `search java`. – gsk Apr 06 '20 at 20:04
0

Try this :

First change theme of your activity to AppTheme.NoActionBar

 <activity
            android:name=".myActivity"
            android:theme="@style/AppTheme.NoActionBar">

then add this EditText inside your Layout.

<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".terminalShortActivity">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#4EACE2"
        app:popupTheme="@style/ThemeOverlay.AppCompat">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Terminal Shortcuts"
                android:textColor="#FFFFFF" />
        </LinearLayout>
    </androidx.appcompat.widget.Toolbar>
    <EditText
        android:id="@+id/search_ip_terminal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:background="@drawable/search_ip_bg"
        android:drawableLeft="@drawable/ic_search_black_24dp"
        android:drawablePadding="8dp"
        android:hint="Enter Your Item"
        android:textColor="#7C000000" />
// Add here you remaing layout

</LinearLayout>

search_ip_bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/blue"/>
    <corners android:radius="32dp"/>
    <stroke
        android:width="2dip"
        android:color="#039BE5" />
    <padding android:top="12dp" android:left="12dp" android:bottom="12dp" android:right="12dp"/>

</shape>
Amitoz singh
  • 144
  • 1
  • 7
  • Thank you for your comment. Because I could not directly use `EditText` in `menu xml` (it seems it only cares about `item`), I used the following website to apply `EditText` in `menu xml`. Unfortunately, it did not work. I could not even see the modified layout using `EditText` in my app. – gsk Apr 06 '20 at 20:17
  • Change your activity theme in manifest file and use android:theme="@style/AppTheme.NoActionBar" .And then add this EditText in your layout not in menu. – Amitoz singh Apr 06 '20 at 20:24
  • Thank you again. When I set `android:theme="@style/AppTheme.NoActionBar"`, the app unfortunately does not show any action bar anymore, including the newly added toolbar. – gsk Apr 06 '20 at 20:44