Android get wrong screen size - android

I create android emulator with screen:
screen size: 7.5 inches
Resolution(width x height): 720x960
density: mdpi
And in Android virtual device manager it is shown xlarge screen size but in following code it returns Large screen size.
int screenLayout = context.getResources().getConfiguration().screenLayout;
screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
switch (screenLayout) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
return ScreenSize.SMALL;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
return ScreenSize.MEDIUM;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
return ScreenSize.LARGE;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
return ScreenSize.XLARGE;
default:
}

You will be able to get screen Height and Width using:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

Use Below code to get Height and Width of Android Phone Screen .
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
int width= displayMetrics.widthPixels;

getscreen Height and width use
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
get layout Height use
int layoutHeight = Layout.getMeasuredHeight();

public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}

You can try this:
/**
* Get screen width
*
* #param context
* #return screen width
*/
public static int getScreenWidth(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* Get screen height
*
* #param context
* #return screen height
*/
public static int getScreenHeight(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}

Related

gridview images with different alignment

I need to do something like this . It is similar to staggered gridview but all image has same dimension . How do i do this ?
Finally got it . The trick is to use staggered gridview of spancount 2 and have the second and last image to be of different height then all other . Here is an example .
First get the screen width .
WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
Now set the images in onBindViewHolder .
public void onBindViewHolder(final CustomRecycleViewHolder holder, final int position) {
final Holder myHolder = (Holder) holder;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(images.get(position), opts);
opts.inJustDecodeBounds = false;
int height;
if (position == 1 || position == (images.size() - 1)) {
height = 150;
} else {
height = 300;
}
Picasso.with(activity)
.load(images.get(position))
.error(R.drawable.ic_empty)
.placeholder(R.drawable.ic_launcher)
.resize(screenWidth / 2, height)
.centerCrop()
.into((myHolder.images));
}
Result

Resize layout over time

I'm trying to resize a layout over time, what I'm stuck on is trying to loop the decrease/increase in size, I've currently got it working to resize on a screen touch.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
// Gets linearlayout
layout = (RelativeLayout)findViewById(R.id.relativeOne);
params = layout.getLayoutParams();
params.height = width - width/2;
params.width = width - width/2;
sizeAfter = width;
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
sizeAfter = sizeAfter - 2;
params.height = sizeAfter;
params.width = sizeAfter;
return super.onTouchEvent(event);
}
Currently this works fine, however how would I go about implementing maybe a timer to loop after a set amount of time gradually decreasing the layouts size.

Get Screen width and height on android 2.2

How I can get screen width and height for use this value on device with android version 2.2(API lv8).
I use this code:
public static Point Maximum(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Point temp=new Point();
return temp;
}
And when i want check by print Max point to textview:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt= (TextView)findViewById(R.id.text1);
txt.setText(Math.Maximum(this).toString());
}
Point is (0,0). What problem?
Try this code. But this API is deprecated in the new SDK versions you can use this.
Display display = activity.getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Get Device Width and Height, return as point. Hope u like this.
public static Point Maximum(){
Display display = activity.getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Point temp=new Point();
temp.set(width, height);
return temp;
}
Try this code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Try this for getting your screen width. Similarly you can get screen height or can customize the same for returning both in a single function.
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
private int getScreenWidth() {
int width = 400;
int height = 600; // just intialising it to default value in case if there is any problem getting screen width
WindowManager wm = (WindowManager) activity // activity is the context if you are using this method in adapter. Otherwise in Activity you can simply ignore this
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if (android.os.Build.VERSION.SDK_INT >= 13) {
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
} else {
width = display.getWidth();
height= display.getHeight();
}
return width;//or height
}

Not getting screen Height and widht in Android

I using both Using Activity or without Activity, Not getting both height and width in Android.Please correct me, What i am doing wrong.
Here With Activity:
public class LandPort extends Activity{
public final static int height = 0;
public final static int width = 0;
Context context;
DisplayMetrics displaymetrics = new DisplayMetrics();
public LandPort(Context context) {
this.context = context;
}
public int getHeight() {
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
return height;
}
public int getWidth() {
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
return width;
}
}
In MainActivity:
LandPort lp= new LandPort(getApplicationContext());
int height = lp.getHeight();
int width =lp.getWidth();
Getting height and width both 0;
Another Without Activity:
public class GetHeigthWidth {
public final static int height = 0;
public final static int widht = 0;
Context context;
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
public GetHeigthWidth(Context context) {
this.context = context;
}
public int getHeight() {
#SuppressWarnings("deprecation")
int height = display.getHeight();
return height;
}
public int getWidth() {
#SuppressWarnings("deprecation")
int widht = display.getWidth();
return widht;
}
}
From MainActivty:
GetHeigthWidth ghw= new GetHeigthWidth(getApplicationContext());
int height = ghw.getHeight();
int width =ghw.getWidth();
Aslo get Height and Width 0.
So, How to get Actual Height and width in this>
call your getHeight(); getWidth() stuff in
int height;
int width;
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(globalListner);
OnGlobalLayoutListener globalListner = new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
height = view.getHeight();
widht = view.getWidth();
}
});
and dont forget to remove that global listner at the End :)
Try this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
setContentView(R.layout.yourLayout);
}
you need to initialize your activity.
UPDATE
You need to do this:
import android.content.Context;
import android.view.Display;
import android.view.WindowManager;
public class GetHeigthWidth {
public final static int height = 0;
public final static int widht = 0;
Context context;
WindowManager wm;
Display display;
public GetHeigthWidth(Context context) {
this.context = context;
wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
display = wm.getDefaultDisplay();
}
public int getHeight() {
#SuppressWarnings("deprecation")
int height = display.getHeight();
return height;
}
public int getWidth() {
#SuppressWarnings("deprecation")
int widht = display.getWidth();
return widht;
}
}
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

android how to get height and width in onCreateView

i'm creating my view programmatically, no xml at all and need the real size of the area i'm working in. i have been using this code:
public static class DummySectionFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
Context context = getActivity();
int tabNo = getArguments().getInt(ARG_SECTION_NUMBER);
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// check display size to figure out what image resolution will be loaded
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Point size = new Point();
display.getRealSize(size);
display.getSize(size);
int width = size.x;
int height = size.y;
but that doesn't take into account all the other 'decoration' like title bar and nav bar
the docs say not to use display metrics to get the size but how can i find out the size of the view in onCreateView? i tried container.getHeight() but that returns 0
i guess the issue is the view hasn't been created yet but there must be a parent to onCreateView that knows how big the view will be right?
how do i find that?
thanks
FYI, this works:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
Context context = getActivity();
int tabNo = getArguments().getInt(ARG_SECTION_NUMBER);
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// check display size to figure out what image resolution will be loaded
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Point size = new Point();
display.getRealSize(size);
display.getSize(size);
int width = size.x;
int height = size.y;
switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_XXHIGH: height -= 72; break;
case DisplayMetrics.DENSITY_XHIGH: height -= 64; break;
case DisplayMetrics.DENSITY_HIGH: height -= 48; break;
case DisplayMetrics.DENSITY_MEDIUM: height -= 32; break;
case DisplayMetrics.DENSITY_LOW: height -= 24; break;
default:
Log.e("onCreateView", "Unknown density");
}
}
but it doesn't seem like a clean way to do it. i guessed the xhigh and xxhigh, i haven't found the actual numbers yet
ALSO i should add that the view is created from onTabSelected in the MainActivity like this
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, show the tab contents in the
// container view.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}
i think i have it
i think getSize returns the realsize minus the navigation bar, so i need to subtract the action bar
so now i do this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
Context context = getActivity();
int tabNo = getArguments().getInt(ARG_SECTION_NUMBER);
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// check display size to figure out what image resolution will be loaded
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
TypedValue tv = new TypedValue();
int actionBarHeight=0;
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
height -= actionBarHeight;
//Log.d("MainActivity", "onCreateView tabNo="+tabNo +" display width="+width+" height="+height+" rotation="+rotation);
switch(tabNo){
case 1:
return onCreateViewScoreFragment(context, width, height);
case 2:
return onCreateViewFilesFragment(context, inflater, width>height);
default:
return onCreateViewSettingsFragment(context, inflater, width>height);
}
}
it works on my tablet, i'm not 100% sure about other devices, i'll have to wait and see what my customers say

Categories

Resources