I know that this question has been asked before . I tried some of the solutions but nothing is working for me .
What I want in landscape mode is for the searchview to be fully stretched in width and I dont want the keyboard to appear on full screen which makes the UI invisible.
And I want the searchview to be stretched in width
The code below is menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/Search_Box"
android:title="Search"
app:showAsAction="always"
android:icon="#drawable/search_white_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
And here is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
MenuItem item= menu.findItem(R.id.Search_Box);
searchView = (SearchView) MenuItemCompat.getActionView(item);
if(text!=null && !text.isEmpty())
{
// MenuItemCompat.expandActionView(searchView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
item.expandActionView();
Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
}
else {
MenuItemCompat.expandActionView((MenuItem) searchView);
}
searchView.setIconified(false);
searchView.setQuery(text,false);
searchView.clearFocus();
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
please help me where i am wrong
Related
I tried to add android:title=""
that doesn't work.
SearchView searchView = (SearchView) miFind.getActionView();
searchView.setQueryHint("your message");
didn't work as well.
private SearchView.OnQueryTextListener onSearch() {
return new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
WebView mWebView= (WebView) findViewById(R.id.webView);
mWebView.loadUrl("http://www.example.com" +query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
};
If you have defined in your menu an item with this attribute:
app:actionViewClass="android.support.v7.widget.SearchView"
then you must get it in code in order to change its hint.
In onCreateOptionsMenu() method, add this code after you inflate the menu:
MenuItem miFind = menu.findItem(R.id.miFind);
SearchView searchView = (SearchView) miFind.getActionView();
searchView.setQueryHint("your message");
replace miFind with the id of the menu item which acts as your SearchView.
Edit: In onCreateOptionsMenu() add this listener:
miFind.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
searchView.setQueryHint("your message when expanded");
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
searchView.setQueryHint("your message when collapseded");
return true;
}
});
If you are talking about the search hint, you need use this method from SearchView: setQueryHint
From the documentation:
Sets the hint text to display in the query text field. This overrides
any hint specified in the SearchableInfo.
This value may be specified as an empty string to prevent any query
hint from being displayed.
Example:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.SearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionSearch|flagNoExtractUi"
app:searchHintIcon="#null"
app:searchIcon="#null"
app:queryBackground="#null"
app:submitBackground="#null"/>
</android.support.v7.widget.Toolbar>
Java:
final SearchView searchView = findViewById(R.id.search_view);
searchView.setQueryHint("Your search string");
I have created one custom action view layout using MenuItemCompat Java code is as shown in code below :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem shoppingCartitem = menu.findItem(R.id.my_action_item_id);
shoppingCartitem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("MyAPP", "Listener called");
return true;
}
});
MenuItemCompat.setActionView(shoppingCartitem, R.layout.my_custom_action_view_layout);
return super.onCreateOptionsMenu(menu);
}
menu_main.xml as follows :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/my_action_item_id"
android:icon="#drawable/ic_shopping_cart_white_24dp"
android:orderInCategory="9999"
android:title="#string/menu"
myapp:showAsAction="always" />
</menu>
What am I missing here, any suggestions ?
From the documentation:
Set a custom listener for invocation of this menu item. In most situations, it is more efficient and easier to use onOptionsItemSelected(MenuItem) or onContextItemSelected(MenuItem).
So you can simply override the onOptionsItemSelected callback, which is easier.
By the way to me your code seems to be right, but it doesn't do anything. Have you tried doing something like Log.v("MyAPP", "Listener called"); inside it?
EDIT:
I wrote this code:
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.my_action_item_id);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("MyApp", "Inside listener");
return true;
}
});
MenuItemCompat.setActionView(item, R.layout.main_activity);
return super.onCreateOptionsMenu(menu);
}
}
main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/my_action_item_id"
android:icon="#drawable/ic_launcher"
android:orderInCategory="9999"
android:title="Menu"
myapp:showAsAction="always" />
</menu>
And this is the result of the LogCat (I clicked the icon twice):
As you can see it works without problems
please try
MenuItem shoppingCartitem = menu.findItem(R.id.my_action_item_id);
change to
MenuItem shoppingCartitem = (MenuItem) findViewById(R.id.my_action_item_id);
I've been searching for half an hour and none of the solutions for other people are working for me.
Here is what is shows in the preview to the right:
Here is the java code in my activity (I removed the other stuff):
public class PlayGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_game);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_playgame);
myToolbar.setTitle("ChessClock");
myToolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(myToolbar);
}
#Override
public void onBackPressed() { } //Back Button Disabled
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbaroverflow, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_pause:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
All the instructions said to make an XML in my 'menu' directory but I didn't have one so I made it (called 'menu' inside of 'res'). The XML 'actionbaroverflow' has this code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_pause"
android:orderInCategory="100"
android:title="Pause"
app:showAsAction="always"/>
And the item is simply not showing up when I run the app. I had an icon but someone said it could have been too big, so I just made it text and it's still not showing up.
Tou need to add to your R.menu.actionbaroverflow
android:icon="#drawable/your_icon"
Will be something like this:
<item
android:id="#+id/action_pause"
android:icon="#drawable/your_icon"
android:orderInCategory="100"
android:title="Pause"
app:showAsAction="always"/>
This works for me fine:
<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:activity=".ProductsNewOrder">
<item android:id="#+id/action_pause"
android:title="Pause"
android:visible="true"
app:showAsAction="always"
android:icon="#drawable/ic_pause"/>
</menu>
and in the activity:
setSupportActionBar(myToolbar);
if (myToolbar!= null) {
myToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_pause: {
//do something
break;
}
}
return true;
}
});
}
and where i inflate the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbaroverflow, menu);
return true;
}
Hope it helps!!!
i'm developing an application with API 8. now i'm trying to add 3 items in my menu, one
of item i added is SearchView, i used app:actionViewClass="android.support.v7.widget.SearchView" ofcourse to support the API 8,
and in my MainActivity class in onOptionsItemSelected(MenuItem item) i'm trying to handle the click on searchView, but it is no longer working.
how can fix this?
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">
<!--My SearchView--->
<item
android:id="#+id/menu_search"
android:orderInCategory="100"
android:title="#string/stringSearch"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_magnify"
app:showAsAction="always"/>
<!--My Filter--->
<item
android:id="#+id/menu_filter"
android:orderInCategory="100"
android:icon="#drawable/ic_filter"
android:title="#string/StringFilter"
app:showAsAction="always" />
<!--Some menu--->
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:icon="#drawable/ic_dots"
android:title="#string/Settings"
app:showAsAction="always" />
</menu>
MainActivity.class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//I setup the searchView here
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
....
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
//This is click event on searchView but unfortunately not working
Toast.makeText(getApplicationContext(),"SearcView",Toast.LENGTH_LONG).show();
break;
case R.id.menu_settings:
Toast.makeText(getApplicationContext(),"Menu Settings",Toast.LENGTH_LONG).show();
break;
case R.id.menu_filter:
Toast.makeText(getApplicationContext(),"Menu Filter",Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
So, as you can see in my code, i try to fire a toast when the menu item is clicked, everything is working but not my SearchView.
this is happen when i started to add app:actionViewClass="android.support.v7.widget.SearchView" on menu in searchView item.
anyone can help me?
Thank you very much!
You'll want to use one of the listeners on the SearchView class. The OnSearchClickListener looks like it may be the right fit for you.
Edit. Here's a code example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle clicks
}
});
return super.onCreateOptionsMenu(menu);
}
I am using ActionBarSherlock Tabs with Fragments in my applications:
public class ExampleActivity extends SherlockFragmentActivity{}
I have added an Options Menu in the Action Bar. The problem is while this does show the options menu on the click of virtual button in the action bar, it doesn't do so when the physical button is pressed. I would like the options menu to be displayed when the physical menu button is pressed.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//return super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_prefs:
Intent i = new Intent(this,ShowSettingsActivity.class);
startActivityForResult(i, requestCode);
return true;
case R.id.menu_faq:
startActivity(new Intent(this, AboutApp.class));
return true;
case R.id.menu_contact:
startActivity(new Intent(this, FeedbackApp.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And in res/menu/options_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_settings_dark"
android:orderInCategory="100"
android:showAsAction="always">
<menu
android:id="#+id/e">
<item
android:id="#+id/menu_prefs"
android:title="#string/menu_prefs"/>
<item
android:id="#+id/menu_faq"
android:title="#string/menu_faq"/>
<item
android:id="#+id/menu_contact"
android:title="#string/menu_contact"/>
</menu>
</item>
</menu>
If I remove the sub-menu, it displays the options menu on click of physical button.
(Sorry about what my first answer that was off the point)
I don't think that the <menu> XML element accepts android:id as an attribute. Did you tried removing it ?
Another try :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true; // override the useless returned value by super()
}
Try this code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
Hope this will help you.