So, I want to make a query to JSON with "SearchView" in Actionbar but SearchView is for Search as the name suggests.
I mean to get the symbolic appearance and the possibility to transfer data from them after the assignment of items to the variables (EditText, Button)
I searched the web on this subject but found nothing.
I have basic layout with this view :
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/action_search"
android:showAsAction="always|collapseActionView"
android:title="Search"/>
</menu>
Java :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
inflater.inflate(R.menu.search_in_menu, menu);
return true;
}
Any suggestions?
(I'm a beginner)
Related
Here is what my SearchView looks like using Android Support Library version 23.0.4:
And here is what it looks like using Support Library 24.0.0:
How can I get my SearchView to properly show text in 24.0.0? This is not a text color issue FYI, I have adjusted text color of the EditText to no avail. You can also see it is missing the "x" button on the right.
Update
Here is the XML I am using for my search menu:
<?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">
<item
android:id="#+id/search"
android:icon="#drawable/nav_icon_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
Here is the code I am using in my onCreateOptionsMenu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_location_list, menu);
this.configureSearchView(menu);
super.onCreateOptionsMenu(menu, inflater);
}
private void configureSearchView(Menu menu) {
MenuItem search = menu.findItem(R.id.search);
MenuItemCompat.setOnActionExpandListener(search, this);
this.mSearchView = (SearchView)
MenuItemCompat.getActionView(search);
this.mSearchView.setOnQueryTextListener(this);
this.mSearchView.setOnCloseListener(this);
this.mSearchView.setSubmitButtonEnabled(false);
this.mSearchView.setIconifiedByDefault(true);
this.mSearchMenuItem = search;
}
I was trying to create a menu, and add some actions "ifRoom" using code below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/search_description" />
<item
android:id="#+id/menu_refresh"
android:title="#string/menu_refresh"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/menu_preference"
android:title="#string/menu_preferences"
android:showAsAction="never"
/>
</menu>
But what i got is the menu is displaying at the bottom...
I want it to be at top of the screen...
Any idea what happened here? Thanks!
Screenshot link: http://i.stack.imgur.com/knECg.png
Below is the MainActivity.java onCreateOptionaMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
// Moved from onCreate -- Retrieve the Search View and configure/enable it
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()) );
return true;
}
In my android app .. I am trying t put a search edittext with seARCH icon .. but I cant see the search icon in action bar rather the title is seeing in overflow. I am giving my code below
activity
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:title="Settings"/>
</menu>
MainActvity
public class MainActivity extends ActionBarActivity
{
WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv1 = (WebView) findViewById(R.id.wView1);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.loadUrl("http://www.example.com/songs/");
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
Use Search Manager n search view in onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
and in your main.xml add this item for search
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
I think you must return `true` instead of
super.onCreateOptionsMenu(menu)
in `onCreateOptionsMenu(Menu menu)` method
Excuses. That was wrong.
I think this problem has something to do with the base class you are using. Your are subclassing ActionBarActivity instead of Activity. This means that you are using compatibility for previous versions. For this to work, you need to set your application's Theme to Theme.AppCompat (or a similar one), and also include the Support Library. Look here
I just realized how to fix the problem!
your menu xml file should have an starting tag like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myApp="http://schemas.android.com/apk/res-auto" >
you can replace
myApp
with any names you want, NOW the important part is when you define an action item:
<item
android:id="#+id/actionitem_switch_view"
android:title="#string/view_as"
android:showAsAction="always"
myApp:showAsAction="always"
android:icon="#drawable/ic_action_view_as_list"/>
You'd have two showAsAction tags,one for API<11 devices and one for the others,
Look closely that the two showAsAction tags are different,one is from Android namespace and the other's from the tag you defined in menu Starting point
I would like to create an always-expanded search window in my action bar. I am using the ActionBar Sherlock library. According to some samples I have found, this code should do the trick
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
SearchView searchView = (SearchView) searchItem.getActionView(); //returns null
searchView.setIconifiedByDefault(false);
return true;
}
however, getActionView(); returns null.
Is there some other way to get to the searchView, so that I can call the setIconifiedByDefault(false); method ?
I would like to keep the definition of the searchView in the xml file as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_bar_search"
android:title="#string/action_bar_search"
android:icon="#drawable/ic_magnify"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
Thanks for any help
Your issue is probably with your XML. Change android:actionViewClass="android.widget.SearchView" to android:actionViewClass="com.actionbarsherlock.widget.SearchView"
Are you sure that you're using correct R.menu... link? Also try to use getMenuInflater(); instead of getSupportMenuInflater();
Hi the search widget in the action bar of my application is not displaying the default text for some reason. I am sure I might be missing something.
The code looks like below:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
try{
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
}catch(Exception e){
Log.v(TAG,"inflated"+e.getMessage()+"--"+e.getClass()+"-"+e.getClass());
}
return true;
}
menu/activity_main
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="#string/search_title"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
values/strings.xml
<resources>
<string name="search_title">Search Data</string>
</resources>
The search widget is working perfectly , the only thing is the default text "Search Data" is not showing..
Can someone tell me what am I doing wrong
Try to use this
searchView.setQueryHint(getResources().getString(R.string.search_title));