Android, set WebView dimensions with px in Java - java

Currently I am using the following to set height and width:
//landsapeWebView is a WebView, and deviceHeight and deviceHeight are ints
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceHeight, deviceWidth));
This seems to work fine on some devices but not others. The only conclusion I've come to is that this is setting the height and width like this:
<WebView android:layout_width="(deviceWidth)dp"
android:layout_height="(deviceHeight)dp" />
when what I want is this:
<WebView android:layout_width="(deviceWidth)px"
android:layout_height="(deviceHeight)px" />
How can I specify the unit of measure?
Extra disclosure for #collusionbdbh
display = getWindowManager().getDefaultDisplay();
if(getResources().getConfiguration().orientation == 1){
deviceWidth = display.getWidth();
deviceHeight = display.getHeight();
}else{
deviceWidth = display.getHeight();
deviceHeight = display.getWidth();
}

Try this:
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

Are you sure it is not an orientation issue?
Regardless of the orientation of the device the width is the horizontal and the height is the vertical.
I would try this:
display = getWindowManager().getDefaultDisplay();
deviceWidth = display.getWidth();
deviceHeight = display.getHeight();
I am pretty sure width should be before height:
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceHeight, deviceWidth));
should be:
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceWidth, deviceHeight));

Related

Crop bitmap issue

I'm using this code to crop an image:
Bitmap raw = ((BitmapDrawable)hWlp).getBitmap();
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager)
context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
wallpaper = Bitmap.createBitmap(raw, width/2, 0, width, height);
My source image (raw) is 800x960 and the target image is 800x480 (screen size). So I don't understand why I get this error:
java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
if in my case x + width (480/2 + 480) is 720 and bitmap.width() is 960
You can use like that :
public static Bitmap cropAndScale (Bitmap source,int scale){
int factor = source.getHeight() <= source.getWidth() ? source.getHeight(): source.getWidth();
int longer = source.getHeight() >= source.getWidth() ? source.getHeight(): source.getWidth();
int x = source.getHeight() >= source.getWidth() ?0:(longer-factor)/2;
int y = source.getHeight() <= source.getWidth() ?0:(longer-factor)/2;
source = Bitmap.createBitmap(source, x, y, factor, factor);
source = Bitmap.createScaledBitmap(source, scale, scale, false);
return source;
}
This is example and you can change variables by displaymetrics
You can get this error only, if your app is in landscape orientation ;)
I think, in this case metrics.widthPixels can return 800px instead 480px.

Imageview on a random position [duplicate]

This question already has answers here:
How can I dynamically set the position of view in Android?
(13 answers)
Closed 6 years ago.
I want myImageView to locate itself somewhere on a random position inside the screen, without pushing textviews away. I found this code in another thread, but I can't get it to work.
myImageView = (ImageView) findViewById(R.id.myImageView);
LinearLayout game = (LinearLayout)findViewById(R.id.game);
int width = game.getWidth();
int height = game.getHeight();
random = new Random();
int x = width;
int y = height;
int imageX = random.nextInt(x-50)+50;
int imageY = random.nextInt(y-100)+100;
myImageView.setX(imageX);
myImageView.setY(imageY);
You can give margin programmatically between certain range, may be you can use device width or height for range. Check this links for setting margin dynamically on run time and get screen dimension.
I Think first get programmatically device size then set image view width & height
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
hear you get width & height now set image view size
android.view.ViewGroup.LayoutParams layoutParams =myImageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
myImageView.setLayoutParams(layoutParams);
and if you still have a problem to change it's size try to put it inside of a LinearLayout and manipulate the imageView size using the layout params:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
myImageView.setLayoutParams(layoutParams);

how to draw a square in the centre of the screen in android

I am tring to draw a square in the center of the screen. I want there to be a slight margin to the left and right of the square so that is is away from the edge, the way I am trying to create it is below. The problem is that it will not display properly on all devices and that when the screen is tilted some of the square is cut off. I think this is because I use rectSide = 1000. does anybody know a better way to do this that will work on any screen size?
int rectside =1000;
canvas.drawRect(width/2 - rectSide/2,
height/2 - rectSide/2,
width/2 + rectSide/2,
height/2 + rectSide/2, paint);
You need to get height and width of device programmatically like this
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Update:As pointed out by #Der Golem take the smaller between width and height so that all sides should be equal
Get the device's dimension:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Next, get the smallest dimension:
int diameter = width;
if (height < width){
diameter = height;
}
Now get an offset, I suggest using some kind of percentage of the device, e.g.
int offset = (int) (0.05*diameter);
diameter -= offset;
Finally draw it:
canvas.drawRect(width/2 - diameter/2 ,
height/2 - diameter/2,
width/2 + diameter/2,
height/2 + diameter/2, paint);
You are right, using an absolute number of pixel is not the good way.
You should adapt your rectSide using display height & width. How to get screen size attributes has already been discussed here.
I also strongly recommend you to read this, to get a better understanding of how to manage multiple screen sizes.

Custom VideoView in full Screen

I have a Video view in height=400 and width= 300 and its playing properly with this attributes.
My task is to play video in full screen mode in portrait mode on Double Tap and also its go to its original size on double tap when it playing in full screen mode.
I also tried with this
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height =displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
RelativeLayout.LayoutParams params =(RelativeLayout.LayoutParams)uservides.getLayoutParams;
params.width = width;
params.height = height;
uservideos.setLayoutParams(params);

How to retrieve layout_height in code

I have a layout that contains several sub-views all which set height like:
<View android:layout_width="fill_parent"
android:layout_height="18dip"
android:background="#ff000000" />
How can I retrieve the value stored in layout_height in code?
Ultimately, what I would like to do is being able to adjust the size defined in the layout so I can dynamically change the value defined by layout_height depending on the resolution of the screen resolution and the pixel density.
Is the only way to do so during the onLayout callback?
It would be much easier to redefined the XML on onCreate.
Comments, advices would be much appreciated.
Thanks
JY
Acces the View's Layout and then access the 'height' field: View.getLayoutParams().height
Should have searched a tad further prior to post a question.
This does the trick it seems...
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
factor_y = (dm.ydpi * dm.heightPixels) / (160 * 480);
factor_x = (dm.xdpi * dm.widthPixels) / (160 * 320);
LinearLayout layout = (LinearLayout) findViewById(R.id.mainlayout);
for (int i = 0; i < layout.getChildCount(); i++)
{
View v = layout.getChildAt(i);
int height = v.getLayoutParams().height;
if (height > 0)
{
v.getLayoutParams().height = (int) ((float) height * factor_y);
}
int width = v.getLayoutParams().width;
if (width > 0)
{
v.getLayoutParams().width = (int) ((float) width * factor_x);
}
}

Categories

Resources