Friday, 25 July 2014

PopupMenu in Android API 8+

Popup menu is available from API 11. If we want to use it in API 9 we have to use android-support-v7-appcompat library.



Download Source Code DOWNLOAD

1. Go to File->Import

2. Click Existing Android Code Into Workspace

3.Click Browse

4.Goto where SDK is present SDK->extras->android->support->v7->appcompact

5. Tick Copy projects into workspace

6. Now RightClick on Project->Properties

7. Android->Add Library->android-support-v7-appcompat library.



8. import android.support.v7.widget

Important:- In AndoridManifest.xml set theme "@style/Theme.AppCompat.Light"

9. Now create a xml file for popupmenu
Goto Project->res folder->menu folder create popupmenu.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/Item1"
        android:icon="@drawable/ic_launcher"
        android:title="Item1"/>
    <item
        android:id="@+id/Item2"
        android:icon="@drawable/ic_launcher"
        android:title="Item2"/>
    <item
        android:id="@+id/Item3"
        android:icon="@drawable/ic_launcher"
        android:title="Item3"/>

</menu>

10. For showing icon in popupmenu reflection in used-

void showPopupWindow(View view) {
      PopupMenu popup = new PopupMenu(MainActivity.this, view);
      try {
            Field[] fields = popup.getClass().getDeclaredFields();
            for (Field field : fields) {
                  if ("mPopup".equals(field.getName())) {
                        field.setAccessible(true);
                        Object menuPopupHelper = field.get(popup);
                        Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                        Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                        setForceIcons.invoke(menuPopupHelper, true);
                        break;
                  }
            }
      } catch (Exception e) {
            e.printStackTrace();
      }
      popup.getMenuInflater().inflate(R.menu.popupmenu, popup.getMenu());
      popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            public boolean onMenuItemClick(MenuItem item) {
                  Toast.makeText(getApplicationContext(), "You Clicked : " + item.getTitle(),      Toast.LENGTH_SHORT).show();
                  return true;
            }
      });
      popup.show();
}

11. On button click we will call this function , we just have to pass the View. Open activity_main.xml -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnShowPopup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Show Popup" />

</LinearLayout>

12.  MainActivity.java

package com.tutorialsface.popupmenu;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

      Button btnShowPopup;
     
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnShowPopup = (Button) findViewById(R.id.btnShowPopup);
            btnShowPopup.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                        showPopupWindow(v);
                  }
            });
      }
     
      void showPopupWindow(View view) {
            PopupMenu popup = new PopupMenu(MainActivity.this, view);
            try {
                  Field[] fields = popup.getClass().getDeclaredFields();
                  for (Field field : fields) {
                        if ("mPopup".equals(field.getName())) {
                              field.setAccessible(true);
                              Object menuPopupHelper = field.get(popup);
                              Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                              Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                              setForceIcons.invoke(menuPopupHelper, true);
                              break;
                        }
                  }
            } catch (Exception e) {
                  e.printStackTrace();
            }
            popup.getMenuInflater().inflate(R.menu.popupmenu, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
     
                  public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(getApplicationContext(), "You Clicked : " + item.getTitle(),  Toast.LENGTH_SHORT).show();
                        return true;
                  }
            });
            popup.show();
      }
}

13. AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorialsface.popupmenu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >
        <activity android:name="com.tutorialsface.popupmenu.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Download Source Code DOWNLOAD



No comments:

Post a Comment