Android : How to change the color of the SearchView widget and how to focus cursor and keyboard automatically?

on Friday, March 20, 2015


I have a SearchView in my menu. I did this so far:



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

mMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
showInputMethod(view.findFocus());
}
}
});
getSupportActionBar().setHomeAsUpIndicator(R.mipmap.menu);
showReadArticleMenu(false);
searchView.setBackgroundColor(getResources().getColor(R.color.test));
searchView.setIconifiedByDefault(true);


return true;


}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
Log.e("SearchRequested:",String.valueOf(onSearchRequested()));
searchView.requestFocus();



return true;
}
}


I also have overriden the onQueryTextChange and onQueryTextSubmit methods, since my Activity implements SearchView.OnQueryTextListener. Currently, how it works is: I click on the search icon in the action bar, it opens up the collapsible part, then I have to click on the grayish search widget that just appeared, then type in some text.


What I now need is two things:


1) to make the widget collapsible part white (the line, not the background), as well as the X marker to cancel it. (it's currently bluish-gray, and barely visible in my app style)


.2) I also need to automatically start the keyboard when I click on the search icon in the action bar, as well as erase the previous text from the widget. (so, when I click on the icon, I can automatically start typing the new text and just submit it via "search" or the equivalent in the keyboard)


0 comments:

Post a Comment