1

Solved:

As Zoltán wrote I didn't take the statusbar and titlebar into consideration (They're both 25 in height)

I used the following code in my onClickListener method inside my Activity and forwarded the results to my Service class and printed it out there:

Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight = rectgle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight = contentViewTop - StatusBarHeight;

serviceIntent.putExtra("Titlebarheight", Integer.toString(TitleBarHeight));
serviceIntent.putExtra("Statusbarheight", Integer.toString(StatusBarHeight));

-----------------------------------------------------------------------------------------

I am trying to set the layout height of my ImageView in my main.xml, but when I set the layout_height to 270dp (or px) it fills up the whole screen from top to bottom, but my screen size is actually 320x240 (HTC magic - Android 1.5) - So it SHOULDN'T fill up the screen from top to bottom!

Code inside Activity:

Display display = getWindowManager().getDefaultDisplay();
Log.i("Screen height", Integer.toString(display.getHeight())); // Returns 320
Log.i("Screen width", Integer.toString(display.getWidth())); // Returns 240

Code inside main.xml:

<ImageView
    android:id="@+id/bitmapImageView"
    android:layout_height="270dp" // Fills up the whole screen from top to bottom
    android:layout_width="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#FFFFFF"
    android:src="@drawable/ic_launcher" />

Why is this happening? How can I fix it?

Example when using 270 as layout height:

Example when using 240 as layout height:

Birdman
  • 5,244
  • 11
  • 44
  • 65
  • dp and px are NOT the same unit: http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android – Zoltán Jul 10 '12 at 07:44
  • I know they're NOT the same unit, but I just stated that I tried them both. – Birdman Jul 10 '12 at 07:46
  • You want to do all UI Design dynamically inside Activity or in XML? – Venky Jul 10 '12 at 07:48
  • Partly inside the Activity, but mostly in XML – Birdman Jul 10 '12 at 07:49
  • 1
    Are you trying to make a full screen activity? By default an activity is not full screen and your layout xml is actually contained inside of a ViewGroup that is provided by Android to give you the title bar. – Rich Schuler Jul 10 '12 at 07:50
  • @Qberticus That helps! How do I "subtract" the titlebar ViewGroup that fills up the top of the screen, when getting screen sizes? – Birdman Jul 10 '12 at 07:53
  • If you want a view to fill up the entire vertical space use `fill_parent`. You don't have to try to calculate it manually. If you want a full screen activity, I edited my answer below. – Rich Schuler Jul 10 '12 at 07:57

2 Answers2

2

See: Screen Resolution of android and scale-independent pixel

Basically, a dp (display-independent pixel) is not the same size on all displays. At your resolution 1dp =~ .75px

To do a full screen activity see: Fullscreen Activity in Android?

Specifically, set: android:theme="@android:style/Theme.NoTitleBar.Fullscreen" in the android manifest for that activity.

Community
  • 1
  • 1
Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
  • Thx. But I'm not interested in doing a full-screen Activity, I just need to retrieve the visible Activity size. – Birdman Jul 10 '12 at 08:06
2

The height of the status bar and the title bar of your application seems to be about the same as the width of your ImageView, which is 50px judging from your XML.

So then 50px + 270px = 320px, which is the total height of your screen (including the status and title bar).

Zoltán
  • 21,321
  • 14
  • 93
  • 134