Android SearchView over android launcher icon? - android

Is there any way that search view (after tap/click) expands over android launcher icon like over app title name?
Or maybe, is there a way to change my title to look like launcher icon (like on sreenshot) with some custom font..?
Screenshot:

Using this code inside of my onCreateOptionsMenu() worked:
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setMaxWidth(5000);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ActionBar actionBar = getActionBar();
if (actionBar != null)
actionBar.setDisplayShowHomeEnabled(false);
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
ActionBar actionBar = getActionBar();
if (actionBar != null)
actionBar.setDisplayShowHomeEnabled(true);
return false;
}
});
Note:
For me the Home Icon was not hidden by default as #MattT. suggested, so I hide it manually when the search is opened and show it again when the search is closed. Also note that getActionBar() requires API 14+.
If you're interested, before using searchView I inflate my custom layout:
getMenuInflater().inflate(R.menu.actionbar_items, menu);
Where the layout (menu/actionbar_items.xml) is this:
<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:actionViewClass="android.widget.SearchView"
android:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>

Here is an example I have used in the past to create that type of search view.
http://www.edumobile.org/android/android-development/action-bar-search-view/
Also the action bar should by default take your launcher icon unless you specifically set an android:logo attribute. You can also modify this programmatically using the setLogo() method on actionbar.

Related

Why is action icon not showing on actionbar

I develop android application and I want to create action placed on action bar that is visible as icon. I added a drawable with resolution of 18x18px and created menu with one item, which is my action, as shown below:
<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:context=".RelationsActivity">
<item
android:id="#+id/action_show_profile"
android:icon="#drawable/profile_icon_16"
android:title="#string/action_show_profile"
app:showAsAction="always" />
As you can see the showAsAction attribute is set to "always" and it is still not working. I tried with different AppThemes and nothing helped. There surely is enough space for the icon cuz action bar title is disabled:
getActionBar().setDisplayShowTitleEnabled(false);
The action is present in the overflow dropdown as if there is no space for the icon.
use this
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
And this method too
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.YOUR_MENU_FILE, menu);
return true;
}
implements AppCompatCallback
and use this code inside oncreate method
//let's create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)
AppCompatDelegate delegate = AppCompatDelegate.create(this, this);
//we need to call the onCreate() of the AppCompatDelegate
delegate.onCreate(savedInstanceState);
//we use the delegate to inflate the layout
//delegate.setContentView(R.layout.activity_sync_contact);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu);// icon of navigation
toolbar.setTitle("Inbox");
delegate.setSupportActionBar(toolbar);

Android: How to remove overflow menu icon tooltip?

In Android, when you onLongClick an overflow menu item icon, a tooltip of its title will appear.
I've tried setting the menu title to blank, but a blank tooltip will pop up.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_icon"
android:title="Icon Title"
android:icon="#drawable/ic_myicon"
app:showAsAction="always"/>
</menu>
Notes:
• I'm using app namespace because Android Studio is telling me to use it due to having Theme.AppCompat.Light.NoActionBar as my theme. I've tried android namespace. Doesn't work either.
• My menu is created in a Fragment with hasOptionsMenu(true).
• My ActionBar is a Toolbar.
How can I remove this tooltip if it's possible? Thanks!
Yes it is possible. In the java class remove the
public boolean onCreateOptionsMenu(Menu menu)
and the job will be done. If no remove this also from the java class
public boolean onOptionsItemSelected(MenuItem item)
the only way to do this is up is with android:showAsAction="withText" and set Title as =""
For onLongClickListener:
final MenuItem menuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});

Share icon not showing in ActionBar

I am trying to use ShareActionProvider as in http://developer.android.com/training/sharing/shareaction.html but my ActionBar does not show the share icon. Instead, the overflow button is shown which a menu item "share" (the rest works well). I am using this code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_item_share"
android:title="share"
compat:showAsAction="ifRoom|collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>
In my activity, I have:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_detail, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return super.onCreateOptionsMenu(menu);
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
And my theme is:
<style name="Theme.Base" parent="android:Theme.Light" />
<style name="AppTheme" parent="Theme.Base">
<item name="android:actionModeCloseDrawable">#drawable/ic_action_back</item>
</style>
Why is does the share icon not show up?
Some of your code (compat:showAsAction) is written to use the appcompat-v7 backport of the action bar.
Some of your code (getActionProvider(), Theme.Light) is written to use the native action bar.
Pick one and stick with it.
My guess is that more of your current code is set up for the native action bar, in which case changing compat: to android: in your menu XML resource will probably get your ShareActionProvider working.

How to add EditText to ActionBar for ListView filtering?

I have an activity with action bar and one icon on this ActionBar. When user press this icon I need to hide this icon and show EditText with cross to clear text. When user will done with it (choose item in ListView or press back button) I will need to show icon again.
Here is two related question:
1. How to add EditText on ActionBar for ListView filtering?
2. How to change ActionBar between EditText and icon?
Before click:
After click on 'plus' icon:
You need to add a SearchView widget to your application.
The official documentation is here:
http://developer.android.com/training/search/setup.html
There are also detailed instructions to add it.
searchmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
mainactivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu, menu);
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
setupSearchView();
return super.onCreateOptionsMenu(menu);
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnQueryTextListener(this);
mSearchView.setQueryHint("Search Here");
}
#Override
public boolean onQueryTextChange(String newText) {
//implement the filterng techniques
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
How to add EditText on ActionBar for ListView filtering?
Please refer to this answer by Commonsware . More clearly his sample project will do the trick.
How to change ActionBar between EditText and icon?
Simply override onOptionItemSelected and try to set the visibility of that menu item.
MenuItem item = menu.findItem(R.id.my_item);
item.setVisible(false);
Alternatively , If you want full control in your hands try to customize your action bar

Options Menu not displaying on ActionBar

I wanted to display Menu in ActionBar using ActionBarActivity.
I am getting options menu while clicking options button instead of displaying in the top of action bar.
I need the options menu at top instead of clicking the options menu from 2.3 onwards
My Code :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setSubtitle("mytest");
actionBar.setTitle("Testing");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_action, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_one:
Toast.makeText(this, "clicked search", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
My XML file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_one"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:title="#string/action_settings"/>
<item
android:id="#+id/menu_item_two"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>
Try it:
Create XML file on "../res/menu/menu_main.xml"
manu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:showAsAction="never"
android:icon="#android:drawable/ic_menu_settings"
android:title="#string/menu_settings"/>
</menu>
The option "never" on "showAsAction", important, if you put that the option always show on ActionBar.
Then now we must asociate this option on the MainActivity.java (class whatever you want).
MainActivity.java
public class MainActivity extends Activity {
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Now that we define the elements of our action bar is knowing how to respond to keystrokes made ​​by the user about them.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Log.i("ActionBar", "Settings!");;
//open Activity,Fragments or other action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If you have morer options on your menu only add item on XML menu_main.xml and on the principal class add other "case".
For Example:
MainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
Log.i("ActionBar", "Save!");;
//open Activity,Fragments or other action
return true;
case R.id.menu_settings:
Log.i("ActionBar", "Settings!");;
//open Activity,Fragments or other action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:showAsAction="never"
android:icon="#android:drawable/ic_menu_settings"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_save"
android:showAsAction="never"
android:icon="#android:drawable/ic_menu_save"
android:title="#string/menu_save"/>
</menu>
It is possible that this is because your device has an options button that would open the menu, so Android don't draw the launcher icon.
http://blog.vogella.com/2013/08/06/android-always-show-the-overflow-menu-even-if-the-phone-as-a-menu/
Try with return super.onCreateOptionsMenu(menu); instead of return true in your onCreateOptionsMenu method according to below android document...
http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
As per above android document...
The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above.
So, If you want ActionBar in android 2.3 then you have to use Android Support Library.
Edit:
Your menu XML would need to look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
>
<item android:id="#+id/item_menu_ok"
android:icon="#drawable/ic_action_ok"
android:title="#string/ok"
yourapp:showAsAction="always"/>
<item android:id="#+id/item_menu_cancel"
android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel"
yourapp:showAsAction="always"/>
</menu>
Replace yourapp with your app name or any namespace.
I'm not 100% sure, but it looks like your menu XML file should have a menu in it. In the menu, add the items. Like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_menu" >
<item...
<item...
</menu>
Perhaps this works.
There are also a few other things to remember in general. Android decides by itself what to show in the ActionBar (depending on what fits there and other parameters). What does not fit in the ActionBar goes into the overflow.
Do this in your oncreate()
ActionBar actionBar = getActionBar();
actionBar.show();
This should do what you want.
Action Bar
Ok, let's try another answer. If, for whatever reason, Android decides not to show the items in the action bar, then what your are seeing is indeed the correct behaviour. Items not in the action bar are shown in overflow.
One reason items don't show in the action bar is because they don't have a icon defined.
You do define an icon, as this,
android:icon="#drawable/ic_launcher"
and I suspect this is the problem. The launcher icon is of different size (larger) than the action bar items (smaller). Try defining the icon in correct size and see if it is displayed.
http://developer.android.com/design/style/iconography.html
Edit: There is an action bar icon pack to get you started so you can test to see if it works. Download it and use eclipse to import some icon..

Categories

Resources