ShareActionProvider set a different text for Twitter - android

I try to use ShareActionProvider. It's work but i want put a different text when the user click on Twitter.
How can i fix it?
This my code, the object News contents a title and a url
When he clicks on Twitter :
#account_name myText
And for other
myText
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail_news, menu);
MenuItem item = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) item.getActionProvider();
shareActionProvider
.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
shareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent() {
// Here, is Twitter, add "#Televesdre" in the message
News news = detailAdapter.getCurrentItem(viewPager.getCurrentItem());
String message = news.getTitle() + "\n\n Article: " + news.getUrl();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
return shareIntent;
}
I search how to do before coding because i haven't no idea how to do

Try ShareActionProvider.OnShareTargetSelectedListener

Related

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.

Adding a "share" button to share the app on social networks

I have an app, and I'd like to add a share button to it. Once the button is clicked, I'd like it to open the following window:
Then the user will choose where to share it and it will display the following default message:
"Just found this great app! Find it here: https://play.google.com/store/apps/details?id=com.ideashower.readitlater.pro"
Solution 1: Launch ACTION_SEND Intent
When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
# change the type of data you need to share,
# for image use "image/*"
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, URL_TO_SHARE);
startActivity(Intent.createChooser(intent, "Share"));
Solution 2: Use ShareActionProvider
If you are just looking to add a Share button in Overflow Menu, also have a look at ShareActionProvider.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share, menu);
MenuItem item = menu.findItem(R.id.share_item);
actionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
String shareText = URL_TO_SHARE;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(shareText).getIntent();
actionProvider.setShareIntent(shareIntent);
return true;
}
Hope this helps. :)
As explained on Android Developers on this link: http://developer.android.com/training/sharing/shareaction.html
you have to add this menu item:
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
Then add the following code in Activity:
private ShareActionProvider mShareActionProvider;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.share_menu, 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 true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}

How to remove Messenger Icon from Action Bar

I Action Bar i am using ShareActionProvider, but facing small issue, i am also getting Messenger Icon along with Share Icon, and i don't want to show Messenger Icon in my Action Bar.
So how can i remove Messenger Icon from Action Bar, see my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(getDefaultShareIntent());
}
return super.onCreateOptionsMenu(menu);
}
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, "TEXT");
return intent;
}
I just want to show Share Icon in my Action Bar not Messenger Icon...
see screen shot:
You are welcome to try setShareHistoryFileName(null) and see if that blocks the second icon along with the history.
Otherwise, you will need to write your own action provider as a replacement for ShareActionProvider. Or, you will need to attempt to fork ShareActionProvider to block this second icon.
Create a menu in layout in this way:
<item
android:showAsAction="always"
android:icon="#drawable/share_icon"/>
When you click on it you can show the default sharing sources.

Using Android action bar share intent

I'm using a menu item on the action bar and I want to share my app by clicking the share icon. When I click the share icon it doesn't work. Also, I want to add text saying "install this app" when shared.
Here is my code:
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
Mainpage.xml menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:icon="#drawable/ic_store"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
If you want a static share Intent (i.e., it never changes), then you update your onCreateOptionsMenu to be
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" +
getPackageName();
String yourShareText = "Install this app " + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
you can get the official tutorial here http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider
In the snappet you paste, you forget to call
mShareActionProvider.setShareIntent(intent);

how do i get my ShareActionProvider to submit text Dynamically (from listeners) view pagers etc

I am using Sherlock Library Action Bar, which is similar to the ICS action bars, the place where I am stuck is this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Save")
.setIcon(R.drawable.ic_compose).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Search")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
SubMenu sub = menu.addSubMenu("Options");
sub.add(0, SubMenu.NONE, 0, "First");
sub.add(0,SubMenu.NONE, 1, "Second");
sub.add(0, SubMenu.NONE, 2, "Three");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// HERE IS WHere I AM FACING PROBLEM IN
getSupportMenuInflater().inflate(R.menu.share_action_provider, menu);
// Set file with share history to the provider and set the share intent.
actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); //this is BRILLIANT WAY TO AVOID REPEATation
actionProvider.setShareIntent(createShareIntent());
return super.onCreateOptionsMenu(menu);
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
//THIS TEXT IS WHAT I WANT TO OBTAIN DYNAMICALLY
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Something goes here");
return shareIntent;
}
Whenever I try putting the actionProvider.setShareIntent(createShareIntent()); inside a listener or anywhere other than the actionProvider.setShareIntent(createShareIntent()); I get D/View(11753): onTouchEvent: viewFlags is DISABLED
I wish to include this sharing Action provider and want to input my own text that gets generated on user inputs.
Any inputs are welcome.
Adding this doesn't work
actionItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.e("THIS IS NEVER CALLED", "??");
return true;
}
});
How do I get an update for the Share button being clicked
I hit a similar issue with a android.support.v7.app.ActionBarActivity derivative (though I think this approach should work for any activity) and I overrode onPrepareOptionsMenu and got the effect you were after:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
if (actionProvider != null) {
actionProvider.setShareIntent(createShareIntent());
}
return super.onPrepareOptionsMenu(menu);
}
i use menu like this:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.your_share_button_name:
share();
return true;
}
}
private void share() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"your text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"your subject");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
that's all. I suggest you to put all the layout of the menu in a xml in your res/menu folder.

Categories

Resources