Share icon not showing in ActionBar - android

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.

Related

Hide action bar 3 dots but display items

I am having an issue where i have 2 items in my action bar (one 'refresh' button and one 'Save' Button, but for some reason they do not show, instead they are nested inside an options menu (3 dots). Would anyone know how to remove the 3 dots menu and display my 2 items? I have tried many things but ultimately I just end up removing all three items. Thanks in advance.
Here is my code
add_event_action.xml (this is my menu xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_save"
android:showAsAction="always"
android:title="#string/save"/>
</menu>
Here is my Java class
public class RandomActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_list);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_event_action, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
.show();
break;
// action with ID action_settings was selected
case R.id.action_save:
Toast.makeText(this, "Save selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
}
If I understood correctly, you need two menu buttons in your toolbar.
This works for me, place it in your menu.xml:
<item
android:id="#+id/done"
android:title="#string/done"
app:showAsAction="always|withText"/>
Try to use app:showAsAction instead of android:showAsAction
To expand on the other answers somewhat:
When your activity extends from AppCompatActivity, your menu items should use app:showAsAction. When your activity does not (that is, when you're not using the AppCompat support libraries), your menu items should use android:showAsAction.
Documentation: http://tools.android.com/tips/lint-checks
AppCompatResource
Summary: Menu namespace
Priority: 5 / 10
Severity: Error
Category: Correctness
When using the appcompat library, menu resources should refer to the
showAsAction in the app: namespace, not the android: namespace.
Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute.

ShareActionProvider from support.v7 not clicking

I am trying to get a working "share" button on the title bar. Initially, I followed the advice on developers.android.com for this, but got an exception saying that I needed to use MenuItemCompat. I am doing so, but now my share button won't even "click".
My relevant code is:
public boolean onCreateOptionsMenu(Menu menu) {
Log.d("CYCLE:","onCreateOptionsMenu()");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_to_do_list, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
setShareIntent();
mShareActionProvider.setShareHistoryFileName("my_share_history.xml");
return true;
}
public void setShareIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "some text to share");
mShareActionProvider.setShareIntent(shareIntent);
}
and my menu item in XML is:
<item android:id="#+id/action_share"
android:title="Share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
am I missing something to make this button be clickable? (I am not even talking about functionality yet. Simply the ability to be clicked (you know, like with the clicking/tapping animation).
thanks.
Have you set share Intent? Without the share Intent, Android would not know what exactly need to be shared by the provider.

create a right side menu in Android actionbar

I have an action bar with a drawer on the left. I want to add another menu on the right side of the action bar. It can be three dots or a button or anything else. Is there any way to do this?
Thanks :)
For achieving 3 dots menu in your actionBar, in your activity (which extends AppCompatActivity or ActionBarActivity), you override the creation of options menu like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
Where this R.menu.your_menuis a resource item present in your res/menu folder. One sample menu resource file content
<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="your_package.your_activity">
<item
android:id="#+id/action_edit"
android:orderInCategory="100"
android:title="#string/action_edit"
app:showAsAction="collapseActionView"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="101"
android:title="#string/action_settings"
app:showAsAction="collapseActionView"/>
</menu>
This will show a menu with both the options collapsed by default. To listen for clicks on these menu items, you override onOptionsItemSelected and perform the necessary action
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//Do something
...
return true;
case R.id.action_edit:
//Do something else
...
return true;
}
return super.onOptionsItemSelected(item);
}
You can create a layout for your ActionBar and then use something like the following in your activity:
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);
And in your R.layout.custom_action_bar create the buttons you need.

Android SearchView over android launcher icon?

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.

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