Reputation: 11
I'm designing an app that changes the padding of the linear layouts according to the resolution of the phone... I've designed the XML file for a resolution of 480*800 Here is one linear layout...I have 5 in 1 page...
<LinearLayout
android:id="@+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="78dp"
android:paddingLeft="120dp" >
<Button
android:textSize="30dp"
android:id="@+id/page"
android:textColor="@android:color/transparent"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pages" />
</LinearLayout>
Now I've used this code in JAVA file::
l3=(LinearLayout)findViewById(R.id.layout3);
size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
{
w.getDefaultDisplay().getSize(size);
width = size.x;
height = size.y;
}
else
{
d = w.getDefaultDisplay();
width = d.getWidth();
height = d.getHeight();
}
height1=(height/800)*113;
width1=(width/480)*14;
l3.setPadding(width1, height1, 0, 0);
But the variables width and height are having fixed values 480 and 800. So the code to get width and height pixels is not working...please help!!!! My target API is 8,10 and 17. Please help me with the code to find resolution(pixels) of the screen.
Upvotes: 0
Views: 152
Reputation: 485
http://developer.android.com/reference/android/util/DisplayMetrics.html
You can use it as follows:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
for density, there's
int densitiy = displaymetrics.densityDpi;
Upvotes: 1
Reputation: 3255
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final int height = dm.heightPixels;
final int width = dm.widthPixels;
Upvotes: 0