Sunday, 14 June 2015

Popup menu | Drop down menu

33 comments
 In this tutorial we learn how to write a program for create a Drop down menu in android.It also known as Popup Menu. the Drop down menu is a one types of menu like Context menu,Option Menu etc. they can be used for settings like Delete,Search,Copy,Pest,Rename,Move,More etc.In this example we see how to create simple Drop down menu in android that contains various menu items.

Let's do fun with code:


CREATE NEW PROJECT
We need:

1) main.xml located here ...res/menu
1) activity_main.xml
2) MainActivity.java

 Step 1: Write code into main.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/one"
        android:title="Rename"/>
    <item
        android:id="@+id/two"
        android:title="Delete"/>
    <item
        android:id="@+id/three"
        android:title="Copy"/>

</menu>

Step 2: write code into activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btclickme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me" />


</LinearLayout>

Step 3: Write code into MainActivity.java

package dev.androidapplink.custommenuapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends Activity {
// create variable
Button btnclickme;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
btnclickme = (Button) findViewById(R.id.btclickme);

btnclickme.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// Create the instance of Menu
PopupMenu popup = new PopupMenu(MainActivity.this, btnclickme);
// Inflating menu using xml file
popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());

// registering OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
}

}

Step 4: Now Run Your Project:




33 comments :

  1. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai

    ReplyDelete
  2. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.

    Block chain training in bangalore

    ReplyDelete
  3. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    angularjs training in chennai

    ReplyDelete
  4. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism online training

    ReplyDelete
  5. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Devops Training in Chennai

    Devops Training in Bangalore

    Devops Training in pune

    ReplyDelete
  6. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.

    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  7. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS Training in Toronto | Amazon Web Services Training in Toronto , Canada

    AWS Training in New york city | Amazon Web Services in New York City


    AWS Training in London | Amazon Web Services Training in London , UK

    ReplyDelete
  8. It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  9. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    advanced excel training in bangalore

    ReplyDelete
  10. Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!

    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  11. After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  12. I Got Job in my dream company with decent 12 Lacks Per Annum Salary, I have learned this world most demanding course out there in the current IT Market from the Data Science Course in Bangalore Providers who helped me a lot to achieve my dreams comes true. Really worth trying.

    ReplyDelete
  13. Visit for Blockchain training in Bangalore:
    Blockchain training in Bangalore

    ReplyDelete
  14. Visit for Blockchain training in Bangalore:- Blockchain training in Bangalore

    ReplyDelete
  15. Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
    Angular js Training in Chennai

    Angular js Training in Velachery

    Angular js Training in Tambaram

    Angular js Training in Porur

    Angular js Training in Omr
    Angular js Training in Annanagar

    ReplyDelete
  16. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    amazon web services aws training in chennai

    microsoft azure training in chennai

    workday training in chennai

    android-training-in chennai

    ios training in chennai

    ReplyDelete
  17. Your courage in facing challenges is truly inspiring. You never back down from a difficult situation. Buy suit price in india from suit price in india

    ReplyDelete
  18. Young discover writer situation new admit social.religious

    ReplyDelete

Follow me Share