Question

I'm building a Android application, and it's designed for Galaxy Tab, by specs, the screen size is 1024x600, but if I try to add my component in this size, the left and bottom are cut off.

I added the following code to see the difference

  WindowManager mWMgr = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
  int width = mWMgr.getDefaultDisplay().getWidth();
  int height = mWMgr.getDefaultDisplay().getHeight();
  Toast.makeText(getApplicationContext(),
    "[system][w=" + width + ":h=" + height + "]", Toast.LENGTH_LONG).show();

  width = getWindowManager().getDefaultDisplay().getWidth();
  height = getWindowManager().getDefaultDisplay().getHeight();
  Toast.makeText(getApplicationContext(),
    "[this][w=" + width + ":h=" + height + "]", Toast.LENGTH_LONG).show();

When running, this shows: [system][w=600:h=1024] [this][w=400:h=683]

Why it happens?

If needed I add my code to put the components on screen.

This is my layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView android:id="@+id/image01" android:layout_width="match_parent"
        android:layout_height="match_parent" android:scaleType="fitXY" />
</LinearLayout>

In this case, the image I draw on the ImageView get scaled to the screen size (400x683)

I tested with:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="600px"
    android:layout_height="1024px">
    <ImageView android:id="@+id/image01" android:layout_width="match_parent"
        android:layout_height="match_parent" android:scaleType="fitXY" />
</LinearLayout>

And by this way, the left and bottom of the image is cut off the screen.

Was it helpful?

Solution

Don't use Display.getWidth/Height(). These return the raw size of the display. This is not how much space you actually have. For example, the status bar will reduce the actual space you have from that, by some undefined amount along some axis. And don't think using FLAG_FULLSCREEN will save you from this, because it is perfectly reasonably for a device to have a "status bar" that contains things like the back and home button, and thus not something you can get rid of.

The correct way to resize to the screen is through the view system. The simplest thing here is just to make a subclass of View that implements onSizeChanged(), and stick this in with setContentView(). Once the layout pass completes, your onSizeChanged() will be called with the space available for the view. This will be called whenever that space changes -- due to an orientation change, display change, IME being shown, etc.

OTHER TIPS

Does it help to specify the respective value in the manifest:

<supports-screens android:smallScreens=["true" | "false"] 
                  android:normalScreens=["true" | "false"] 
                  android:largeScreens=["true" | "false"] 
                  android:anyDensity=["true" | "false"] />

See Android docs on this.

What do you have in your XML? Fill parent or wrap content for the view? you need first one depending on what you're doing...

Edit: on top Thorsten Dittmars answer...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top