Android - Simple Pop up Menu

Operating System

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="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplepopupmenu.MainActivity">
  8.  
  9. <TextView
  10. android:id="@+id/tv_title"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="What is your favorite Programming Language"
  14. android:layout_centerHorizontal="true"
  15. android:textSize="30dp"
  16. android:layout_marginTop="150dp"
  17. android:gravity="center"/>
  18. android:id="@+id/btn_change"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_centerHorizontal="true"
  22. android:layout_marginTop="20dp"
  23. android:layout_below="@+id/tv_title"
  24. android:text="Select an option" />
  25. </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="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simplepopupmenu">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity"
  13. android:configChanges="orientation"
  14. android:screenOrientation="portrait">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17.  
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </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="http://schemas.android.com/apk/res/android">
  3. <item
  4. android:id="@+id/c"
  5. android:title="C" />
  6. <item
  7. android:id="@+id/cplus"
  8. android:title="C++" />
  9. <item
  10. android:id="@+id/csharp"
  11. android:title="C#" />
  12. <item
  13. android:id="@+id/java"
  14. android:title="Java" />
  15. <item
  16. android:id="@+id/php"
  17. android:title="PHP" />
  18. <item
  19. android:id="@+id/vb"
  20. android:title="VB" />
  21. <item
  22. android:id="@+id/python"
  23. android:title="Python" />
  24. <item
  25. android:id="@+id/ruby"
  26. android:title="Ruby" />
  27. </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!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment