As obvious as it is, I went to the Android Developer's website to look up the available methods for finding width and height. And it said android.view.View.getWidth() and android.view.View.getHeight() will do just the thing. But..... it did NOT do the thing that way.
I had been looking around all over the forums and sites to find the solution.... LOTs of people are asking this questions, but i couldn't find that absolute solution from any of the forums. So, once I figured out the way, I thought of putting the solution up on my blog.
At first, here is how the android activity looks:
Activity with a Toast Message showing width and height of the green frame |
You made my day! Thanks alot!
ReplyDeleteCheers mate, but Christ! I want to love Android development but it's SO painful, a simple thing like measuring the size of a View requires a custom class!?
ReplyDeletetotally agree with you on that one........ stupid stuff.
ReplyDeletethank you man.....i was trying this since 2 days....
ReplyDeleteThanx a lot. You made my day.
ReplyDeleteonSizeChanged() solution works in my case to get Height and width of a custom view.
Syed, Compiled an ran the example. The following code has no effect that can be seen. Anyone else have this problem?. Thanks, Darin.
ReplyDelete@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
String msg = "width: " + viewWidth + "height: " + viewHeight;
System.out.println(msg);
}
System.out.println(msg) cannot be seen on the emulator screen. It can be seen in the logcat. You could instead produce a Toast. The toast would show up on the emulator's screen.
DeleteThe trick to determine the width and/or height happens in the onSizeChanged() method. The onDtaw() method (in this example) just USES the determinED width and height.
Hope i could help.
Thanks again Syed I have all the scaling code down all I need now is how to get the intial height to work with for a livewallpaper, if you want I can email you what I got and maybe together we can come up with a solution for this, the way I got set up it will scale all images and also were they should be placed, only one thing is needed and that is how to get that intial height let me know if you want to try this I can email the files maybe we both can learn this and get it going, I know the rescaling in android is a nightmare for a lot folks, this method I have will even scale the x and y positions properly but I am just learning myself
ReplyDeleteSam
thanks for the feedback.... i do not seem to have much experience with live wallpapers... you can try this though if it helps...
ReplyDeletemyListOfViews.get(i).setLayoutParams(
new LayoutParams(
getWindowManager().getDefaultDisplay().getWidth() / myListOfViews.length,
getWindowManager().getDefaultDisplay().getHeight() / 2
)
);
what this does is positions a row of views (eg. buttons) on the screen whose heights are half of that of
the screen size....... and whose widths are the width of the screen equally divided among the set of views.
i am not sure if my answer helped you in your quest.... i am also a newbie on android... stackoverflow.com has a REALLY good community of experienced and newcomer android developers.... try tapping in there.
tc
excellent tutorial, I am just starting to learn java and android and I have been looking for a way to get the width and height of the screen then use that to rescale all images and positions of images by creating a ratio from the original height * background image height (which will always be bigger, this is for a livewallpaper I am trying to create, but I just am having issues passing the information to other class files, hopefully I can figure this one out.
ReplyDeleteUnless you have a way of doing this, like I said I am learning and it's probably a simple solution.
Thanks for this I will try it.
Sam
Thanks for this post, you helped me out.
ReplyDeleteFinally, someone spells this out clearly. Thank you!
ReplyDeletethankyou for help
ReplyDeletecheers :)
ReplyDeleteThanks, just what i was looking for!
ReplyDeleteThanks for a good post on android application. I have subscribed to your RSS feed.
ReplyDeleteManeesh
Gotocamera.com
awesum example dude...simple and TO THE POINT :-)...thnx...
ReplyDeletehow you get Height?
ReplyDeleteThank you so much! That solved all problems I had in my app! :)
ReplyDelete