Android - Simple Pop up Menu

Submitted by: 
Operating System: 
Visitors have accessed this post 987 times.

In this tutorial we will try to create a Simple Pop Up Menu using Android. This simple app can be used when choosing a list and display its value. The android is an open source operating system it's free and user friendly to mobile developers. Android is available to any devices such as TV, phones, watches etc. So now let's do the coding.....

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simplepopupmenu.MainActivity">
  11.  
  12. <TextView
  13. android:id="@+id/tv_title"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="What is your favorite Programming Language"
  17. android:layout_centerHorizontal="true"
  18. android:textSize="30dp"
  19. android:layout_marginTop="150dp"
  20. android:gravity="center"/>
  21. android:id="@+id/btn_change"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_centerHorizontal="true"
  25. android:layout_marginTop="20dp"
  26. android:layout_below="@+id/tv_title"
  27. android:text="Select an option" />
  28. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplepopupmenu">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

Creating the List

This is where we store the list of all the data that we need. To do this right click on the res select new the choose directory. After that create a new xml file namely programming.xml inside the newly created directory. Then write these block of codes inside the file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="<a href="http://schemas.android.com/apk/res/android">
  3. " rel="nofollow">http://schemas.android.com/apk/res/android">
  4. </a> <item
  5. android:id="@+id/c"
  6. android:title="C" />
  7. <item
  8. android:id="@+id/cplus"
  9. android:title="C++" />
  10. <item
  11. android:id="@+id/csharp"
  12. android:title="C#" />
  13. <item
  14. android:id="@+id/java"
  15. android:title="Java" />
  16. <item
  17. android:id="@+id/php"
  18. android:title="PHP" />
  19. <item
  20. android:id="@+id/vb"
  21. android:title="VB" />
  22. <item
  23. android:id="@+id/python"
  24. android:title="Python" />
  25. <item
  26. android:id="@+id/ruby"
  27. android:title="Ruby" />
  28. </menu>

The Main Function

This code contains the main function of the application. This code will display the data within the external xml file to display inside the button property. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.

  1. Button btn_change;

Finally, write these block of codes inside the onCreate method to run the application.

  1. btn_change = (Button)findViewById(R.id.btn_change);
  2.  
  3. btn_change.setOnClickListener(new View.OnClickListener() {
  4. @Override
  5. public void onClick(View v) {
  6. PopupMenu popupMenu = new PopupMenu(MainActivity.this, btn_change);
  7. popupMenu.getMenuInflater().inflate(R.menu.programming, popupMenu.getMenu());
  8. popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  9. @Override
  10. public boolean onMenuItemClick(MenuItem item){
  11. Toast.makeText(MainActivity.this, "I Like " + item.getTitle(), Toast.LENGTH_SHORT).show();
  12. return true;
  13. }
  14. });
  15.  
  16. popupMenu.show(); }
  17. });

Try to run the app and see if it worked.

There you have it we have created a Simple Pop up Menu using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.