Share button with ShareActionProvider added twice on Action Bar [duplicate] - android

This question already has an answer here:
ActionBar share item produces "Android System" thingy
(1 answer)
Closed 7 years ago.
I have an issue with a Share Button in the Action Bar.
The screenshot:
As you can see, there are two icons on the right. But I only added one icon (the first) which is inactive. The second one is active, and do what I want (share the content).
As a result, I'd just want the first icon with the behavior of the second one.
Where does the second icon come from ??!!
The menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/animation_detail_share"
android:title="#string/share"
android:showAsAction="ifRoom"
android:actionProviderClass=
"android.widget.ShareActionProvider"
/>
</menu>
The fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_inplace_animation_details, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.animation_detail_menu, menu);
MenuItem item = menu.findItem(R.id.animation_detail_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
if(mShareActionProvider != null) {
mShareActionProvider.setShareIntent(Intent.createChooser(sendIntent, getResources().getText(R.string.share)));
}
}
I inflate the menu only inside the fragment, not in the activity.

I had two issues here:
createChooser() creates the new icon
Had to add setShareHistoryFileName(null); to prevent showing another icon.
So the final code looks like:
...
mShareActionProvider.setShareHistoryFileName(null);
mShareActionProvider.setShareIntent(sendIntent);
...

Related

Remove Toolbar items on runtime

I have a Toolbar within my app which I would like to modify it contents dynamically, during the app execution (in other words, on run-time).
For example, the app is capable of taking and previewing photos; once that photos are previewed, the user is able to select some photos and perform a sending action to a server. I want also to make the user able to delete photos once that some of them are selected and for doing that I would like that the "delete" item on the action bar (identifiable via the trash icon) will be visible only when one or more photos are selected.
Is this possible to do?
If yes, how?
In other words and, more generically, I want to control items (visibility) in the toolbar, setting the code as they will be "visible" only when some conditions are "true" (in the example above, when photos are selected or, in a different example, when user is logged) and invisible when they are "false" (when user isn't logged).
For now I just need to "remove" (or make invisible) items in the Toolbar, but it will be useful also to know if is possible to add items in the toolbar on run-time.
I'm adding some code which can help to understand the problem.
app_bar.xml file in "/res/layout", which graphically defines the Toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:theme="#style/ToolbarTheme" />
menu_resources.xml file, which defines the Toolbar items
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "User favourite function", should appear as action button if possible -->
<item
android:id="#+id/action_app_icon"
android:icon="#mipmap/ic_launcher"
android:title="#string/action_bar_app_icon"
app:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_delete"
android:icon="#drawable/trash"
android:title="#string/action_bar_delete"
app:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:icon="#drawable/settings"
android:title="#string/action_bar_settings"
app:showAsAction="never"/>
<!-- About, should always be in the overflow -->
<item
android:id="#+id/about"
android:icon="#android:drawable/ic_dialog_info"
app:showAsAction="never"
android:title="#string/action_bar_about"/>
Part of the activity in which the toolbar is
public class myClass extends AppCompatActivity implements View.OnClickListener {
// Instantiating a toolbar
private Toolbar toolbar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_class);
// Adding toolbar to the activity
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
}
// The method that creates an options menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_resource, menu);
// This make the delete item invisible
menu.findItem(R.id.action_delete).setVisible(false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// Perform the settings action
return true;
case R.id.about:
// Perform the about
return true;
case R.id.action_delete:
deletePhotos();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public static void manageSelection(Boolean state, int position){
if (photoArray.getNumberSelected() == 0) {
// disable the trash icon and its functionality;
} else {
// enable the trash icon with its functionality;
}
}
// This method allows to deleteItems images to the array
public void deletePhotos() {
//code for deleting photos
}
}
Thanks for your time.
Try this code in your activity please:
public class ActivityClass extends AppCompatActivity {
MenuItem menuItem; // Make global toolbar's menuItem
.
.
.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
menuItem = menu.findItem(R.id.toolbar_action_button) // Your toolbar´s button ID and save it in your global menuItem
return super.onCreateOptionsMenu(menu);
}
public void showMenuItem(){
menuItem.setVisible(true); // Call this method in runtime when you need show it
}
public void hideMenuItem(){
menuItem.setVisible(false); // Call this method in runtime when you need hide it
}
[EDIT]
Thanks to Alessandro Iudicone´s comment we have an alternative way to get the toolbar´s menu too without the global MenuItem but only global Toolbar instance:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
return super.onCreateOptionsMenu(menu);
}
public void showMenuItem(){
toolbar.getMenu().findItem(R.id.toolbar_actio‌​n_button).setVisible‌​(true);
}
public void hideMenuItem(){
toolbar.getMenu().findItem(R.id.toolbar_actio‌​n_button).setVisible‌​(false);
}
Hope it helps :)
For hiding the icon:
toolbar.findViewById(R.id.menu_item_id).setVisibility(View.INVISIBLE);
For adding a view to the toolbar:
TextView textView = new TextView(MainActivity.this);
textView.setText("Hello World");
toolbar.addView(textView);
This only works for views on the toolbar itself, not for the overflow menu. You'll probably have to use code found in this stack overflow answer if you want to mess with the overflow menu: Android Toolbar overflow menu view id

How to add Search View on Action bar on one of the fragments in tab layout dynamically?

Please provide me a better solution if you have I am working on android app in which on home Activity by using TabLaout&ViewPager i have added 5 tabs on below
Home
Social
Notice
Search
Links
I have set for MainActivity in values/style but i have placed custom action bar tag in search.xml. I know how to add SearchView on action bar in activity but i am facing issues using fragments. My goal is just to show search view widget on action bar when user will on "Search" tab. On other tabs i don't want to show. Further more I have added
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"/> in Manifest file in tag and setHasOptionsMenu(true);
in Search Fragment. But action bar is showing on search screen without search View Widget, please correct me where i am doing wrong.
You have to add the SearchView only in the searchFragment:
searchFragment class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_news, container, false);
...
setHasOptionsMenu(true);
.....
return rootView ;}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
try {
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
// do your search
return false;
}
#Override
public boolean onQueryTextChange(String s) {
// do your search on change or save the last string or...
return false;
}
});
}catch(Exception e){e.printStackTrace();}
}
menu_search 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">
<item android:id="#+id/action_search"
android:icon="#drawable/action_search_btn"
android:title="Search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

Want to add share button symbol on my app

Hi I have done following in my app
I am able to share my content but it comes from menuinflator not the symbol
Image shows that menuinflator has share button
If i click on share button ,I can share , But I want have the share symbol ,How to do it ?
I have read the following link
https://developer.android.com/training/sharing/shareaction.html
but symbol doesn't comes .
My code is below
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Menu xyz;
TextView t1 ;
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.t1 = (TextView)findViewById(R.id.textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_filexml, menu);
menu.getItem(0).setVisible(true);
this.xyz = menu;
// Return true to display menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share :
String code = " ";
code = this.t1.getText().toString();
Intent sendIntent = new Intent();
sendIntent.setAction("android.intent.action.SEND");
sendIntent.putExtra("android.intent.extra.TEXT", code);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu resource file (menu_filexml.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
This code allows me to work as given in images but sharing symbol is not coming
you need to use custom view for your menu item. just create an layout file and name it "my_item_custom_view" and put your icon as 'ImageView' in it then assign the layout file like below:
<item
android:id="#+id/menu_share"
android:title="share"
app:showAsAction="always"
app:actionLayout="#layout/my_item_custom_view"/>

Android Sherlock Actionbar onOptionsItemSelected is not called

I m using Sherlock Actionbar to show share option in Action Bar of my application.
The Share option works fine.
I want to get the option which user selects from Share option, so that I can share
different text for facebook, messages, mail etc.
For this I am using onOptionsItemSelected function but this function
is never called.
Please help me to fix this or is there any workaround
to achieve this. Thanks..
public class MainActivity extends SherlockActivity {
private ShareActionProvider mShareActionProvider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getSupportMenuInflater().inflate(R.menu.items, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
Intent intent = getDefaultShareIntent();
if(intent!=null)
mShareActionProvider.setShareIntent(intent);
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
intent.putExtra(Intent.EXTRA_TEXT,"Sample Content !!!");
return intent;
}
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
System.out.println("Testing...................");
return false;
}
}
Items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/share"
android:title="#string/share"
android:showAsAction="always"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
/>
</menu>
For this I am using onOptionsItemSelected function but this function is never called.
It is not supposed to be called. It is not called for any action provider.
I want to get the option which user selects from Share option, so that I can share different text for facebook, messages, mail etc.
That is not directly supported by ShareActionProvider.

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