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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 183
    Comment on it

    In the below example code I have created a custom popup menu function. Here first I have added a Button in actvity_main.xml layout, Then in next step I have created a new popup.xml layout in menu folder, here I have added popup id and title, Now See in coding area here first I have implement OnClickListener interface and then I have used in PopupMenu class OnMenuItemClickListener method. You can see below program it clearly describe you how to create Custom Popup in android.

    Step(1)activity_main.xml layout-
     

    <RelativeLayout xmlns:android="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="com.andoidsrc.popupmenu.MainActivity" >
    
        <Button
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="#eecdeeef"
            android:onClick="onClick"
            android:layout_marginTop="20dp"
            android:text="Select your profile" />
    
    </RelativeLayout>
    
    

    Srep(2)Created a new popup.xml layout in menu folder-
     

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
            <item
                android:id="@+id/lang_devloper"
                android:showAsAction="ifRoom|withText"
                android:title="Devloper"
                android:visible="true"/>
            <item
                android:id="@+id/lang_tester"
                android:showAsAction="ifRoom|withText"
                android:title="Tester"
                android:visible="true"/>
            <item
                android:id="@+id/lang_coder"
                android:showAsAction="ifRoom|withText"
                android:title="Coder"
                android:visible="true"/>
            <item
                android:id="@+id/lang_designer"
                android:showAsAction="ifRoom|withText"
                android:title="Designer"
                android:visible="true"/>
    
        </menu>

    Step(3)MainActivity-
     

    public class MainActivity extends Activity implements View.OnClickListener {
    
        private PopupMenu popupMenu;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public void onClick(View anchor) {
            // TODO Auto-generated method stub
            popupMenu = new PopupMenu(MainActivity.this, anchor);
            popupMenu.setOnDismissListener(new OnDismissListener());
            popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener());
            popupMenu.inflate(R.menu.popup);
            popupMenu.show();
        }
    
        private class OnDismissListener implements PopupMenu.OnDismissListener {
    
            @Override
            public void onDismiss(PopupMenu menu) {
                Toast.makeText(getApplicationContext(), "Popup Menu is dismissed",
                        Toast.LENGTH_SHORT).show();
            }
    
        }
    
        private class OnMenuItemClickListener implements
                PopupMenu.OnMenuItemClickListener {
    
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.lang_devloper:
                        Toast.makeText(getApplicationContext(), "Devloper got clicked",
                                Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.lang_tester:
                        Toast.makeText(getApplicationContext(), "Tester got clicked",
                                Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.lang_coder:
                        Toast.makeText(getApplicationContext(), "Coder got clicked",
                                Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.lang_designer:
                        Toast.makeText(getApplicationContext(), "Designer got clicked",
                                Toast.LENGTH_SHORT).show();
                        return true;
    
                }
                return false;
            }
    
        }
    
    }
    
    

     

 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: