Android search padding rigth search settings

Hi, I have a problem with my search.

I am trying to do a full search string, but the sentences have a clear addition.

Here is a screenshot:

my app searchview

And my java code:

public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE); final MenuItem item=menu.findItem(R.id.procura); MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item) { tbs.setVisibility(View.GONE); vwpgr.setVisibility(View.GONE); return true; } @Override public boolean onMenuItemActionCollapse(MenuItem item) { tbs.setVisibility(View.VISIBLE); vwpgr.setVisibility(View.VISIBLE); return true; } }); searchView=(SearchView) MenuItemCompat.getActionView(item); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setQueryHint(getResources().getString(R.string.app_hint)); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { Intent search=new Intent(MainActivity.this,searchableactivity.class); search.putExtra("searchquery",query); startActivity(search); searchView.clearFocus(); item.collapseActionView(); return true; } @Override public boolean onQueryTextChange(String newText) { return false; } }); final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); //this remove left padding searchTextView.setDropDownAnchor(R.id.appbar); // this is not working searchTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT); //and i tryed this too: searchTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT); try { Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); mCursorDrawableRes.setAccessible(true); mCursorDrawableRes.set(searchTextView, R.drawable.search_cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background } catch (Exception e) { } return true; } 

my xml menu:

 <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=".MainActivity"> <item android:id="@+id/procura" android:title="@string/app_hint" android:icon="@mipmap/ic_search_black_24dp" app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView"/> 

And I tried this:

decision 1

decision 2

decision 3

How can i solve this?

0
source share
2 answers

try it

 searchTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT); 

Instead

 searchTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT); 
0
source

Here is my solution, Just setting the SearchView.SearchAutoComplete DropDownWidth view to match the parent or screen width doesn't work, because it is limited by the background resource, which has padding on all sides.

So, I realized that in order to make it work, we must change the background.

Solution: (you need to do this every time you change the size of the layout of the view):

 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String query) { SearchView.SearchAutoComplete mSearchSrcTextView = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); mSearchSrcTextView.setDropDownWidth(getResources().getDisplayMetrics().widthPixels); mSearchSrcTextView.setDropDownBackgroundResource(R.color.colorGray); modelManager.getLocationsSearchResults(query); return true; } }); 

Result

0
source

Source: https://habr.com/ru/post/976569/


All Articles