Home > Enterprise >  Search bar not appearing on android activity
Search bar not appearing on android activity

Time:06-26

I'm working on an android project with Java, and currently I'm trying to get an activity that uses a search bar and searches for a given entry to work. I have a problem with the search bar not appearing at all, I followed multiple guides and searched for similar questions to no avail, hence here I am trying to find an answer here. The files : search_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">
    <item
        android:id="@ id/search"
        android:icon="@drawable/ic_search"
        android:title="Rechercher"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="always|collapseActionView" />
</menu>

activity_stock.xml :

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

    <ListView
        android:id="@ id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

stock.java :

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class stock extends AppCompatActivity {

    ListView lv;
    ArrayAdapter<String> adapter;
    SearchView s;
    JSONParser parser = new JSONParser();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock);

        lv = findViewById(R.id.list_view);

        List<String> l = new ArrayList<String>();
        l.add("a");
        l.add("b");
        l.add("c");

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, l);
        lv.setAdapter(adapter);
    }

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

        MenuItem i =  menu.findItem(R.id.search);
        s = (SearchView) i.getActionView();
        s.setQueryHint("Tappez ici pour chercher le code bar");
s.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                adapter.getFilter().filter(s);
                return true;
            }
        });
        return true;
    }

I don't see anything wrong with the code, and even when I tried doing it with a toolbar the search bar still does not appear, I've been stuck here for a while so would really appreciate some help, thanks !

Screenshot from the activity :

enter image description here

CodePudding user response:

You are using app:actionViewClass="android.widget.SearchView" in your menu.xml . But you are casting it into androidx.appcompat.widget.SearchView in stock.java.

So use, app:actionViewClass="androidx.appcompat.widget.SearchView" app:showAsAction="ifRoom|collapseActionView" in menu.xml

CodePudding user response:

I've solved the problem, after testing stuff I found out that the main issue was that my onCreateOptionsMenu function is never called when the activity is launched, so after looking through some questions about similar issues here I added a toolbar to the xml file, and added this to the java file :

toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);

And the search bar appears now.

  • Related