1

I can give link via Button.But I want to add a App store link to "rateus" in menu item.(please see attached image)here is the button code in MainActivity.java.This is not working for menu item.please help me.

//rateus button
        android.widget.Button ratebutton = (android.widget.Button) findViewById(R.id.id_rateus);
        ratebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            android.net.Uri.parse("market://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
                }catch (android.content.ActivityNotFoundException e){
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            android.net.Uri.parse("https://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
                }
            }
        });
//end rateus button code

here is my menu item image... rate us ite menu

enter image description here

here is the code for rate us item

<item
        android:id="@+id/id_rateus"
        android:orderInCategory="100"
        android:title="@string/action_rateus"
        android:textAllCaps="false"
        app:showAsAction="ifRoom" />
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mr.Soft
  • 143
  • 2
  • 14

2 Answers2

0

This is how you can handle a menu item click

  1. Inflate the menu file

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.menu_file, menu);
     return true;
    }
    
  2. Then handle onCLick here

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.id_rateus:
     //handle onclick event for intent to playstore
     return true;
    default:
     return super.onOptionsItemSelected(item);
     }
    }
    
0

You need to override the menu functions, something like the code below.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds appModels to the action bar if it is present.
    this.menu = menu;
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
        case R.id.share:

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBodyText = "Check it out. Your message goes here";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
            startActivity(Intent.createChooser(sharingIntent, "Shearing Option"));
            return true;

        case R.id.id_rateus:
            try {
                    startActivity(new Intent(Intent.ACTION_VIEW,                                
android.net.Uri.parse("market://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
                }catch (android.content.ActivityNotFoundException e){
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            android.net.Uri.parse("https://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
                }

            return true;
    }

    return super.onOptionsItemSelected(item);
}
CodeChimp
  • 4,745
  • 6
  • 45
  • 61
  • Thanks for the answer..but I already have above code for adding to share app link like this link. https://www.viralandroid.com/2016/05/adding-share-action-to-android-app.html how to add your part into this.. – Mr.Soft Jun 21 '18 at 12:17
  • I've updated my code to show how the share and rateus menu items will be included in a switch statement – CodeChimp Jun 21 '18 at 12:25
  • Glad to have helped, please accept my answer if you are satisfied. – CodeChimp Jun 21 '18 at 12:46