简体   繁体   中英

How do i get the dimensions of the screen when orientation changes? [android]

I know how to get the dimensions of the screen onCreate. I was making a runnable which will allow me to get the dimensions inside of that.

The thing is that when i rotate my device, it doesnt seem to properly return the dimensions.

I was thinking to do it onConfigurationChanged() as that seems to firre when i rotate my device. I was trying to do the following but i dont think it was giving valid data.

Rect r = new Rect();
Window win = getWindow();
win,getDecorView().getWindowVisibleDisplayFrame(r);
int height = r.height();
int width = r.width();

but it seems that they arent correct. I was trying to move a created item around the screen, and it doesnt go to the edges of the screen. I dont think it has anything to do with my drag function.

I feel the dimensions are incorrect.

How is this done? Am i on the right path?

The workaround i had created was that onConfigurationChange() I created a bundle with attributes and then recalled onCreate(). Essentially this would recreate in the new orientation.

This was what i did to get around this issue at hand, and it made it so the layout is rerendered.

Bundle b = new Bundle();
b.putString("test","data here");
onCreate(b);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM