3

Android: Hey I want to retrive the width of the screen and divide it by 3. I want to use this length in LayoutParams, but it is not working. What am I doing wrong ? The app crashes at // 1.

public class LoaderImageView extends LinearLayout {

private Context mContext;
private Drawable mDrawable;
private ProgressBar mSpinner;
private ImageView mImage;
Context ctx;
Display display;

public LoaderImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public LoaderImageView(Context context) {
    super(context);
    init(context);
}

/**
 * First time loading of the LoaderImageView
 * Sets up the LayoutParams of the view, you can change these to
 * get the required effects you want
 */
private void init(final Context context) {
    mContext = context;

//1. //this is were the application crashes, from here

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

// to here

    mImage = new ImageView(mContext);
    mImage.setLayoutParams(new LayoutParams((width/3), 150));
    mImage.setVisibility(View.GONE);
    mImage.setBackgroundColor(Color.WHITE);
    mImage.setPadding(3, 3, 3, 3);



    mSpinner = new ProgressBar(mContext);
    mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    mSpinner.setIndeterminate(true);

    addView(mSpinner);
    addView(mImage);


}

/**
 * Set's the view's drawable, this uses the internet to retrieve the image
 * don't forget to add the correct permissions to your manifest
 * 
 * @param imageUrl the url of the image you wish to load
 */
public void setImageDrawable(final String imageUrl) {
    mDrawable = null;
    mSpinner.setVisibility(View.VISIBLE);
    mImage.setVisibility(View.GONE);
    new Thread() {
        public void run() {
            try {
                mDrawable = getDrawableFromUrl(imageUrl);
                imageLoadedHandler.sendEmptyMessage(RESULT_OK);
            } catch (MalformedURLException e) {
                imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
            } catch (IOException e) {
                imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
            }
        };
    }.start();
}

/**
 * Callback that is received once the image has been downloaded
 */
private final Handler imageLoadedHandler = new Handler(new Callback() {

    public boolean handleMessage(Message msg) {
        switch (msg.what) {
        case RESULT_OK:
            mImage.setImageDrawable(mDrawable);
            mImage.setVisibility(View.VISIBLE);
            mSpinner.setVisibility(View.GONE);
            break;
        case RESULT_CANCELED:
        default:
            // Could change image here to a 'failed' image
            // otherwise will just keep on spinning
            break;
        }
        return true;
    }
});

/**
 * Pass in an image url to get a drawable object
 * 
 * @return a drawable object
 * @throws IOException
 * @throws MalformedURLException
 */
private static Drawable getDrawableFromUrl(final String url) throws IOException, MalformedURLException {
    return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), "name");
}

}
Ukjent
  • 823
  • 2
  • 9
  • 26

4 Answers4

2

At the point the view is created, it is not yet attached to a layout nor window. Try moving that code to onAttachedToWindow.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
2

Despite getWindowManager() is Activity's method: http://developer.android.com/reference/android/app/Activity.html#getWindowManager()

Also getSize() method is available only starting with API livel 13

http://developer.android.com/reference/android/view/Display.html#getSize(android.graphics.Point)

Probably that's not good, to cover all android api versions use

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated
Dmitry
  • 425
  • 4
  • 10
0

I believe getWindowManager() needs to be called on the Activity, not the Layout class. Try retrieving the Width/Height in your activity, then pass those values (width/height) into your extended Linear Layout class and use them.

Gophermofur
  • 2,101
  • 1
  • 14
  • 14
0

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

details here..

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300