Android Search View not displaying any text - android

I am using search view . It was working before now its not responding, I cant see any text being typed in the Search View, also I am not getting back any call back when text is changed.
Here is the xml layout file,
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:id="#+id/searchView"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/cardsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"/>
</LinearLayout>
Here is how I am setting SearchView Callback,
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.colorPrimary));
searchEditText.setHintTextColor(getResources().getColor(R.color.dark_grey));
searchView.setOnQueryTextListener(this);
I am not getting any callbacks to these methods when I type in SearchView.
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
filterCards(newText);
return false;
}
I am implementing SearchView.OnQueryTextListener in my Fragment.
Thanks.

Try to give,
android:textColor="#000000"
to your android.support.v7.widget.SearchView
Note : May be your text is white color, that's why you can't to see.

Related

Android Search View to remove cross icon

I am using the search view in custom Toolbar according to my design, I am having another cross icon, so I want to remove the cross icon for search view them on default provided by android.
<SearchView
android:id="#+id/search_learning_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="#string/toolBar_hint"
android:clickable="true"
android:searchIcon="#null"
android:paddingTop="#dimen/element_padding_small"
android:focusable="false"
android:paddingBottom="#dimen/element_padding_small"
android:background="#color/White"
android:queryBackground="#color/White"
android:iconifiedByDefault="false"
android:layout_alignParentTop="true">
</SearchView>
ImageView searchViewIcon = (ImageView)searchView.findViewById(R.id.search_close_btn);
searchViewIcon.setVisibility(View.GONE);
Toast.makeText(CustomSearchActivity.this, ""+searchViewIcon, Toast.LENGTH_SHORT).show();
Use this to do so-
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String query) {
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchViewIcon.setVisibility(View.GONE);
return true;
}
});

SearchView + Filterable RecyclerView make SearchView losing focus

I have both searchView and recyclerView in same layout.
recyclerView is using filter and it's working properly.
The problem occurred when i entered query on searchView. Instead of let me finish typing, searchView will lose focus after i entered first character of my query. Let's say i want show all items which contain "1234" in its name, searchView will lose focus after i entered "1" then display all items which contain "1". I have to click that searchView again if i want to continue.
Here is my layout (in fragment):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.SearchView
android:id="#+id/sv_test"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
android:inputType="number"
app:iconifiedByDefault="false"
app:queryBackground="#android:color/transparent"
app:queryHint="Hint"
app:theme="#style/AppSearchView">
</android.support.v7.widget.SearchView>
<View
android:id="#+id/divider50"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/light_grey"
android:visibility="visible" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_test"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Here is my filter command:
private void setSearchview() {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return false;
}
});
}
I have similar layout in custom alert dialog and it's working fine without any issues. Weird right?
If you have a focusable item (like an EditText) inside your RecyclerView, it might be stealing the focus on the first update.
To disable the RecyclerView gaining any focus, you can add the descendantFocusability parameter to the XML like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_test"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content" />
If you need to focus inside the RecyclerView, you might work out a solution which sets this parameter dynamically (so it turns focus off before the update, and back on after it).
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return true;
}
#Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return true;
}
});
change false to true.

Android SearchView change cursor position after hint icon

I am using android.support.v7.widget.SearchView and the hint icon is at the left of the cursor as shown in the image .
But i want to make the cursor begins after the hint search icon .
XML:
<android.support.v7.widget.SearchView
android:id="#+id/airlines_searchView"
android:iconifiedByDefault="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:onClick="EnableSearchView"
android:background="#fbf7f7"
/>
I got the EditText of the search view as following :
EditText search_text=(EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
and i don't know what are the required properties to set to it .
I checked That but no solutions found, any help please ?
Have to change the android:iconifiedByDefault to app:iconifiedByDefault
and add those two lines to avoid launching the keyboard on starting the activity and it is shown only after clicking searchview
android:focusable="false"
android:focusableInTouchMode="true"
So Try this:
<android.support.v7.widget.SearchView
android:id="#+id/airlines_searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:textStyle="normal"
android:layout_marginTop="15dp"
android:background="#fbf7f7"
android:focusable="false"
android:focusableInTouchMode="true"
app:iconifiedByDefault="false"/>
Set search view iconified by default to false on click and to true on close the search view
searchView.setOnSearchClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.setIconifiedByDefault( false );
}
} );
searchView.setOnQueryTextListener( new android.support.v7.widget.SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//do search
}
#Override
public boolean onQueryTextChange(String newText) {
//do search
}
} );
searchView.setOnCloseListener( new android.support.v7.widget.SearchView.OnCloseListener() {
#Override
public boolean onClose() {
searchView.setIconifiedByDefault( true );
}
} );
<android.support.v7.widget.SearchView
android:id="#+id/airlines_searchView"
app:iconifiedByDefault="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="#fbf7f7"
/>
Instead of android:iconifiedByDefault="false" set app:iconifiedByDefault="false"

button cannot be seen even though set visibility android

I have a searchview and button next to it. And the button is make as visible GONE. when typing something on searchview I want to show the button. My problem is it is not showing the button properly even though i make the visibility to VISIBLE.
svSearchSentItem.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
btnClear.setVisibility(View.VISIBLE);
preLast = 0;
isQuickSearch = true;
currentMailInboxListMap = new ArrayList<Map<String, String>>();
setSentItemListView(query, 0, 50, true,false);
((DashboardActivity)getActivity()).mailSentSearchQuery=query;
searchCycle = 1;
svSearchSentItem.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.isEmpty()) {
}
else
{
btnClear.setVisibility(View.VISIBLE);
}
return false;
}
});
XML layout
LinearLayout
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:weightSum="8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/layoutSearch">
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/svMailSent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Search"
android:autoText="true"
android:inputType="text"
android:queryHint="Search"
android:layout_weight="6"
android:layout_centerInParent="true"
android:iconifiedByDefault="false"
/>
<Button
android:id="#+id/btnSentSearchClear"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="Clear"
android:textAllCaps="false"
android:layout_weight="2"
android:visibility="gone"/>
</LinearLayout>
U can use ajax with android to check if the searchview is empty or not, if it is not empty then make view.visible else view.gone.
http://programmerguru.com/android-tutorial/android-ajax-auto-load
this might help!
It didn't help changing anything in XML layout. I found this solution instead. In the oncreateview, I get the layoutparams and assign it in the onQueryTextSubmit event in searchview.
oldLayoutParams = svSearchSentItems.getLayoutParams();
svSearchSentItem.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
btnClear.setVisibility(View.VISIBLE);
preLast = 0;
isQuickSearch = true;
currentMailInboxListMap = new ArrayList<Map<String, String>>();
setSentItemListView(query, 0, 50, true,false);
((DashboardActivity)getActivity()).mailSentSearchQuery=query;
searchCycle = 1;
svSearchSentItem.clearFocus();
svSearchSentItem.setLayoutParams(oldLayoutParams);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.isEmpty()) {
}
else
{
btnClear.setVisibility(View.VISIBLE);
}
return false;
}
});
sorry but you cant use those 3 properties together in this way :)
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_weight="2"
when you use layout_weight you need set one of property: height or width to 0dp:
android:layout_height="0dp"
android:layout_width="0dp"
https://developer.android.com/guide/topics/ui/layout/linear.html
weight (float weight)
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
i propose you to delete all properties and first set only:
id
width
height
if it you will see what u want then you can add some extra properties this way you will see when it fails :)
use this and report result :
<LinearLayout
android:id="#+id/layoutSearch"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="8">
<SearchView
android:id="#+id/svMailSent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"/>
<Button
android:id="#+id/btnSentSearchClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
#ceph3us, I changed the layout as you suggested. But still I can't see the button. It seems that it is going right corner outside the screen – creative Pro
you are embedding badly the child view, or your parent is erroneously defined
and don't use "fill_parent" from api 8 is depreciated - btw os'es with api's < 8 are almost dead :)
LinearLayout.LayoutParams.FILL_PARENT;
public static class LayoutParams {
/**
* Special value for the height or width requested by a View.
* FILL_PARENT means that the view wants to be as big as its parent,
* minus the parent's padding, if any. This value is deprecated
* starting in API Level 8 and replaced by {#link #MATCH_PARENT}.
*/
#SuppressWarnings({"UnusedDeclaration"})
#Deprecated
public static final int FILL_PARENT = -1;
...
}
It didn't help changing anything in XML layout. I found this solution instead. In the oncreateview, I get the layoutparams and assign it in the onQueryTextSubmit event in searchview.
oldLayoutParams = svSearchSentItems.getLayoutParams();
YEAH BUDDY YOU ROCK!!!
copy paste does not require understanding - i wish to have more developers like you :)
see this is what you trying to do:
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
final View btnClear = findViewById(R.id.btnSentSearchClear);
SearchView sv = (SearchView) findViewById(R.id.svMailSent);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
btnClear.setVisibility(newText!=null
&& !newText.isEmpty() ? View.VISIBLE : View.GONE);
return false;
}
});
}
FULL WORKING ANDROID STUDIO EXAMPLE ON GITHUB
https://github.com/c3ph3us/exampleHideButtonForSearchView/

Android - Make whole search bar clickable

I am using a search-view element in my fragment to implement search feature.
<SearchView
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="7dp"
android:background="#color/white" />
The problem is only the search icon is clickable other area in the search bar is not clickable, when i click the icon only i can able to search.
Can you please help me to make the whole search area clickable.
This can be simply done by setting Iconified to false on OnClick of SearchView.
searchBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchBar.setIconified(false);
}
});
Reference: Eric Lui's answer
Hopefully it will help.
UPDATE:
You can also use it directly in your XML
app:iconifiedByDefault="false"
search_bar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
search_bar.onActionViewExpanded();
}
});
The trick isn't so much to make the entire area clickable as much as it is to expand it and then hide the keyboard.
First, your layout in the XML is fine, leave it as is, in your Java, you want to have the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//Define your Searchview
SearchView searchView = (SearchView) findViewById(R.id.search_bar);
//Turn iconified to false:
searchView.setIconified(false);
//The above line will expand it to fit the area as well as throw up the keyboard
//To remove the keyboard, but make sure you keep the expanded version:
searchView.clearFocus();
}
What this accomplishes is it expands the keyboard to the entire length of the area, it focuses on it, which allows the entire area to be clickable, and then it removes the keyboard so that it looks to the user like they are able to click that field and bring up a keyboard.
Should accomplish what you are looking for.
-Sil
add this to your xml android:iconifiedByDefault="false" it will keep
open your searchview.
and to clear it add clearfocus to your searchview object.
What is the clickable mean? trigger search action or just make the edit area focused? If it is the first, you can just make the icon clickable=false. and make the whole layout clickable and implement a event listener.
<SearchView
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:click="onClick"
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="7dp"
android:background="#color/white" />
The onClick method should be
public void onClick(View v) {
  InputMethodManager im = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
im.showSoftInput(editText, 0);
}
For latest versions of android -
Try:
app:iconifiedByDefault="false"
Example -
<android.support.v7.widget.SearchView
android:id="#+id/source_location_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_marginRight="20dp"
app:iconifiedByDefault="false"
app:queryHint="Your hint text" />
searchView.setIconified(false);
Just write it in xml
android:iconifiedByDefault="false"
Worked for me
Write the lines of code given below in your Java file:
SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setQueryHint("Enter your address");
searchView.setIconified(false);
searchView.clearFocus();
After that, don't forget to write android:focusable="true" in your XML file inside SearchView tag.
For example:
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true" />
Use This
searchView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
searchView.onActionViewExpanded();
OR
searchView.setIconified(false);
}
});
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean b)
{
if(!b)
{
if(searchView.getQuery().toString().length() < 1)
{
searchView.setIconified(true); //close the search editor and make search icon again
OR
searchView.onActionViewCollapsed();
}
searchView.clearFocus();
}
}
});
in code:
private void initSearchView(View layout) {
// Locate the EditText in listview_main.xml
searchInput = (SearchView) layout.findViewById(R.id.search);
//searchInput.onActionViewExpanded();//force show keyboard at start
//==>region to enable search after click in input-text(not only at magnifier icon)
searchInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchInput.onActionViewExpanded();
}
});
searchInput.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (!b) {
if (searchInput.getQuery().toString().length() < 1) {
searchInput.setIconified(true);
}
searchInput.clearFocus();
}
}
});
//endregion
}
and in my layout:
<androidx.appcompat.widget.SearchView
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar_main"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="2dp"
android:background="#drawable/search_border"
android:inputType="text"
android:layoutDirection="ltr"
android:searchSuggestThreshold="2"
app:queryHint="#string/search"
app:searchHintIcon="#null" />
Use the below code.
<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/earningSearchView"
app:queryHint="Search"
app:iconifiedByDefault="false"
app:queryBackground="#android:color/transparent"/>
So recently I've had to do this and I tried all the answers provided but each had some dodgy behaviour - for example the x close/erase all button would only show up if you'd clicked on the search icon, otherwise you'd be able to edit etc. but you would only see the x as an erase all button.
Looking at the SearchView's code I noticed that clicking the mSearchButton calls onSearchClicked() and not the suggested onActionViewExpanded(), but the former is a package private function so it can't be called directly. So I came up with this:
private val searchButton by lazy { searchView.findViewById<ImageView>(R.id.search_button) }
searchView.setOnClickListener { searchButton.callOnClick() }
This way you get the same behaviour no matter where you click and you don't need to manually set the iconified property in either the xml or programatically.
What you ask can be done as follows
For the first click in the bar we use the method onClick of the SearchView to expand it.
public void onClick(View v) {
SearchView searchView = (SearchView) v;
searchView.setIconified(false);
}
When it doesn't have the focus then we collapse the SearchView.
Also for the next clicks on the bar, onClick just doesn't work, so we used onFocusChange to expand SearcView too.
final SearchView searchView = findViewById(R.id.searchView);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
searchView.setIconified(false);
}
if (!b) { // Not have focus
if (searchView.getQuery().toString().length() < 1) {
searchView.setIconified(true);
}
searchView.clearFocus();
}
}
});

Categories

Resources