0

How to customize the menus in this image. can anyone tell the solution

I want to display four submenus(FB, Google+, Twitter and SeeAll) If i Touch SeeAll a dialog has to popup which consist of more(FB, Google+, Twitter, Linkedin, NetLog, etc..,)

Code :

Menu.xml:-

   <menu xmlns:android="http://schemas.android.com/apk/res/android" >

       <item android:id="@+id/menu_item_share"
            android:showAsAction="ifRoom"
            android:title="Share"
            android:actionProviderClass="android.widget.ShareActionProvider" />

    </menu>

Activity:-

  public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
         MenuItem item = menu.findItem(R.id.menu_item_share);

         ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();

         Intent myIntent = new Intent();
         myIntent.setAction(Intent.ACTION_SEND);
         myIntent.putExtra(Intent.EXTRA_TEXT, "Whatever message you want to share");
         myIntent.setType("text/plain");


         myShareActionProvider.setShareIntent(myIntent);

         return true;
Namrata
  • 1,683
  • 1
  • 17
  • 28
PothiraJ
  • 65
  • 14

1 Answers1

1

Try This :-

      <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_location_found"
        android:clickable="true"
        android:icon="@drawable/ic_launcher"
        android:showAsAction="always"
        android:title="Share">
        <menu>
            <item
                android:id="@+id/facebook"
                android:orderInCategory="1"
                android:showAsAction="never"
                android:title="Facebook">
            </item>
            <item
                android:id="@+id/twitter"
                android:orderInCategory="2"
                android:showAsAction="never"
                android:title="Twitter">
            </item>
            <item
                android:id="@+id/gplus"
                android:orderInCategory="3"
                android:showAsAction="never"
                android:title="Google Plus">
            </item>
            <item
                android:id="@+id/seeall"
                android:orderInCategory="4"
                android:showAsAction="never"
                android:title="See All">
            </item>
        </menu>
    </item>

</menu>

Activity (Java Code):-

     public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

            @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.facebook:

            Toast.makeText(getApplicationContext(), "Testing", 1000).show();

            // write the code here, what you want the action when user click
            // facebook submenu....

            break;
        case R.id.twitter:

            break;

        case R.id.gplus:

            break;

        case R.id.seeall:

            break;

        default:
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Kotlin Code : -

class MainActivity:Activity() {
  protected fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  }
  fun onCreateOptionsMenu(menu:Menu):Boolean {
    val inflater = getMenuInflater()
    inflater.inflate(R.menu.main, menu)
    return super.onCreateOptionsMenu(menu)
  }
  fun onOptionsItemSelected(item:MenuItem):Boolean {
    when (item.getItemId()) {
      R.id.facebook ->
      Toast.makeText(getApplicationContext(), "Testing", 1000).show()
      R.id.twitter ->
      { //code
      }
      R.id.gplus ->
      { //code
      }
      R.id.seeall ->
      { //code
      }
      else -> return true
    }// write the code here, what you want the action when user click
    // facebook submenu....
    return super.onOptionsItemSelected(item)
  }
}
Namrata
  • 1,683
  • 1
  • 17
  • 28
  • ViewUserProfileActivity what should be content in the class ? – PothiraJ Mar 05 '14 at 09:27
  • hi.. i got the below error inside switch ** case action_twt cannot be resolved or is not a field ** – PothiraJ Mar 05 '14 at 09:49
  • can u help me in writing the code under case statements Just u refer this image http://thorbek.net/online/wp-content/uploads/2013/07/Screenshot_2013-07-21-19-41-33.png – PothiraJ Mar 05 '14 at 10:00
  • @PothiraJ: i have updated my answer.. and it is working well on my side.. hope your problem will b solve. :) rest ask me if you get stuck in nywhere.. – Namrata Mar 05 '14 at 10:20
  • Thank you bro.., Its working. When i click Facebook it has to take me to the facebook page instead of displaying toast and If i Touch SeeAll a dialog has to popup which consist of more(FB, Google+, Twitter, Linkedin, NetLog, etc..,) – PothiraJ Mar 05 '14 at 10:36
  • your welcome.. :) thats great.. :) Please accept the answer.. and upvote the answer too :) – Namrata Mar 05 '14 at 10:43
  • hello Bro : I hav accepted ur ans. plz help me to do this ** When i click Facebook it has to take me to the facebook page instead of displaying toast and If i Touch SeeAll a dialog has to popup which consist of more(FB, Google+, Twitter, Linkedin, NetLog, etc..,) ** – PothiraJ Mar 05 '14 at 11:00
  • 1
    dear for facebook you have write some code.. google it. you need some sdk for that... – Namrata Mar 05 '14 at 11:01
  • Follow this link, http://stackoverflow.com/questions/6869938/how-to-integrate-facebook-twitter-and-google-plus-to-an-android-app – Namrata Mar 05 '14 at 11:04
  • k dear., That popup ? If i touch seeall i wanna popup window., That window it consist of fb, g+, Twi ..etc.., can u help me in writing the code – PothiraJ Mar 05 '14 at 11:15
  • there are many methods to show popup.. you can display your activity as an dialog and also you can make a custom dialog for it.. – Namrata Mar 05 '14 at 11:17
  • k. These are the screen shoots https://drive.google.com/file/d/0B7LgS9eeYsU9N1I1cnk2czY0cFAwRXdWbXJpT1RnMC1mWlpN/edit?usp=sharing https://drive.google.com/file/d/0B7LgS9eeYsU9RnFlVi1aVWs1TnRNUUh6NkJhZEdxWENzaVZF/edit?usp=sharing – PothiraJ Mar 05 '14 at 11:27
  • kindly ans my question :When i click Facebook it has to take me to the facebook page, help me to write the code under case R.id.facebook: – PothiraJ Mar 05 '14 at 14:04
  • i have gave you link. did you follow it or did you google something which you want??? – Namrata Mar 06 '14 at 04:26
  • dear friend., How to share via google+., Can u send code.., – PothiraJ Mar 06 '14 at 10:10
  • Can anybody fix this person issue http://stackoverflow.com/questions/24551175/set-color-of-shareactionprovider. It was similar to this. – Ramesh_D Jul 03 '14 at 11:46