Sunday, 6 November 2016

Pop-up Context Menu in Android

This tutorial is regarding the Pop-up context menu in android. With this tutorial will see how to create a simple pop-up menu.
Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu. Anchor text is the text relative to which your pop-up is visible.
The android.widget.PopupMenu is the direct subclass of java.lang.Object class.

1. Create a file as follows activity_main.xml:
This file will be created automatically when your project is being created.

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" 
    tools:context=".MainActivity" >  
  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:layout_margin="16dp"  
        android:text="Show Popup" />  
  
</RelativeLayout> 

2. Create another class named popup_menu.xml:
This file is to be created inside the res/menu directory.

<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  
  
    <item  
        android:id="@+id/one"  
        android:title="Item_One"/>  
      
    <item  
        android:id="@+id/two"  
        android:title="Item_Two"/>  
        
    <item  
        android:id="@+id/three"  
        android:title="Item_Three"/>  
          
</menu> 

3. Now in your activity class named MainActivity.java do the following:

public class MainActivity extends Activity {  

         Button button1;  
           
         @Override  
         protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          setContentView(R.layout.activity_main);  
            
          button1 = (Button) findViewById(R.id.button1);  
          button1.setOnClickListener(new OnClickListener() {  
           
           @Override  
           public void onClick(View v) {  
            //Creating the instance of PopupMenu  
            PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
            //Inflating the Popup using xml file  
            popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  
           
            //registering popup with 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();//showing popup menu  
           }  
          }); 
         }  
    }  

That's all. Your Pop-up menu is created.

7 comments:

  1. how to xender apk restore files
    makes transferring and sharing files between devices fast and convenient, and it also allows users to restore files that were previously transferred or backed up. To restore files using Xender APK, first open the app and navigate to the “My Files” or “Backup & Restore” section. From there, select the “Restore” option, which will display a list of files or folders that have been saved earlier. You can choose the specific files you want to recover and tap “Restore” to bring them back to your device. This feature ensures that important documents, photos, videos, and other media can be quickly recovered without the need for an internet connection, making Xender APK a reliable tool for file management and backup.

    ReplyDelete
  2. The PM Kisan registration status allows farmers in India to check the progress of their enrollment under the Pradhan Mantri Kisan Samman Nidhi (PM-Kisan) scheme, which provides financial assistance of ₹6,000 per year to eligible farmers. By visiting the official PM Kisan portal, farmers can enter their Aadhaar number, account number, or mobile number to verify whether their registration has been successfully completed and if their payment installments have been released. This feature ensures transparency, helping farmers stay updated on their application status, identify any errors in their details, and take necessary steps to rectify issues for timely benefit transfer. The portal also provides alerts for any rejected applications, making it easier for farmers to complete their registration process without unnecessary delays.

    ReplyDelete
  3. Urdu novels website in PDF format offer a convenient way for readers to explore the rich world of Urdu literature without any cost. These PDFs allow enthusiasts to access classic and contemporary novels anytime, anywhere, making it easier for students, researchers, and literature lovers to enjoy stories from famous Urdu writers. Available in digital format, these novels can be read on smartphones, tablets, or computers, providing flexibility and portability. They cover a wide range of genres, including romance, social issues, historical fiction, and mystery, giving readers diverse options to suit their tastes. Moreover, free Urdu novel PDFs help preserve literary culture by making important works more accessible to a global audience.

    ReplyDelete
  4. Shop Solar InvertersShop Solar InvertersWhen you shop solar inverters, you’re investing in the central technology that turns your solar panel output into usable electricity for your home or business.

    ReplyDelete
  5. Shop Solar Inverters When you shop solar inverters, you’re investing in the central technology that turns your solar panel output into usable electricity for your home or business. Solar inverters not only convert DC power into AC power but also help maximize system efficiency, improve performance monitoring, and protect your setup from electrical issues.

    ReplyDelete
  6. tgslot game is a popular online gaming platform that provides a wide variety of slot games, live casino experiences, and betting options for enthusiasts. The platform is designed to be user-friendly, offering seamless navigation and quick access to games directly through its TGSlot link. Players can enjoy high-quality graphics, interactive gameplay, and secure transactions, making it a trusted choice for both beginners and seasoned gamers. With regular updates and attractive promotions, TGSlot ensures an engaging experience for everyone looking to enjoy online gaming from the comfort of their devices.

    ReplyDelete