简体   繁体   中英

How to fix Pop up window to multiple screen size in Android

I'm creating popup window open at the bottom of screen on button click event.The popup window is correctly open at the location of bottom in screen size of 720 x 1280 pixels (~294 ppi pixel density) but same as to run in the screen size of 480 x 854 pixels (~196 ppi pixel density) it is displaying in the center of screen. How to fix this popup window issue for multiple screen size.

This is my popup xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="fill_parent"
    android:layout_height="180dp"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    android:padding="3dp">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:background="@android:color/transparent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Title"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@drawable/curve_shap"
            android:gravity="center_horizontal|center_vertical"
            android:text="Take Photo"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/darker_gray"
            android:textSize="14sp" />

        <Button
            android:id="@+id/button_Camera"
            android:layout_width="fill_parent"
            android:layout_height="40dp"

            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@drawable/curve_shap"
            android:text="Camera"
            android:textColor="#3A86CF" />

        <Button
            android:id="@+id/button_Gallery"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@drawable/curve_shap"
            android:text="Gallery"
            android:textColor="#3A86CF" />


    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_marginTop="4dp"
        >

        <Button
            android:id="@+id/btnCancelCamera"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"

            android:background="@drawable/curve_shap"
            android:text="Cancel"
            android:textColor="#3A86CF"
            android:textSize="16sp"
            android:textStyle="bold" />
    </RelativeLayout>


</LinearLayout>

This is my popup window code.

imgProfilePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.camera_popup, (ViewGroup) findViewById(R.id.popup_element));
                popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setWidth(720);
                popupWindow.setHeight(350);
                popupWindow.setFocusable(true);
                popupWindow.showAtLocation(popupView, Gravity.BOTTOM, 0, 0); //87
                popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

                Button btnCamera = (Button) popupView.findViewById(R.id.button_Camera);
                Button btnGallery = (Button) popupView.findViewById(R.id.button_Gallery);
                Button btnDismiss = (Button) popupView.findViewById(R.id.btnCancelCamera);

                btnCamera.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
                        String picformat = "IMG_" + 0 + "_" + s.format(new Date()) + ".png";
                        Log.e(" picformat ", " = " + picformat);

                        Intent i = new Intent(Filter_Screen.this , Image_Cropping.class );
                        startActivity(i);

                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "classNKK_ProfilePic";
                        File myPath = new File(extr, picformat);
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myPath));
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);

                        Log.e("Camera", " Open");
                        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                        String imageUserProfile_path = baseDir + "/classNKK_ProfilePic/" + picformat;
                        editor.putString("profile_picformat", imageUserProfile_path);
                        editor.commit();


                        popupWindow.dismiss();


                    }
                });

                btnGallery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(galleryIntent, PROFILE_GALLERY);
                        Log.e("Gallery open", "");
                    }
                });

                btnDismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
            }
        });

And i have to create folder of layout for multiple screen size and i put the popup window xml file in this folders.

  1. layout
  2. layout-hdpi
  3. layout-mdpi

Thanks in advanced .

You should take into consideration that devices have different screen sizes:

  • xlarge screens are at least 960dp x 720dp
  • large screens are at least 640dp x 480dp
  • normal screens are at least 470dp x 320dp
  • small screens are at least 426dp x 320dp

So instead of placing your layout in layout-hdpi and layout-mdpi, I would suggest creating a dimens.xml for each screen size and extract your size related values there.

For example if you want the popup height to be 100dp for normal screens and 200dp for large screens create the following files:

res/values-normal/dimens.xml

<resources>
    <dimen name="popup_height">100dp</dimen>
</resources>

res/values-large/dimens.xml

<resources>
    <dimen name="popup_height">200dp</dimen>
</resources>

and in your layout use

android:layout_height="@dimen/popup_height"

Additional info can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM