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
}
Related
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;
}
I want to create 10 ImageViews in random position and I want them to be the same, I tried using ImageView[], I got the screen size(width and height) and created random place for each ImageView (in range of screen size). Some of the ImageViews are out of bounds as there are sometimes 7,8 ImageViews I can see. here's my code:
ImageView ball[], you;
int layoutwidth, layoutheight;
Random rand = new Random();
RelativeLayout rlt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rlt = (RelativeLayout) findViewById(R.id.rlt);
you = (ImageView) findViewById(R.id.imageView);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
layoutwidth = size.x;
layoutheight = size.y;
ball = new ImageView[11];
for (int i = 0; i < 10; i++) {
ball[i] = new ImageView(this);
ball[i].setTag(i);
ball[i].setBackgroundResource(R.mipmap.ball);
int randomx = rand.nextInt(layoutwidth+1);
int randomy = rand.nextInt(layoutheight+1);
ball[i].setX(randomx);
ball[i].setY(randomy);
rlt.addView(ball[i]);
}
}
How can I make the code work as I need it?
You can try to replace this
int randomx = rand.nextInt(layoutwidth+1);
int randomy = rand.nextInt(layoutheight+1);
ball[i].setX(randomx);
ball[i].setY(randomy);
rlt.addView(ball[i]);
with
int randomx = rand.nextInt(layoutwidth+1-widthOfImageView);
int randomy = rand.nextInt(layoutheight+1-heightOfImageView);
rlt.addView(ball[i]);
ball[i].setTranslationX(randomx);
ball[i].setTranslationY(randomy);
where widthOfImageView,heightOfImageView is imageview's width & height
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.
i want ImageView's to look same on different screen sizes. To make it easier for you to understand how it should look there are some image:
Biggest problem is that my imageViews are created programmatically. ImageViews layout and size are set by this code
LinearLayout LinearLayoutas = (LinearLayout)findViewById(R.id.LinearLayout);
ImageView ImageViewas = new ImageView(this);
ImageViewas.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayoutas.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ImageViewas.setLayoutParams(params);
ImageViewas.getLayoutParams().height = 650;
ImageViewas.getLayoutParams().width = 950;
params.setMargins(0, 50, 0, 0);
Any ideas how to change this code that my app can look same on diffrent screen sizes?
Well, you can retrieve device resolution and set imageView width and height. Here is the solution.
private class ScreenResolution {
int width;
int height;
public ScreenResolution(int width, int height) {
this.width = width;
this.height = height;
}
}
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
ScreenResolution deviceDimensions() {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
// getsize() is available from API 13
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return new ScreenResolution(size.x, size.y);
}
else {
Display display = getWindowManager().getDefaultDisplay();
// getWidth() & getHeight() are deprecated
return new ScreenResolution(display.getWidth(), display.getHeight());
}
}
..................
LinearLayout LinearLayoutas = (LinearLayout)findViewById(R.id.LinearLayout);
ImageView ImageViewas = new ImageView(this);
ImageViewas.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayoutas.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ScreenResolution screenRes = deviceDimensions();
ImageViewas.setLayoutParams(params);
ImageViewas.getLayoutParams().height = screenRes.height;
ImageViewas.getLayoutParams().width = screenRes.width;
params.setMargins(0, 50, 0, 0);
Also when the device orientation will be changed, the width and height should be swapped. You can do that in onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);
// swap device width and height here and re-assign
}
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;