Question

Yes, I know this question has been asked dozens of times before. However, when implementing several of the solutions, my app force closes. I have tried doing it on a button click:

    //Determine Screen Size and Create Layout Param
    android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int BtnWth = display.getWidth() / 4;
    int BtnHgt = display.getHeight() / 4;
    RelativeLayout.LayoutParams BtnDim = new RelativeLayout.LayoutParams(BtnWth, BtnHgt);

    // Declare MPC pads
    Button pad1 = (Button) findViewById(R.id.pad1);

public void onClick(){

    // Set width and height of each pad
    pad1.setLayoutParams(BtnDim);
}

And also using the onDraw() method. Both force close on a real Android 3.1 device.

Was it helpful?

Solution

Try this code instead to get the screen width and height in pixels:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;

Make sure you add this to your imports:

import android.util.DisplayMetrics;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top