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
10. For showing icon in popupmenu reflection in used-
11. On button click we will call this function , we just have to pass the View. Open activity_main.xml -
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










Permanent sidebar blogroll backlinks on real websites with dofollow links for long-term SEO authority and stable rankings. backlink blogroll
ReplyDeleteThis weblog seems to get a great deal of site visitors. How do you promote it? It gives a nice individual twist on things. I guess getting something useful or substantial to post about is the most important factor. Ice Cream Bakeries Maine
ReplyDeleteYou need to be a part of a tournament first of the most useful blogs on-line. I will suggest this website! 오피아트
ReplyDeleteHi I wish to to share a remark here concerning you to definitely have the ability to inform you just how much i personally Loved this particular study. I must run off in order to aTurkey Day Dinner but wanted to leave ya a simple comment. We preserved you So will be coming back subsequent function to read more of yer high quality posts. Continue the quality work. alexistogel
ReplyDelete“I had to refresh the page times to view this page for some reason, however, the information here was worth the wait.” taco time
ReplyDeleteI conceive this web site holds very excellent indited content material posts . olxtoto slot
ReplyDeleteTruy cập link vào ST666 chính thức không bị chặn 2026. Khám phá kho game giải trí xanh chín, nạp rút siêu tốc trong 90 giây và bảo mật thiết bị cao st666
ReplyDeletesometimes you got to taper down on playing too much compter games coz it is quite addictive” situs togel
ReplyDeletevanity leads to more plastic surgery procedures. people are becoming more conscious about their appearance- Bandar Togel Macau
ReplyDeleteI have to get across my appreciation for your kindness giving support to people that actually need help with this concept. Your personal commitment to getting the solution across had become really effective and has continuously made some individuals like me to realize their desired goals. Your own insightful advice denotes this much to me and somewhat more to my office workers. Many thanks; from all of us. 강남스웨디시
ReplyDelete
ReplyDeleteThe Bone Density Solution - The material is readable, practical, and accessible.
The Bone Density Solution
I got what you intend, saved to fav, very decent website . ALGOTOTO
ReplyDeleteI agree with your thought. Thank you for your sharing. football prediction
ReplyDeleteI will invite all my friends to your blog, you really got a great blog.,`”~’ Normative Social Influence
ReplyDeleteI love to visit your web-blog, the themes are nice.::;:, What Is a Simulated Diamond
ReplyDeletebike racks could really help you secure your bike when you leave it ,. Free Body Diagram
ReplyDeleteMaxorin - “Convenience is definitely one thing I appreciate.” Maxorin
ReplyDelete