Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Popup function in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 123
    Comment on it

    Here I have created Popup function , Popup function can be used to display menu item list, anchor text and listitem etc. In the below example when we clicked on menu button the popup will be open and if we click outside the popup it will disappears.
    Here in below code example I have explain how to make Popup function.
    Step(1)- activity_main.xml

    <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"  
        android:paddingBottom="@dimen/activity_vertical_margin"  
        android:paddingLeft="@dimen/activity_horizontal_margin"  
        android:paddingRight="@dimen/activity_horizontal_margin"  
        android:paddingTop="@dimen/activity_vertical_margin"  
        tools:context=".MainActivity" >  
    
        <Button  
            android:id="@+id/button1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentTop="true"  
            android:layout_marginLeft="62dp"  
            android:layout_marginTop="50dp"  
            android:text="Show Item" />  
    
    </RelativeLayout>
    

    Step(2)- poupup_menu.xml

    <menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  
    
        <item  
            android:id="@+id/one"  
            android:title="Apple"/>  
    
        <item  
            android:id="@+id/two"  
            android:title="banana"/>  
    
        <item  
            android:id="@+id/three"  
            android:title="orange"/>  
               <item  
            android:id="@+id/four"  
            android:title="Mango"/>  
    </menu>  
    

    Step(3)-MainActivity-

    public class MainActivity extends Activity {  
              @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) {  
                PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
                popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); 
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
                 public boolean onMenuItemClick(MenuItem item) {  
                  Toast.makeText(MainActivity.this,"Select this : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
                  return true;  
                 }  
                });  
    
                popup.show();//showing popup menu  
               }  
              });
             }  
        }  
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: