0

I'm using the QuickAction code from here and am having some difficulty after a week of messing with it not displaying correctly on certain screen sizes. I've narrowed down the problem to one line in the code, but can't seem to get it correctly display on my phone and emulator at the same time. Allow me to explain.

The line in question is in the "PopupWindows.java" file, and is the following:

 public static final int MAX_WIDTH_POPUP_WINDOWS = 435;

I've changed this to "433" as that fits on perfectly with 3 icons on a 2.3, 4.0.1, and 4.1 emulator with a resolution of 800x480, which looks like this:

enter image description here

But on the phone (1920x1080), it renders like this:

enter image description here

Where it gets weird, is when I turn that number to 865, which renders on the phone correctly

enter image description here

but then in the emulator shows like this:

enter image description here

I've tried changing the xml for quickaction to wrap content, as well as setting hard coded widths, but this doesn't fix it. See code below:

PopupWindows.java

package actionitem;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater; 
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * Custom popup window.
 * 
 * @author Lorensius W. L. T <lorenz@londatiga.net>
 * 
 */
public class PopupWindows {
protected Context mContext;
protected PopupWindow mWindow;
protected View mRootView;
protected Drawable mBackground = null;
protected WindowManager mWindowManager;
Resources res;
// public static final int MAX_WIDTH_POPUP_WINDOWS = 433;

public static final int MAX_WIDTH_POPUP_WINDOWS = 865;

/**
 * Constructor.
 * 
 * @param context
 *            Context
 */
public PopupWindows(Context context) {
    mContext = context;
    mWindow = new PopupWindow(context);

    mWindow.setTouchInterceptor(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                mWindow.dismiss();

                return true;
            }

            return false;
        }
    });

    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}

/**
 * On dismiss
 */
protected void onDismiss() {
}

/**
 * On show
 */
protected void onShow() {
}

/**
 * On pre show
 */
protected void preShow() {
    if (mRootView == null)
        throw new IllegalStateException("setContentView was not called with a view to display.");

    onShow();

    if (mBackground == null)
        mWindow.setBackgroundDrawable(new BitmapDrawable(res));
    else
        mWindow.setBackgroundDrawable(mBackground);

    int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
    int w = screenWidth < MAX_WIDTH_POPUP_WINDOWS ? screenWidth : MAX_WIDTH_POPUP_WINDOWS;
    // if (screenWidth < 490) {
    // w = 433;
    // } else if (screenWidth > 865) {
    // w = 865;
    // }
    mWindow.setWidth(w);// WindowManager.LayoutParams.WRAP_CONTENT);
    mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    mWindow.setTouchable(true);
    mWindow.setFocusable(true);
    mWindow.setOutsideTouchable(true);

    mWindow.setContentView(mRootView);
}

/**
 * Set background drawable.
 * 
 * @param background
 *            Background drawable
 */
public void setBackgroundDrawable(Drawable background) {
    mBackground = background;
}

/**
 * Set content view.
 * 
 * @param root
 *            Root view
 */
public void setContentView(View root) {
    mRootView = root;

    mWindow.setContentView(root);
}

/**
 * Set content view.
 * 
 * @param layoutResID
 *            Resource id
 */
public void setContentView(int layoutResID) {
    LayoutInflater inflator = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    setContentView(inflator.inflate(layoutResID, null));
}

/**
 * Set listener on window dismissed.
 * 
 * @param listener
 */
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
    mWindow.setOnDismissListener(listener);
}

/**
 * Dismiss the popup window.
 */
public void dismiss() {
    mWindow.dismiss();
}
}

and my quickaction.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="220dp"
android:layout_height="wrap_content" >

<FrameLayout
    android:id="@+id/header2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/scroll"
    android:layout_marginTop="10dip"
    android:background="@drawable/quickaction_top_frame" />

<ImageView
    android:id="@+id/arrow_up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/quickaction_arrow_up" />

<HorizontalScrollView
    android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/header2"
    android:background="@drawable/quickaction_slider_background"
    android:fadingEdgeLength="0dip"
    android:paddingLeft="1dip"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/tracks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="4dip"
        android:paddingTop="4dip" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/quickaction_slider_grip_left" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/quickaction_slider_grip_right" />
    </LinearLayout>
</HorizontalScrollView>

<FrameLayout
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignRight="@id/scroll"
    android:layout_below="@id/scroll"
    android:background="@drawable/quickaction_bottom_frame" />

<ImageView
    android:id="@+id/arrow_down"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/footer"
    android:layout_marginTop="-1dip"
    android:src="@drawable/quickaction_arrow_down" />

</RelativeLayout>

Sorry in advance for the long post, but its a habit as I work in support, and believe providing the most detail up front helps resolve an issue sooner :)

Thanks!

Evan R.
  • 1,210
  • 1
  • 27
  • 42

1 Answers1

0

You shouldn't use hardcoded pixel values. The easiest way to get around this problem is to declare a dimension in an xml resource file. Then in your code you can do:

  width = getResources().getDimensionPixelSize(R.dimen.popup_width);

That will get you the number of pixels adjusted according to the density. You should figure out how many DPs each of your buttons is and declare the resource accordingly.

dmon
  • 30,048
  • 8
  • 87
  • 96