I'm writing a recipe book, and I've encountered a problem - when I click my menu item, which is supposed to send email, with email address and subject pre-filled, nothing happens ...
Any ideas why ???
public class recipedisplayscreen extends Activity {
TextView EmailAddress;
TextView EmailSubject;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipedisplayscreen);
TextView MethodDisplay = (TextView) findViewById(R.id.textView3);
TextView IngredientsDisplay = (TextView) findViewById(R.id.textView5);
Intent i = getIntent();
String Ingredients = i.getStringExtra("textView1");
String Method = i.getStringExtra("textView2");
Log.e("recipedisplayscreen", Ingredients + "." + Method);
MethodDisplay.setText(Method);
IngredientsDisplay.setText(Ingredients);
EmailAddress=(TextView) findViewById(R.id.textView2);
EmailSubject=(TextView) findViewById(R.id.textView4);
ActionBar actionBar = getActionBar();
setTitle(R.string.title);
actionBar.setDisplayHomeAsUpEnabled(true);
setTitle(Method);}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onOptionsItemSelected1(MenuItem recipe_suggest) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ EmailAddress.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.recipe_menu1, menu);
return true;
}
}
I believe this is what you want :D delete onOptionsItemSelected1 and replace onOptionsItemSelected with this, this is assuming that recipe_suggest is the menu item's id?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.recipe_suggest:
//Other menu item I believe
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ EmailAddress.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
What a switch does is match the object in the "switch" to the case, the case that matches the "switch" gets carried out, so if the menu item id (MenuItem item; item.getItemId()) matches the ID either android.R.id.home or R.id.recipe_suggest (I assume you are using the action bar as you are referring to the Android R file?) this is how it should be done to the best of my knowlegde :)
Because you have two onOptionsItemSelected? Well, one is called onOptionsItemSelected*1* , need to merge that into the real one.
You can not just create a method called onOptionsItemSelected1() and expect that android calls it when you hit the menu entry to send the mail.
Merge this method to the original onOptionsItemSelected() and put it in the right case, than it should work.
Why do you have a separate method called onOptionsItemSelected*1* ?
All the code reacting to action items should be in onOptionsItemSelected :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case android.R.id.email_action_item:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
(...)
default:
return super.onOptionsItemSelected(item);
}
}
Related
This is my ActionBar, it has two buttons:
private void showActionBar() {
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.activity_main_actions, null);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(v);
}
I used this method to show buttons. I called this method in onCreate.
Now i want when i click on Any button which is in action bar New activity open.
For example i have AskActivity.java and MessageActivity.java
now when i click on ASK button AskActivity.java opens.
Is this possible?
I have used this but its not working.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_ask:
Intent i = new Intent(getApplicationContext(), AskActivity.class);
startActivity(i);
return true;
case R.id.action_message:
Intent ij = new Intent(getApplicationContext(), MessageActivity.class);
startActivity(ij);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I think it's because the onOptionsItemSelected method is related to MenuItem and not the CustomView. The two buttons are not option menu items, they are buttons inside the layout activity_main_actions. You have two choices - either create a new on click listener, as follows:
Button action_ask = (Button) findViewById(R.id.action_ask);
action_ask.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
// do something
}
}
Or, use the on click attribute method:
<Button
android:id="#+id/action_ask"
...
android:onClick="actionAskClicked" />
And then inside your Activity:
public void actionAskClicked() {
// do something
}
Same for the other button action_message. Hope this helps.
You need to create a method to open an activity from menu button click:
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_ask:
startActivity(AskActivity.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void StartActivity(Class<?> cls) {
Intent intent = new Intent(activity, cls);
activity.startActivity(intent);
}
Here is my problem:
Create a MainActivity. Add a button which will start another activity SecondActivity.
Intent i = new Intent(getActivity(),SecondActivity.class);
startActivityForResult(i,0);
Inside the SecondActivity, I capture the back button click event and also add a button to return to the first Activity.
When back button in action bar is clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
//finish();
return false;
}
return super.onOptionsItemSelected(item);
}
When the button inside activity is clicked:
Button btn = (Button)this.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
The onActivityResult in MainActivity is called when I click the button inside the SecondActivity, but it's never been called if I click the back button in Actionbar of SecondActivity. Can anybody tell me why? Thanks
Here is the code which is working:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I guess the finish() will close the current Activity, and return true inform that action has been processed. (The default back action seems to be different from finish().)
Try this:-
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Good answer is Gopal Rao code in the same question. Its worked for me. This is a copy of his solution:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent result = new Intent((String) null);
result.putExtra("SOME_CONSTANT_NAME", true);
setResult(RESULT_OK, result);
finish();
return true;
}
else {
return super.onOptionsItemSelected(item);
}
}
im having problem implementing the menu to share my app. When ever i open my activity that has the icon share_button, the dialog box "share via" is displayed immediately. I reckon i have problem with this line of code "startActivity(Intent.createChooser(shareIntent(), "Share..."));"
here is my code
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareHistoryFileName(null);
// 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);
startActivity(Intent.createChooser(shareIntent(), "Share via"));
return true;
}
menu item
<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_share"
android:actionProviderClass="android.widget.ShareActionProvider" />
Change you code like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareHistoryFileName(null);
return true;
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_item_share:
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);
startActivity(Intent.createChooser(shareIntent(), "Share via"));
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
onCreateOptionsMenu will call when your activity will call.
You need to start the share intent when Share button pressed`
So call share Intent in onOptionsItemSelected
You have to move this line of code startActivity(Intent.createChooser(shareIntent(), "Share via")); out into a button click handler or menu select handler.
The function onCreateOptionsMenu() will be called to setup the menu and that will happen when your Activity starts up.
I am having problems with the ShareActionProvider. I like to share the current url of an webview (url can be changed dynamically via JS) via a ShareActionProvider. To do this I overwrote onOptionsItemSelected to change the Intent via setShareIntent. But when I click on the ShareActionProvider and share it via a specific application, nothing happens. I debugged it and I found out that onOptionsItemSelected is not triggered. Can somebody help me out?
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.game, menu);
MenuItem mShareActionProviderItem = (MenuItem) menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) mShareActionProviderItem.getActionProvider(); // is a field
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
mShareActionProvider.setShareIntent(sendIntent); //needed to be able to click on Item
mShareActionProviderItem.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl();
/*ShareButton set URL*/
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url));
mShareActionProvider.setShareIntent(sendIntent);
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_share:
/*URL holen*/
String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl();
/*ShareButton mit URL bestücken*/
ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url));
mShareActionProvider.setShareIntent(sendIntent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
I know it's a bit late but I just had the same issue and I solved it by defining a OnShareTArgetSelectedListener on my ShareActionProvider object. :
mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {
#Override
public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) {
return false;
}
});
http://developer.android.com/reference/android/widget/ShareActionProvider.html#setOnShareTargetSelectedListener(android.widget.ShareActionProvider.OnShareTargetSelectedListener)
I'm trying to create an android context menu (the one that pops-up when you press the 'menu' button'). I've read all the tutorials I could find and nothing helped. I'm new to android developing.
I've created the menu.xml file but I don't understand how to give functionality to ID's. This is how my code looks:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The thing that I don't get: what to do with 'newGame();' and 'showHelp();'. I wish that when I click on a menu button a new activity starts. How do I do it?
First thing is you have code is for option menu not for context menu
you can call new activity like below
You can directly call a new activity without using option menu by
Intent myIntent = new Intent(this, NewGame.class);
startActivity(myIntent);
if you want to give option to user on press menu button then try below code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "New Game");
menu.add(0, 1, 1, "Help");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle().toString.equalsIgnoreCase("New Game")) {
Intent intent = new Intent(this, NewGame.class);
startActivity(intent);
finish();
}
else if(item.getTitle().toString.equalsIgnoreCase("Help")) {
Toast.makeText(getBaseContext(), "Help", 2000).show();
}
}
Intent intent = new Intent(this, NewGame.class);
startActivity(intent);
Have you read about how activity works?
This starts the activity NewGame
Intent myIntent = new Intent(this, NewGame.class);
startActivity(myIntent);