Android Action bar only showing in overflow - android

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

Related

Android Support Library 24.0.0 does not show text in SearchView

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;
}

How to collapse SearchView on backpressed using appcompat v7.?

I am using appcompat v7 for searchview with toolbar except actionbar. below is my menu xml file and java file.
menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_action_sort"
android:title="#string/sort"
app:showAsAction="ifRoom"/>
</menu>
java file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
now i want to collapse searchview if it is expanded otherwise want to work backpressed on backpressed() method. how can i achieve this.?
Collapsing on backpressed is handled by default in my own setup. I didn't implement a custom onBackPressed method. I did nothing special except extending from ActionBarActivity.
I used MenuItemCompat to get actionview, that might do the trick.
This is my menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.yourapp.youractivity">
<item
android:id="#+id/search"
android:title="#string/app_name"
android:icon="#drawable/nav_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
This is how i create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.shop_list, menu);
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
//Using MenuItemCompat here, that can do the trick
searchView =
(SearchView)MenuItemCompat.
getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
//etc...
//etc...
return true;
}
In onBackPressed call invalidateOptionsMenu() or store SearchView as Activity field and call searchView.onActionViewCollapsed(); but that can require additional work when restoring your Toolbar state (title state etc.).
Try this inside the Class and extend ActionBarActivity in your Class
(Example: - public class Home extends ActionBarActivity)
#Override
public void onBackPressed() {
super.onBackPressed()
}
For collapsing the SearchView: Try this,
menu.collapseActionView();
(or)
searchView = menuItem.getActionView().
(or)
searchView.onActionViewCollapsed()

Remove icon/title for actionbar with expanded SearchView?

I created a new project, targeting api 11 and above. I have an activity where I want a SearchView expanded by default:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setIconifiedByDefault(false);
}
which works, but I don't want any icon or title on the actionbar, just the searchview. I tried getting rid of my title and icon by doing:
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setIcon(android.R.color.transparent);
But my actionbar still has this padding to the left of the searchview:
Is there a way to get rid of it?
Thanks
-------- Edit --------------------
Here's my activity in full:
public class GreatAndroid extends FragmentActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem mi = menu.findItem(R.id.action_search);
mSearchView = (SearchView) mi.getActionView();
mSearchView.setIconifiedByDefault(false);
return true;
}
}
and here's the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="#string/search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
Even though the icon is transparent, it still takes up the space:
getActionBar().setIcon(android.R.color.transparent);
Remove this line and add:
getActionBar().setDisplayShowHomeEnabled(false);
Now, you should have the SearchView take up all the available space.
Action items that can be collapsed have a max width. The only workaround I can think of is the following:
public class GreatAndroid extends FragmentActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem mi = menu.findItem(R.id.action_search);
mSearchView = (SearchView) mi.getActionView();
mSearchView.setIconifiedByDefault(false);
// You can use Display.getSize(Point) from API 13 onwards.
// For API 11 and 12, use Display.getWidth()
final Point p = new Point();
getWindowManager().getDefaultDisplay().getSize(p);
// Create LayoutParams with width set to screen's width
LayoutParams params = new LayoutParams(p.x, LayoutParams.MATCH_PARENT);
mSearchView.setLayoutParams(params);
return true;
}
}
Although it looks like there's still space to the left, you can use mSearchView.setBackgroundColor(Color.RED); to see that there really isn't.
Regarding left padding you could try using collapseActionView
Here is an example menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search_hint"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
I could not get #Vikram's awnser to work, but I found that another answer from Stack Overflow:
Open your styles.xml file and add below codes in your Actionbar style
<item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
<item name="displayOptions">showHome|homeAsUp|showTitle</item>
<item name="android:icon">#android:color/transparent</item> <--this do the magic!
p/s: I'm using Actionbar Sherlock and this works just fine
(See Remove icon/logo from action bar on android work)

SearchView as query JSON

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)

Accessing actionView in the ActionBar

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();

Categories

Resources