Convert Dp To Px

Description

The formula for converting dp to px (pixels) is as follows:

pixels = dp * (dpi / 160)

where dpi is either 120, 160, 240, or 320.

If the Button on a 235 dpi screen, its actual width is

160 * (240/160) = 240 px.

When run on the 180 dpi emulator, which isregarded as a 160 dpi device, its actual pixel is now 160 * (160/160) = 160 px.

In this case, one dp is equivalent to one px.

You can use the getWidth() method of a View object to get its width in pixels:


public void onClick(View view) {
    Toast.makeText (this,String.valueOf(view.getWidth()),Toast.LENGTH_LONG).show();
}




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView