Android will get screen width

Android: Hey, I want to get the width of the screen and divide it by 3. I want to use this length in LayoutParams, but it does not work. What am I doing wrong? Application crashes when // 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. // it was an application crash, hence

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

//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 the view 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");
}

}
+3
source share
4 answers

At the point where the view is created, it is not yet attached to the layout or window. Try moving this code to onAttachedToWindow.

+2
source

Despite getWindowManager()this, it is an activity method: http://developer.android.com/reference/android/app/Activity.html#getWindowManager ()

Also, the method getSize()is only available with the livel 13 API.

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

, , aproid Android,

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated
+2

I believe that getWindowManager () should be called in the Activity, not in the Layout class. Try to get the width / height in your activity, then pass these values ​​(width / height) to your extended Linear Layout class and use them.

0
source

If you are not in Office, you can get the default mapping through WINDOW_SERVICE:

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

details here ..

0
source

All Articles