I have create a new app and I want to show the SHARE button on the right side of the top bar. It used to work with previous apps but in this last one isn't displaying the share icon and I don't know why:
res/menu/main.xml
<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" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/share"
android:showAsAction="always"
android:title="#string/action_share"
android:icon="#android:drawable/ic_menu_share"
/>
</menu>
MainActivity.java
#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 boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.share:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
shareIntent.putExtra(Intent.EXTRA_TEXT, "text");
startActivity(Intent.createChooser(shareIntent, "Share Via"));
break;
default:
break;
}
return true;
}
I don't know if this is important, but I have this target on the manifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
Any help is REALLY appreciated.
Related
I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click. I don't know why and searched for information but it doesn't help me.
This my XML code profile__detail_menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:icon="#drawable/ic_baseline_more_vert_24"
android:title="">
<menu>
<item
android:id="#+id/change_name"
android:title="Change name"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
</item>
</menu>
This my Java code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.profile__detail_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
Toast.makeText(Profile.this,"Something", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.change_name:
Toast.makeText(this, "change_name selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.new_photo:
Toast.makeText(this, "new_photo selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.log_out:
Toast.makeText(this, "log_out selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The thing which I feel is wrong is your menu XML file. For some reason you have a menu tag inside a menu tag. So there is sort of a sub menu if you get my point? But you're not inflating that actually.
Modify the XML as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/change_name"
android:title="Change name"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
I have updated your menu as per you code. If you need, add more items to it.
I added Share App and Rate us links to Action bar.It shows nicely.
When click on Rate us ,it will open Play store link.It's OK.
But when click on Share App,it will open sharing dialog and App store link also.
I want to disable opening App store link when click Share App.
Another problem,
When click back button in app store page,it will go to Play store.But I want to go back to my app.
How to solve my 2 issues...?please help me
Here is my Java code.
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, String.format(getString(R.string.txt_share_me), "http://play.google.com/store/apps/details?id=" + this.getPackageName()));
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
case R.id.id_rateus:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=PackageName")));
}
return true;
}
Here is my Menu code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_share"
android:orderInCategory="100"
android:title="#string/action_share"
android:textAllCaps="false"
app:showAsAction="always" />
<item
android:id="#+id/id_rateus"
android:orderInCategory="100"
android:title="#string/action_rateus"
android:textAllCaps="false"
app:showAsAction="never" />
</menu>
Add break; after your share case and
Use
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + this.getPackageName())));
To access your app via the play store
As I am new to Android Studio I have one question to ask here and I hope You guys don't mind.
So I created action menu that should have 3 doots at right upper end and that should call menu.
Anyway here is my menu:
In my main activity I am able to see menu but I am not able to see title and 3 doots.
Code of my main.java class (LocationInit):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.GoogleMaps:
Intent googlemaps = new Intent(this, googlemaps.class);
startActivity(googlemaps);
break;
case R.id.Settings:
Intent settings = new Intent(this, SettingsActivity.class);
startActivity(settings);
break;
case R.id.LastLocation:
Toast.makeText(getApplicationContext(),"Help", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
Finally my app_bar.xml (XML Of action bar):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/CustomToolbarStyle"
app:popupTheme="#style/CustomPopupTheme">
</android.support.v7.widget.Toolbar>
main_menu.xml
code :
<?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/menu_main_setting"
android:icon="#drawable/ic_settings"
android:orderInCategory="100"
app:showAsAction="always"
android:actionLayout="#layout/toolbar"
android:title="Setting" />
<item
android:id="#+id/menu_main_setting2"
android:icon="#drawable/ic_settings"
android:orderInCategory="200"
app:showAsAction="always"
android:actionLayout="#layout/toolbar"
android:title="Setting" />
</menu>
and just override onCreateOptionsMenu like this in your MainPage.java
#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, menu);
return true;
}
and in onCreate() add this
toolbar.inflateMenu(R.menu.main_manu);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In my android app I am having a menu item logout.I wanted to add another menu item My Panel which redirects into admin panel page gmumbai.co.in/admin.How to accomplish this.Please help me with a sample code.Thanks in advance.
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_overview, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Intent intent;
switch (item.getItemId()) {
case R.id.action_logout:
app = ((MyApplication) getApplicationContext());
app.logOut();
intent = new Intent(overview.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
overview.this.startActivity(intent);
overview.this.finish();
return true;
case R.id.action_MyPanel:
Uri uri=Uri.parse("http://gmumbai.co.in/admin");
overview.this.startActivity(new Intent (Intent.ACTION_VIEW,uri));
overview.this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
<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="com.gmumbai.gvendor.overview">
<item android:id="#+id/action_logout" android:title="#string/action_logout"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/action_MyPanel" android:title="#string/action_MyPanel"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
For adding a menu entry, try this:
res/menu/main_menu.xml
<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=".MainActivity">
<item
android:id="#+id/action_logout"
android:icon="#drawable/ic_logout"
android:title="Logout"
app:showAsAction="always|collapseActionView" />
<item
android:id="#+id/action_redirAdmin"
android:icon="#drawable/admin_console"
android:title="Admin Console"
app:showAsAction="always|collapseActionView">
</item>
</menu>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_logout:
//Logout
return true;
case R.id.action_redirAdmin:
//Goto AdminConsole
//startActivity(new Intent(MainActivity.this, AdminConsole.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If you are trying to display a webpage in your view, make sure you have a WebView in AdminConsole.java
For adding WebView, try this:
admin_console_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="#+id/adminConsoleWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
</RelativeLayout>
AdminConsole.java
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_console_activity);
mWebView = (WebView) findViewById(R.id.adminConsoleWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://gmumbai.co.in/admin");
}
I've used a pretty standard implementation of the ShareActionProvider in my actionbar and it works fine. However, when I start another activity from an actionbutton in the actionbar and return from that activity the dropdownmenu of the SharedActionProvider is automatically opened.
This behavior doesn't occur when I execute the exact same code to open another activity from a normal button.
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/menu_btn_my_scedule"
android:icon="#drawable/ic_menu_my_calendar"
android:title="To Scedule"
android:showAsAction="ifRoom" />
<item android:id="#+id/menu_btn_share"
android:title="Share.."
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_set_scedule"
android:icon="#drawable/ic_menu_today"
android:title="Set Scedule"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_settings"
android:icon="#drawable/ic_menu_manage"
android:title="Settings"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_feedback"
android:icon="#drawable/ic_menu_manage"
android:title="Feedback/Question"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_about"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
The other menu methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.optionsmenu, menu);
MenuItem actionItem = menu.findItem(R.id.menu_btn_share);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
actionProvider.setShareIntent(createShareIntent());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return false;
case R.id.menu_btn_my_scedule:
Intent i = new Intent(ONTTOptions.this, ONTTShowScedule.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_set_scedule:
i = new Intent(ONTTOptions.this, ONTTScedule.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_settings:
i = new Intent(ONTTOptions.this, ONTTPreferences.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_feedback:
showFeedbackDialog();
return true;
case R.id.menu_btn_about:
i = new Intent(ONTTOptions.this, ONTTAboutActivity.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
default: return super.onOptionsItemSelected(item);
}
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + ONTTConfig.PACKAGE_NAME);
return shareIntent;
}
I must have missed something somewhere, thanks for any help.
I found the problem. I thought android:actionProviderClass="android.widget.ShareActionProvider"
was also needed on every actionbar item I wanted in the overflow menu. I simply removed it on every item except the share button ofcourse.
My menu xml now looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_btn_my_scedule"
android:icon="#drawable/ic_menu_my_calendar"
android:title="To Scedule"
android:showAsAction="ifRoom" />
<item android:id="#+id/menu_btn_share"
android:title="Share.."
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_set_scedule"
android:icon="#drawable/ic_menu_today"
android:title="Set Scedule"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_settings"
android:icon="#drawable/ic_menu_manage"
android:title="Settings"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_feedback"
android:icon="#drawable/ic_menu_manage"
android:title="Feedback/Question"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_about"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction="collapseActionView" />
</menu>