androidandroid-layoutlistviewsearch-suggestion

suggestion in search view (height)


I've used the MaterialSearchView library, The point is that I used navigation drawer at this Activity before I used this library, so I had to use two layers for menu XML. I do not know if it is right or not. I've sampled the code I used to be:

This is for navigation drawer menu items:

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_SellPaper"
        android:icon="@drawable/ic_seller"
        android:title="@string/SellMyPaper" />
    <item
        android:id="@+id/nav_sendPaper"
        android:icon="@drawable/ic_input_black_24dp"
        android:title="@string/SendPaperMenu" />

    <item
        android:id="@+id/nav_MyDownloads"
        android:icon="@drawable/ic_lib"
        android:title="@string/MyLibMenu" />
    <item
        android:id="@+id/nav_fav"
        android:icon="@drawable/ic_favorite"
        android:title="@string/MyFav" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_account_box_black_24dp"
        android:title="@string/UserAccMenu" />
    <item
        android:id="@+id/nav_share"
        android:icon="@drawable/ic_share_black"
        android:title="@string/ShareMenu" />
    <item
    android:id="@+id/nav_slideshow"
    android:icon="@drawable/ic_slideshow"
    android:title="@string/LearningMenu" />
    <item
    android:id="@+id/nav_logout"
    android:icon="@drawable/ic_exit_to_app"
    android:title="@string/LogoutMenu" />
</group>

<item android:title="@string/ConnectUsMenu">
    <menu>
        <item
            android:id="@+id/send_tel"
            android:icon="@drawable/ic_send_black_24dp"
            android:title="@string/TelegramMe" />
        <item
            android:id="@+id/send_mail"
            android:icon="@drawable/ic_email_black_24dp"
            android:title="@string/EmailME" />
    </menu>
</item>

this is for search menu item:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_action_search"
    android:orderInCategory="100"
    android:title="@string/abc_search_hint"
    app:showAsAction="always" />

In Appbar layout I have used the following code:

<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:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        app:titleTextColor="@color/TextView"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


    <com.miguelcatalan.materialsearchview.MaterialSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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


<include layout="@layout/content_menu" />

And my Activity java code contains the following:

        public class MenuActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener, View.OnClickListener, MaterialSearchView.OnQueryTextListener, MaterialSearchView.SearchViewListener {
     MaterialSearchView searchView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_menu);
            searchView = findViewById(R.id.search_view);
            searchView.setVisibility(GONE);
            searchView.setOnQueryTextListener(this);
            searchView.setOnSearchViewListener(this);
...
 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, SearchURL, new Listener<String>() {

            @Override
            public void onResponse(String response) {

                try {
                    //this is my output array
                    ArrayList<String> arrayList = new ArrayList<>();

                    //lvl1
                    JSONArray jsonarray = new JSONArray(response);
                    for (int i = 0; i < jsonarray.length(); i++) {
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        //lvl2
                        JSONArray catArray  = jsonobject.getJSONArray("Category");
                        for(int j = 0; j < catArray.length(); j++){

                            //lvl3
                            if(!arrayList.contains(catArray.getString(j)) && !catArray.isNull(j) ){

                                arrayList.add(catArray.getString(j));

                            }
                        }

                    }

                    SharedPreferences prefs=PreferenceManager.
                            getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor edit=prefs.edit();
                    Set<String> set = new HashSet<String>(arrayList);
                    edit.putStringSet("CAT", set);
                    edit.apply();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }

        });

        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);

 Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
     DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);

        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
....
}

   @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_Sell) {

     .........
        }


        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    ...
     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);

        MenuItem item = menu.findItem(R.id.action_search);
        searchView.setMenuItem(item);

        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            if (matches != null && matches.size() > 0) {
                String searchWrd = matches.get(0);
                if (!TextUtils.isEmpty(searchWrd)) {
                    searchView.setQuery(searchWrd, false);
                }
            }

            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

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

    @Override
    public void onSearchViewShown() {
        searchView.setVisibility(View.VISIBLE);

        SharedPreferences prefs = PreferenceManager.
                getDefaultSharedPreferences(getApplicationContext());
        Set<String> set = prefs.getStringSet("CAT", null);
        List<String> sample = null;
        String[] values = new String[0];
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            sample = new ArrayList<String>(Objects.requireNonNull(set));

            values = new String[Objects.requireNonNull(sample).size()];

            for (int i = 0; i < sample.size(); i++) {
                values[i] = sample.get(i);
            }
        }

        searchView.setSuggestions( getResources().getStringArray(R.array.query_suggestions)  );


    }

    @Override
    public void onSearchViewClosed() {
        searchView.setVisibility(View.GONE);


    }

After using this method, I'm having a problem, the number of search suggestions for me Not more than one, and I want to see more suggestions.

The image below shows I have 3 suggestions, but I can only see one suggestion:

What I see:

What I see

What I want to see:

What I want to see

Please guide me thanks from you...


Solution

  • I suggest that you set the height to android:layout_height="?attr/actionBarSize". This is what I do in almost all scenarios.