views:

309

answers:

1

Hi guys, this is a really simple question on which I've found no answer :/ How can I quickly access the screen resolution (width, height) as integer values?

I've tried this one, but it always shows zero on my emulator:

DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;

In my case I want to dynamically create a table with tableRows, each containing two cols. This cols all shall fill half of the screen in width.

Can someone give me a hint?

+2  A: 

The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.

kgiannakakis
so what does this mean for me, when I dynamically want to add TableRows and after finishing add the table to the main LinearLayout and set this one to ContentView (by using this.setContentView(ll), where ll is my linearLayout?
poeschlorn
onSizeChanged will be called the first time your View is drawn and whenever the orientation changes. When it is called, store the dimensions of the screen in members of your classes. Use these values when you are drawing (in the OnDraw method).
kgiannakakis
thanks, I'm gonna try this :)
poeschlorn