4

I want to get the screen width , and set it to my layout which is a child of a Horizontal Scroll View . I am doing this but it makes my layout wider than I want .

padding is defined with dips.

 Display display = getWindowManager().getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics (); display.getMetrics(outMetrics); float density = getResources().getDisplayMetrics().density; int padding = getResources().getDimensionPixelSize(R.dimen.padding); layoutWidth = (int) ((outMetrics.widthPixels * density) - 2*padding); 
2
  • if you want to add the view dynamically then why don't you use LayoutParams.MATCH_PARENT ? Commented Jan 27, 2014 at 12:54
  • because the container layout in the HorizontalScrollView cant be match_parent Commented Jan 27, 2014 at 13:10

3 Answers 3

4
 Display display = getWindowManager().getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics (); display.getMetrics(outMetrics); float density = getResources().getDisplayMetrics().density; float dpHeight = outMetrics.heightPixels / density; float dpWidth = outMetrics.widthPixels / density; 

Also refer: http://developer.android.com/guide/practices/screens_support.html#dips-pels

Sign up to request clarification or add additional context in comments.

2 Comments

:( . It'l help if you can paste the xml and code where you set the layout width
I create the Layout with LayoutInflator , and this is where I set the width container.getLayoutParams().width = width;
2

You can utilize following code of mine.

MainActivity.java

package com.mehuljoisar.d_screensize; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override protected void onResume() { super.onResume(); ScreenSizeUtils mScreenSizeUtils = new ScreenSizeUtils(MainActivity.this); ((TextView)findViewById(R.id.tvPx)).setText("wxh (px): "+mScreenSizeUtils.GET_widthxheight_px()); ((TextView)findViewById(R.id.tvDp)).setText("wxh (dp): "+mScreenSizeUtils.GET_widthxheight_dp()); } } 

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tvPx" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvDp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvPx" /> </RelativeLayout> 

ScreenSizeUtils.java

package com.mehuljoisar.d_screensize; import android.content.Context; import android.content.res.Configuration; import android.graphics.Point; import android.os.Build; import android.util.DisplayMetrics; import android.view.Display; import android.view.WindowManager; public class ScreenSizeUtils { // helper classes private Context mContext; private WindowManager mWm; private Display mDisplay; private Configuration mConfig; private DisplayMetrics mDisplayMetrics, mRealDisplayMetrics; // helper attributes private static final int Unknown = -255; private int width_px, height_px; private int width_dp, height_dp; //values private String widthxheight_px; private String widthxheight_dp; public ScreenSizeUtils(Context mContext) { this.mContext = mContext; init(); } private void init() { mWm = ((WindowManager) mContext .getSystemService(Context.WINDOW_SERVICE)); mDisplay = mWm.getDefaultDisplay(); mConfig = mContext.getResources().getConfiguration(); mDisplayMetrics = new DisplayMetrics(); mDisplay.getMetrics(mDisplayMetrics); if(Build.VERSION.SDK_INT >= 17) { mRealDisplayMetrics = new DisplayMetrics(); mDisplay.getRealMetrics(mRealDisplayMetrics); } } public String GET_widthxheight_px() { if (Build.VERSION.SDK_INT < 13) { width_px = mDisplay.getWidth(); height_px = mDisplay.getHeight(); } else { Point mPoint = new Point(); mDisplay.getSize(mPoint); width_px = mPoint.x; height_px = mPoint.y; } widthxheight_px = width_px + " x " + height_px; return widthxheight_px; } public String GET_widthxheight_dp() { int real_width_px = Unknown; int real_height_px = Unknown; if (Build.VERSION.SDK_INT >= 17) { real_width_px = mRealDisplayMetrics.widthPixels; real_height_px = mRealDisplayMetrics.heightPixels; } else { //it is necessary if you haven't called it before GET_widthxheight_px(); } height_dp = (int) (((double) ((real_height_px == Unknown) ? height_px : real_height_px) / mDisplayMetrics.density) + 0.5); if (Build.VERSION.SDK_INT >= 13) { width_dp = mConfig.screenWidthDp; } else { width_dp = (int) (((double) ((real_width_px == Unknown) ? width_px : real_width_px) / mDisplayMetrics.density) + 0.5); } widthxheight_dp = width_dp + " x " + height_dp; return widthxheight_dp; } } 

Screenshot

Screen-size demo

I hope it will be helpful !!

Comments

0

This code will return width (w) & height (h) of screen.

DisplayMetrics dMetrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dMetrics); float density = dMetrics.density; int w = Math.round(dMetrics.widthPixels / density); int h = Math.round(dMetrics.heightPixels / density); 

activity is instance of Activity which would you like to get screen size.

You have to remember that: When your device is in landscape orientation, w > h. When it in portrait orientation w < h.

So from width & height you can detect your device is in what orientation.

1 Comment

I have tried this , but it didn't help also. I am getting too wide Layout. I think int w doesn't contain the screen width in dips

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.