Home
> Tricks & Tips > Get Window Status Bar and Title Bar Height Size
Get Window Status Bar and Title Bar Height Size
This is an interesting tip
to get the height size of status bar and title bar, assuming that they are both on the top of screen.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvContent = (TextView)findViewById(R.id.tvContent);
tvContent.postDelayed(new Runnable() {
@Override
public void run() {
String display = String.format("Status Bar Height = %d\nTitle Bar Height = %d", getStatusBarHeight(), getTitleBarHeight());
tvContent.setText(display);
}
}, 2000);
}
public int getStatusBarHeight() {
Rect r = new Rect();
Window w = getWindow();
w.getDecorView().getWindowVisibleDisplayFrame(r);
return r.top;
}
public int getTitleBarHeight() {
int viewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
return (viewTop - getStatusBarHeight());
}
The reason why getting directly inside onCreate() method doesnot work is because the view is being measured; so it’s better to retrieve the size after finishing measuring. In this case, I attach a handler to the TextView and run after 2 seconds delay. You also can run on a Listener as well.
Cheers,
Pete Houston
Comments (0)
Leave a comment
Leave a Reply Cancel reply
Calendar
Categories
- Home (4)
- Learning (149)
- Books & Materials (2)
- Design Patterns (2)
- PhoneGap (1)
- Tricks & Tips (75)
- Tutorials (69)
- Of Diary (20)
- Pete's Works (8)
- Applications (7)
- Publications (1)
- Tools (7)
Most Popular
Tag Cloud
activity
android
application
applications
bitmap
blog
cache
color
custom
data
database
design
detect
device
diary
effect
element
emulator
file
filter
home
houston
image
intent
launcher
memory
mp3
new
parse
pete
processing
query
screen
secret
showcase
sqlite
tag
tips
tools
tricks
tutorials
view
xml
xpath
zip
Archives
- May 2013 (1)
- January 2013 (5)
- December 2012 (4)
- October 2012 (2)
- September 2012 (1)
- August 2012 (2)
- July 2012 (12)
- June 2012 (1)
- May 2012 (7)
- March 2012 (1)
- February 2012 (3)
- January 2012 (2)
- December 2011 (8)
- November 2011 (12)
- October 2011 (19)
- September 2011 (16)
- July 2011 (29)
- June 2011 (63)
Statistics
- 552,591 View
