Search code examples
javaandroidandroid-studioandroid-layoutandroid-toolbar

I have a problem with ToolBar and SearchView


I am working on android app and designing with the help of androidx. I put SearchView into the Toolbar and I want that by clicking any point on ToolBar, the searchView would activate (currently, only by clicking icon of searchbar it activates. This is my code in activity_main XML file

I want that by clicking any ToolBar point, SearchView to activate and let user enter the text

<androidx.appcompat.widget.Toolbar
        android:id="@+id/tool_bar_id"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_margin="20dp"
        android:layout_marginTop="40dp"
        android:background="@drawable/circle"
        android:elevation="5dp"
        app:layout_constraintCircleRadius="100dp"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.SearchView
            android:id="@+id/search_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp" />
    </androidx.appcompat.widget.Toolbar>

Solution

  • You can do it by code, add clicklistener to toolbar

    Toolbar toolbar=findViewById(R.id.tool_bar_id);
            SearchView searchView=findViewById(R.id.search_id);
            toolbar.setOnClickListener(v -> {
                searchView.setIconified(false);
    
            });