How to add multiple activities under one onCreateOptionsMenu(Menu menu) - android

I want to add multiple activities under one `onCreateOptionsMenu(Menu menu) in my android app, I have already added two activities and they are working fine but third activity isn't working, following is my code
onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareURL();
}
if(item.getItemId() == R.id.menu_item_refresh){
mWebView.reload();
return true;
}
if(item.getItemId() == R.id.share_this_app)
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onOptionsItemSelected(item);
}
private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This Website!"));
shareIntent.setPackage("com.whatsapp");
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
return intent;
}
menu_main.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:title="#string/share"
android:id="#+id/menu_item_share"
android:showAsAction="always"
android:icon="#drawable/share"
/>
<item
android:id="#id/menu_item_refresh"
android:title="Refresh"
android:showAsAction="never"
android:icon="#drawable/refresh"
/>
<item
android:id="#+id/share_this_app"
android:title="Share this app"
android:showAsAction="never"
android:actionProviderClass="android.widget.ShareActionProvider"/>
From above, menu_item_share and menu_item_refresh is working, but Share this app isn't working.

It's probably your are missing a return true statement below this line mShareActionProvider.setShareIntent(getDefaultShareIntent());. You can simply follow a clean structure to achieve your task.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share: {
shareURL();
break; //or, return true;
}
case R.id.menu_item_refresh: {
mWebView.reload();
break; //or, return true;
}
case R.id.share_this_app: {
mShareActionProvider.setShareIntent(getDefaultShareIntent());
break; //or, return true;
}
return super.onOptionsItemSelected(item);
}

Hey I think this would help...
You have written
android:showAsAction="never".
So it would generate an error while putting an icon inside the menu icon(three dots at toolbar).
Hence put the value "always" or "ifRoom" for this attribute.
Hope this would work :-)

Related

Opening new activities from Popup menu items

I'm having a popup menu on click of action bar button. When i click on the action bar button I'm getting my popup window. But i want to open another activities on clicking the popup menu items. How could i do that?
Following are my code snippets.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi") #Override
public boolean onOptionsItemSelected(MenuItem item) {
View menuItemView = findViewById(R.id.action_button);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.popup);
popupMenu.show();
return true;
}
and my popup menu is as follows,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<item
android:id="#+id/one"
android:title="About"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
<item
android:id="#+id/two"
android:title="Contact Us"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
</menu>
What i want to do is, when i click on these menu items another activities has to be opened. How could i do that?
Can someone help me please. Thanks in advance.
Use the id to launch the activity using switch statement with menu itemId
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.one:
Intent intent1 =new Intent(this,ActivityOne.class);//firstActivity
startActivity(intent1);
return true;
case R.id.two:
Intent intent2 =new Intent(this,ActivityTwo.class);//second Activity
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try this
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),
item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
To open activity on popup menu click:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}

Action Bar items not working when clicked

I'm having an unusual issue here. My action bar was working properly then i went an tested it now and it totally stop. When pressed, they give no response. One is a back button and the other is a send button. Both of which aren't working. Here is my code for the Menu
ActivityOne.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_send, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
super.onBackPressed();
return true;
case R.id.action_send:
new PostUpLoad().execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_send.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ActivityOne">
<item
android:id="#+id/action_send"
android:orderInCategory="100"
android:title="#string/send"
android:icon="#drawable/ic_send"
app:showAsAction="ifRoom" />
</menu>
Everything seems fine but they aren't working at all. Any help would be greatly appreciated.
Replace R.id.home with android.R.id.home.
You're missing breaks in your switch case, and I'm not sure about returning true in onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_send, menu);
return super.onOptionsItemSelected(item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
super.onBackPressed(); //might use finish() instead
break;
case R.id.action_send:
new PostUpLoad().execute();
break;
default:
super.onOptionsItemSelected(item);
}
}

Adding an Easy Share Action in ActionBar

I have problem. I was follow a lot of guides, but still can't get my Share button to work. I got the icon showing in ActionBar, but when I press nothing is happening when android:showAsAction="always". But when android:showAsAction="never" it works. I just want share icon to be always shown in ActionBar. What I am doing wrong? Please help
Here is my code:
public class Main extends Activity {
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.shareButton);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.shareButton:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Check it out";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
return true;
case R.id.aboutButton:
Intent intent = new Intent(this, About.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here 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/shareButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="#string/share"
android:showAsAction="always"/> **---> when I put here "NEVER" then it works! But I want Share to be always as icon**
<item
android:id="#+id/aboutButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="never"
android:title="#string/about_dev"/>
</menu>
If you didnĀ“t find a solution try this to get the ShareActionProvider
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share)
.getActionProvider();
mShareActionProvider.setShareIntent(doShare());
return true;
}
And doShare() would be:
public Intent doShare() {
// populate the share intent with data
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Put whatever you want");
return intent;
}
Try "Intent share = new Intent (Intent.ACTION_SEND)" instead.
You seem to be doing everything right!
In
case R.id.shareButton:
instead of using
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
use:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
mShareActionProvider.setShareIntent(sharingIntent);
Refer the following links for more information:
https://developer.android.com/reference/android/widget/ShareActionProvider.html
https://developer.android.com/training/sharing/shareaction.html#set-share-intent

Android ActionBarSherlock On Top Icon Back

I'm trying to make my top action bar icon to allow users to go back to previous screen. I tried to implement these codes. But none are working. Can anyone please guide me on this. I know this looks simple, I'm new to android . Below are my codes.
Problem : When i tap on the icon button it just cleared my screen without going to the previous screen.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_item);
checkInternetConnection();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //<--THIS
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, SingleViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the way I do it:
#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, main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
return true;
}
In your ressources(res)
go to menu
and add
this make sur u have some button back for in your drawable
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/back"
android:icon="#drawable/back1"
android:showAsAction="always|withText"
android:title="back"/>
now in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
case R.id.back:
Intent in = new Intent(this, <classname which you want to go back>.class);
startActivity(in);
break;
}
return false;
}
you may try this code
<item
android:id="#+id/back"
android:icon="#drawable/btn_back"
android:showAsAction="always|withText"
android:title="#string/txt_back"/>
>
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
//return super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case R.id.back:
back_action();
return true;
default:
break;
}
return true;
}
>

Android create drop down menu

Does anyone know how I can create some sort of drop down or pop up on a page so that i can then go to any page from the page that i am on ?
For example i have a menu page but when i select an option i want to be able to navigate to another page instead of going back to the menu
public class PopUpMenu extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popupmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.location:
Intent intent1 = new Intent(getApplicationContext(), MyLocation.class);
startActivity(intent1);
return true;
case R.id.search:
Intent intent2 = new Intent(getApplicationContext(), Search.class);
startActivity(intent2);
return true;
case R.id.add:
Intent intent3 = new Intent(getApplicationContext(), AddSite.class);
startActivity(intent3);
return true;
// case R.id.help:
// Intent intent4 = new Intent(getApplicationContext(), Help.class);
// startActivity(intent4);
// return true;
case R.id.exit:
Intent intent5 = new Intent(Intent.ACTION_MAIN);
intent5.addCategory(Intent.CATEGORY_HOME);
intent5.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent5);
default:
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/location" android:title="My Location" />
<item android:id="#+id/search" android:title="Search" />
<item android:id="#+id/add" android:title="Add Site" />
<item android:id="#+id/help" android:title="Help" />
<item android:id="#+id/exit" android:title="Exit" />
</menu>
First of all, here is a good tutorial for you: Activity Testing
You have a couple of samples on the developers.android site.
The most comprehensive is the API Demos which includes a wide specter of tools and techniques.
Then, for your purpose, the widget is named Spinner, and a demo of it is Spinner Test.
The menu (in XML):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/food" android:title="Food" />
<item android:id="#+id/other" android:title="Other" />
</menu>
To set the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
To handle switching activities:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.food:
Intent intent1 = new Intent(getContext(), Food.class);
startActivity(intent1);
return true;
case R.id.other:
Intent intent2 = new Intent(getContext(), Other.class);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Categories

Resources