So I have this code, which is called onConfigurationChanged :
private void setVideoLayoutParams(){
LinearLayout.LayoutParams params;
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
// this works as expected
showNavigationAndStatusBars();
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
params = new LinearLayout.LayoutParams(size.x, size.x * 9 / 16);
}else{
hideNavigationAndStatusBars();
// this does not work properly
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
params = new LinearLayout.LayoutParams(size.x, size.y);
}
controller.setPauseButtonSize();
Log.d(TAG, "video layout params set : " + params.width + "/" + params.height);
videoFrame.setLayoutParams(params);
}
private void hideNavigationAndStatusBars(){
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
private void showNavigationAndStatusBars(){
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(uiOptions);
}
When the screen is rotated into landscape, the navigation and status bars disappear. However my videoFrame does not fill the screen.
A small portion on the right side of the screen remains blank and is equal to height of the navigation bar.
Another small portion on the bottom of the screen equal to the height of the notification bar remains blank.
When the params are created, my logger shows 1794/1080. When I touch the screen, I log the size of the videoFrame and it shows 1794/1009. My guess is it should show, in both cases, 1800/1080.
What am I doing wrong?
Put your Videoview inside a Relative layout and add alignparentright, alignparentleft, alignparenttop and alignparentbottom "true". It will be perfect.
Your layout is like this right?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView android:id="#+id/myvideoview"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
</VideoView>
</RelativeLayout>
And try
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
videoView.setLayoutParams(params);
Related
Set i have a fragment CustomSwitchFragment, that i want to use as followed:
<fragment android:name="m6world.test_toggle_yes_no.CustomSwitchFragment"
android:id="#+id/custom_switch_fragment"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="horizontal" />
i don't want to have to input 80dp, i would like to be able to put wrap_content and the fragment will be set whatever width i used when created it.
Basically i don`t want to have to tell people who are gonna use my fragment, to set it to 80dp specifically.
I guess you can use something like this
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width * 70 / 100;
params.height = height * 30 / 100;
getWindow().setAttributes(params);
getWindow().getAttributes().verticalMargin = 0.2F;
I'm trying to create a AlertDialog which will show an image in fullscreen. My problem is that the dialog does not fit the image.
I could scale the image to fit the horizontal or vertical limit (depending on the screen oreintation) with some code that I found. However , the dialog shows an empty space that I want eliminate.
My dialog shows this:
But I want that it fits the image without the left and right empty space, like this:
My code to create the alert dialog:
void showImage(String path, String name) {
// Get image from the aboslute path
BitmapDrawable bitmap = new BitmapDrawable(getActivity().getResources(), path);
// Construct the dialog
AlertDialog.Builder imageDialog = new AlertDialog.Builder(getActivity());
imageDialog.setTitle(name);
imageDialog.setCancelable(true);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.thumbnail, null);
ImageView image = (ImageView) layout.findViewById(R.id.imageView);
image.setImageDrawable(bitmap);
imageDialog.setView(layout);
imageAlert = imageDialog.create();
// Fullscreen
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
WindowManager manager = (WindowManager) getActivity().getSystemService(
Activity.WINDOW_SERVICE);
lp.copyFrom(imageAlert.getWindow().getAttributes());
lp.width = manager.getDefaultDisplay().getWidth();
lp.height = manager.getDefaultDisplay().getHeight();
imageAlert.getWindow().setAttributes(lp);
image.setMinimumWidth(lp.width);
image.setMinimumHeight(lp.height);
imageAlert.show();
}
My XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/imageView"/>
</LinearLayout>
I have tried scaleType = fitXY, but it deforms the image.
How can I make the dialog fit the image keeping the same aspect ratio? Any help would be appreciate
I could find a way to solve my problem, but I dont know if its the best way to do it. I calculated the image size manually and set the image and dialog to this size.
First, I check which dimesion will limit the size of the image, and set the screen value to that dimesion. Then, I calculated the other dimesion in relation to the previous to keep the same ratio and the image is not deformed.
// Get screen size
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
// Get image size
int imgHeight = bitmap.getBitmap().getHeight();
int imgWidth = bitmap.getBitmap().getWidth();
// Get dialog params
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(imageAlert.getWindow().getAttributes());
// Which dimension is bigger in relation to screen size
if (((float)imgHeight) / screenHeight > ((float)imgWidth) / screenWidth) {
// Height is bigger so width is calculated on relation to it to keep the same ratio
lp.height = screenHeight;
lp.width = (int) (imgWidth * ((float) screenHeight) / imgHeight);
}
else {
// Width is bigger so height is calculated on relation to it to keep the same ratio
lp.height =(int) (imgHeight * ((float) screenWidth) / imgWidth);
lp.width = screenWidth;
}
// Set the new size
image.getLayoutParams().width = lp.width;
image.getLayoutParams().height = lp.height;
image.requestFocus();
imageAlert.show();
imageAlert.getWindow().setAttributes(lp);
Use
imageDialog.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
Try doing this imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); and also make height and width match_parent as shown below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/imageView"/>
</LinearLayout>
everyone. I try to attach full screen view to moto360. However, when it works on LG/samsung square devices. It left a black out bar on bottom, which is about same size of original black bar on bottom. Is this a bug or what? anyone ever found this happened?
Here's my code:
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
float width = metrics.widthPixels;
float height = metrics.heightPixels;
Log.d(TAG, "width: " + width + " height:" + height);
// attatch view to windows
WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
wmParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.flags = LayoutParams.FLAG_NOT_TOUCHABLE
| LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.TYPE_SYSTEM_OVERLAY;
wmParams.gravity = Gravity.TOP | Gravity.TOP;
wmParams.x = 0;
wmParams.y = 0;
wmParams.width = metrics.widthPixels;
wmParams.height = metrics.heightPixels;
wManager.addView(view, wmParams);
by the way, change metrics.widthPixels to Match_Parent is not working.
Any clue is appreciated. For now I suspect it is a bug on moto360 rom which consider the screen size as (real space - bottom bar size). So, when the view is attached, is return some black space on bottom.
i try to check screen resolution
i googled and i found one example,but i can not check screen resolution.i try check 480X800 and 720X1280 screen resolution
int screenSize = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
switch (screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
mapimg.getLayoutParams().height = 400;
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
mapimg.getLayoutParams().height = 200;
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
break;
default:
}
Layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/relativeLayout1"
android:layout_alignParentLeft="true" >
<ImageView
android:id="#+id/mapimages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.google.android.gms.maps.MapView
android:id="#+id/pointMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/mapimages"
class="com.google.android.gms.maps.MapFragment" >
</com.google.android.gms.maps.MapView>
</RelativeLayout>
this is a my code
i try to my imageview's height would be different in same screen resolution
in 480X800 height 200 and in 720X1280 400
when i run program in a both screen resolution imageview has 200 height
what am i doing wrong? if anyone knows solution please help me
thanks
After setting parameters like height to a view that has already finished its requestLayout() pass, you need to again request for system to measure it. Do this at the end of switch statement:
mapimg.requestLayout();
you can get the screen size like so:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
not if you are not doing this from an activity you can also do:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
use this code for get screen dp
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;
and set your image width and height with this line :
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
dpWidth/2,dpHeight/2);
image.setLayoutParams(lp);
I what the Dialog to cover up the whole screen width.
Hence:
Dialog dialog = new Dialog(this);
LayoutParams params = dialog.getWindow().getAttributes();
params.height = LayoutParams.MATCH_PARENT;
params.width = LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(params);
But the result is:
Skip is a button in a parent layout (MATCH_PARENT as width and height
and 10dp padding and orange background).
Even in this answer the final result has some gaps on sides.
Is there a way to cover the whole screen width without any gaps?
The solution that worked for me was the following:
Grab the device dimensions using:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Then set the width of the dialog to the screen width
params.width = size.x;