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);
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>
I need to get the exactly size screen but i cant get it.
I tried this two different codes:
DisplayMetrics display = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = display.heightPixels;
width = display.widthPixels;
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
height = size.y;
width = size.x;
In both cases the height its correct (1080) but the width its not correct. It must return 1920 but its returning 1794. Its a Samsung Galaxy S4 (1920x1080).
I have a full screen app, with no actionbar or navigation bar, etc.
Why is happening this and how can ai get 1920. Im using API >= 14.
Greets
You can try with getRealSize instead of getSize (getRealSize)
Point size = new Point();
getWindowManager().getDefaultDisplay().getRealSize(size);
height = size.y;
width = size.x;
Here you can see my carousel layout xml file.
<com.touchmenotapps.carousel.simple.HorizontalCarouselLayout
android:id="#+id/carousel_layout_event"
android:layout_width="600dp"
android:layout_height="500dp"
android:layout_above="#+id/relativeLayout1"
android:layout_alignTop="#+id/carousel_layout" >
</com.touchmenotapps.carousel.simple.HorizontalCarouselLayout>
I want to set the running android simulator screen width & height to the android:layout_width & android:layout_height...
can anyone help me. thanks
HorizontalCarouselLayout hcl=(HorizontalCarouselLayout)findViewById(R.id.carousel_layout_event);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
hcl.setLayoutParams(new LayoutParams(width,height));
Firs you get the divice size i.e.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int divWidth= size.x;
int divHeight = size.y;
Create a function to set the layout params. i.e.
public void setWidthHeight(View v, int width, int height){
LayoutParams lp;
lp = v.getLayoutParams();
lp.width = width;
lp.height = height;
v.setLayoutParams(lp);
}
then call this function as,
setWidthHeight(yourView,divWidth,divHeight);
I have a layout with an imageview. This imageview should have a 4/3 ratio. In portrait mode i get the width of the screen then compute the height and set the imageView dimensions.
int[] tailles = new int[2];
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int widthScreen = dm.widthPixels;
tailles[0] = widthScreen;
tailles[1] = (tailles[0]*3)/4;
This part works great.
In landscape mode i need to set the imageView width according to the available height. So i need to get the height of the screen minus the status/notification bar.
I saw the value of 48px for the bar in this question. Is it a standard size ?
What the best solution to get the available height regardless of the used device ?
Display d = ((Activity) mContext).getWindowManager()
.getDefaultDisplay();
int h = d.getHeight();
int w = d.getWidth();
in portrait h > w and in landscape w > h
If you're creating an Activity, than you have to have some sort of layout. Get it's root component, and retrieve it's size (getWidth, getHeight). That'll be the available width and height without counting the status bar.