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

I hope it will be helpful !!
LayoutParams.MATCH_PARENT?