I have this intent
Button share = (Button)findViewById(R.id.share);
share.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//createShareIntent( );
}
});
and these two methods in the class:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.layout.menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
myShareActionProvider = (ShareActionProvider)item.getActionProvider();
myShareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
myShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"testing");
return shareIntent;
}
public void doShare(Intent shareIntent)
{
// When you want to share set the share intent.
myShareActionProvider.setShareIntent(shareIntent);
}
and this sdk version configuration:
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15"/>
But my problm is that I do not know how to call this. How do I actually get the menu to show up after the share button is clicked?
Thanks!
You can open an option menu from your code by calling openOptionsMenu() method. From the doc here it does-
Programmatically opens the options menu. If the options menu is
already open, this method does nothing.
But the question is why are you doing so. You have hardware menu button to open option menu. So it is better to leave that so. Even If you want to show user some option by button click then use Dialog. Why option menu?
Related
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.
After clicking a button to open a new Activity using this method:
setContentView(R.layout.activity_comunidades01);
The second Activity still display the same menu as the previous one.
I have already readed serval methods to fix it as the one related in here:
Android: How to enable/disable option menu item on button click?
But I discovered that the methods to intialize and create the menu are never called. I even try to follow this other link without success:
onCreateOptionsMenu is never called
I even deleted all the items in the menu.xml for this activity but still displaying the previous activity options.
I also clarify I am using android 4.4 as target API but level 10 as a minimum one since some devices that will be used are running android 2.3.
My second Activity is like this:
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_SecondActivity);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
System.out.println("EN ON PREPARE OPTIONS MENU");
(menu.findItem(R.id.sincronizar)).setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
System.out.println("EN ON CREATE OPTIONS MENU");
getMenuInflater().inflate(R.menu.SecondActivity, menu);
return true;
}
}
To start second activity use this:
Button bt = (Button)findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
//finish(); //if you want to close FirstActivity after showing SecondActivity
}
});
After clicking a button to open a new Activity using this method:
setContentView(R.layout.activity_comunidades01)
This is not how you open another activity!
This way you only change the content of the current activity, nothing else.
Use Activity.startActivity() or Activity.startActivityForResult() as described in the docs to start another activity.
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);
}
}
I'm interested about how is possible create a shortcut to different items of settings in android devices.
Specifically to advances settings of mobile data.
I want to do an app that allows user a quick access to switch menu 2g-3g.
I know that the app dosen't do switch, I only want the shorcut to this menu.
I don't know how It's made!
I'm very glad if somebody help me!
Thanks a lot!!!
public class MainActivity extends Activity implements OnClickListener
{
Button boton;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton = (Button) findViewById(R.id.boton);
boton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v)
{
if(v.getId() == R.id.boton)
{
startActivity(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS));
}
}
}
I do this code by the explanation of Seraphim but dosen't work.
To switch to the 2G/3G settings page use this code inside the Button click handler:
startActivity(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS));
or others, look at:
http://developer.android.com/reference/android/provider/Settings.html
Other interesting are:
android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS
android.provider.Settings.ACTION_NETWORK_OPERATOR_SETTINGS
android.provider.Settings.ACTION_WIRELESS_SETTINGS
android.provider.Settings.ACTION_APN_SETTINGS
i would like to suggest u that refer this three link(FIRST,SECOND,THIRD) and u would be able to change most of the settings by ur own . ANDROID SETTINGS mostly stores there respective values to this three tables.
If you want to enable or disable GPS than use
Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, true);
I use this example
http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider
But, i want to now, if its possible change the text "see all" for other text, for example, my gallery app, have the text in spanish "ver todo", if possible to change?
And.. if possible know when the share button in the action bar (in my case actionbarsherlock) are press?
i see "However, if the action provider provides a submenu of actions, then your activity does not receive a call to onOptionsItemSelected() when the user opens the list or selects one of the submenu items" but..no other way to know it? I want that when the user press that "button" make one accion also show the list of app to share.
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater= getSupportMenuInflater();
inflater.inflate(R.menu.menu_resultados, menu);
MenuItem item = menu.findItem(R.id.menu_compartir);
mShareActionProvider =(com.actionbarsherlock.widget.ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent ()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_EMAIL,"TestText");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "TestSubject");
return shareIntent;
}
Try setting setOnMenuItemClickListener for ex:
MenuItem item = menu.findItem(R.id.menu_compartir);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
});
You can change the string at actionbarsherlock 's project, by changing lines at res/values/abs__strings, or you could create a folder res/values-es, copy there abs__strings, and edit the lines there. That way, you would have both english and spanish strings on your code, depending on device's language. The line you're looking for in abs__strings is called abs__activity_chooser_view_see_all.
Just a tip, try using the search function in Eclipse for these string changing next time. I found this out by searching "See all...".
About knowing when the button has been pressed, I couldn't figure it out. I just tested that onOptionsItemSelected() does not work, just as you said...