make search bar match its parent width when searching in android studio

  android, android-studio, kotlin, searchview

first I know it’s a confusing title but hear me out

I’m creating a song app where you have multiple songs and when you press on a song the app shows you the songs lyrics.
I searched on google how to make a search bar and I got it working. What I don’t like about it is that it doesn’t fully go to the left.

Here is what I’m talking about

I want the text to disappear and the search bar to take what space is left

My MainActivity file

package com.biskyy.caietdecantarikt



import android.os.Bundle
import android.view.Menu
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.SearchView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var adapter: ArrayAdapter<*>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setSupportActionBar(findViewById(R.id.toolbar))


        adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, resources.getStringArray(R.array.cntArrays))
        lv_listView.adapter = adapter
        lv_listView.onItemClickListener = AdapterView.OnItemClickListener{ parent, view, position, id ->

            when (position) {
                0 -> {
                    setContentView(R.layout.activity_cantare)
                }

            }

            Toast.makeText(applicationContext, parent?.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show()
        }
        lv_listView.emptyView = tv_emptyTextView

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)

        val search = menu?.findItem(R.id.ic_search)
        val searchView = search?.actionView as SearchView
        searchView.queryHint = "Cautati o cantare"

        searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                adapter.filter.filter(newText)

                return true
            }
        })
        return super.onCreateOptionsMenu(menu)
    }

}

My activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/Theme.CaietDeCantariKT.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.CaietDeCantariKT.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <ListView
        android:id="@+id/lv_listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="55dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_emptyTextView"
        android:text="Niciun rezultat"
        android:textSize="20sp"
        android:visibility="gone"
        android:layout_centerInParent="true"/>

</RelativeLayout>

and my menu_main.xml file

<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.biskyy.caietdecantarikt.MainActivity" >
    <item
        android:id="@+id/ic_search"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="ifRoom|withText"
        app:actionViewClass="android.widget.SearchView" />
</menu>

Thanks!

Source: Android Questions

LEAVE A COMMENT