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;
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>
Is the source of the slider and the lesson link:
http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
In this case, the width of the file "left_menu.xml" determined TextView (android:layout_width="260dp")
How do I set the width to "LinearLayout" file "left_menu.xml" depending on the device screen? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.
To set the LinearLayout or TextView width to 1/3 of the device screen:
first of all get the device screen width:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams and set it to the Text:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want
your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
LinearLayout linear = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(params);
As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this:
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(0, 100);
The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs:
(int) (<numberOfDPs> * getContext().getResources().getDisplayMetrics().density + 0.5f)
LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;
The above answers are correct. I'll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute. If so, consider updating weight in your LayoutParams object.
Another good practice is to retrieve the layout params object from the view you're trying to adjust, instead of creating one from scratch and having to set all the values.
LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 100,
/*weight*/ 1.0f
);
YOUR_LinearLayout.setLayoutParams(param);
The easy way to do that, set value of layoutParams, and please notice this size is not in DP.
view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();
I am using this code and the view does not fit the whole screen of the device.
DisplayMetrics metricsLandscape = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metricsLandscape);
relVideoHolder.setLayoutParams(new RelativeLayout.LayoutParams(metricsLandscape.widthPixels, metricsLandscape.heightPixels));
videoView.setLayoutParams(new RelativeLayout.LayoutParams(metricsLandscape.widthPixels, metricsLandscape.heightPixels));
Log.e("DIMENSIONS", "WIDTH by HEIGHT"+ metricsLandscape.widthPixels+"by"+metricsLandscape.heightPixels);
Log.e("VIDEOHOLDER dimension WxH", ""+relVideoHolder.getWidth()+"x"+relVideoHolder.getHeight());
Log.e("VIDEOVIEW dimension WxH", ""+videoView.getWidth()+"x"+videoView.getHeight());
I find this weird because the result of the Logcat is here;
02-14 17:17:25.881: E/DIMENSIONS(6314): WIDTH by HEIGHT 1196by720
02-14 17:17:25.881: E/VIDEOHOLDER dimension WxH(6314): 720x410
02-14 17:17:25.881: E/VIDEOVIEW dimension WxH(6314): 720x403
I wonder why they have different values. Sometimes, the view covers all the screen, but most of the time, it does not fit the full screen.
Please help me.
By the way, I call these codes onConfigurationChanged when Landscape orientation. Thank you.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
I just add getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); to my code and it works like a charm.
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);