Issue with clicking Share APP link in Action bar - java

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

Related

I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click

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.

How to show Icon in drop down menu of PopupMenu

I want to add some icon in the drop-down menu of a popupMenu i created. please help me. I am so much bothered on how to create popupMenu to show dropdown menu items with icon when clicked. I have tried a lot of things which did not work. I tried to set showAsAction to always|withtext, but it didn't work.
Please help me. Any help will be much appreciated!
Below is the Java
showMenuButton = findViewById(R.id.btn_long_press);
//Init popup menu
final PopupMenu popupMenu = new PopupMenu(
this, //the context
showMenuButton //UI view where to click to show the popup menu
);
//add menu xml to our popup menu
popupMenu.getMenuInflater().inflate(R.menu.pop_menu, popupMenu.getMenu());
//handle popup menu item clicks
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
//get id of menu item clicked
int id = menuItem.getItemId();
//handle clicks
if (id==R.id.settings_menu){
//settings selected
Toast.makeText(MainActivity.this, "Settings Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main2Activity.class));
getTitleColor();
return true;
}
else if (id==R.id.manual_menu){
//Manual selected
Toast.makeText(MainActivity.this, "User Manual Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main3Activity.class));
return true;
}
else if (id==R.id.about_menu){
//about selected
Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main4Activity.class));
return true;
}
return false;
}
});
//handle button click to show menu
showMenuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popupMenu.show();
}
});
this is the menu XML
<item
android:id="#+id/settings_menu"
android:title="Settings"
android:icon="#drawable/ic_settings"
app:showAsAction="always|withText" />
<item
android:id="#+id/manual_menu"
android:title="User Manual"
android:icon="#drawable/ic_developer" />
<item
android:id="#+id/about_menu"
android:title="About"
android:icon="#drawable/ic_about"/>
I discovered from my research that to show drop-down popup menu with menu item icon takes deep process. And the process is not clean.
However, i have found a solution. Creating sub menu seems to be the only lee-way to get what we want. And I encourage anyone seeking to create menu icon to follow this way I founded for now while we wait further improvement from google.
<?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/action_more"
android:icon="#android:drawable/ic_menu_more"
android:orderInCategory="1"
android:title="More Options ยป"
app:showAsAction="always">
<menu>
<item
android:id="#+id/settings_menu"
android:title="Settings"
android:icon="#drawable/ic_settings"/>
<item
android:id="#+id/manual_menu"
android:title="User Manual"
android:icon="#drawable/ic_developer" />
<item
android:id="#+id/about_menu"
android:title="About"
android:icon="#drawable/ic_about"/>
</menu>
</item>
</menu>

how to replace settings item with imagebutton

I am developing a navigation drawer activity in android studio, in menu folder there is a settings item at the left corner. I want to remove it and add an image button over there in action bar. Can anyone tell me how to add that image button and how to perform click event on that button in main activity.
Check below image for reference -
I hope this helps, add this inside your menu.xml layout -
The showAsAction attribute allows you to define how the action is displayed. For example, the ifRoom attribute defines that the action is only displayed in the action bar if there is sufficient screen space available.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_settings"
android:title="Settings">
</item>
</menu>
just try this.
no need to add imagebutton you just need to add
android:showAsAction="always"
and icon as
android:icon="#drawable/Your_icon"
do this in the menu.xml file, in item setting
Navigate to main_menu.xml in your menu folder under res and modify it as
<?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/image_button"
android:icon="#android:drawable/ic_menu_search"
android:title="Clickme"
app:showAsAction="always" />
</menu>
In your MainActivity
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId == R.id.image_button){
//do action on imagebutton click
return true;
}
return super.onOptionsItemSelected(item);
}

Android app Share button not showing up

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.

Disable Android menu group

I try to disable a menu group with the following code, but it doesn't work, menu items are still enabled. Can you tell me what's wrong please?
res/menu/menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/a" android:title="A"></item>
<item android:id="#+id/b" android:title="B">
<menu>
<item android:id="#+id/c" android:title="C" />
<item android:id="#+id/d" android:title="D" />
<group android:id="#+id/group_1">
<item android:id="#+id/e" android:title="E" />
<item android:id="#+id/f" android:title="F" />
</group>
</menu>
</item>
</menu>
Java:
public boolean onPrepareOptionsMenu (Menu menu) {
menu.setGroupEnabled (R.id.group_1, false); // does not work
menu.setGroupVisible (R.id.group_1, false); // does not work either
return super.onPrepareOptionsMenu (menu);
}
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater ().inflate (R.menu.menu, menu);
return true;
}
Thanks to user432209's info, here is the answer:
menu.findItem (R.id.b).getSubMenu ().setGroupVisible (R.id.group_1, false);
I'm not sure if you can use a group like this, but try this (its worth a shot):
MenuItem item = menu.findItem(R.id.group_1);
item.setVisible(true);
item.setEnabled(false);
Edit: Your problem is your menu structure and how you create the menu inside onCreateOptionsMenu due to using parent/child menus.
You create a menu for the parent menu, not the child menu, so that is why the call to setGroupEnabled and setGroupVisible fail. You need to create an object in onCreateOptionsMenu that represents the child in order for that object to be passed into onPrepareOptionsMenu and your code to work.
The above-mentioned solution works well but it should be written within onCreateOptionsMenu, here is the example of it:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu
inflater.inflate(R.menu.menu, menu);
// Make the menu item visible and enable it.
MenuItem item = menu.findItem(R.id.group_1);
item.setVisible(true);
item.setEnabled(false);
}

Categories