Is there a layout, that would allow me to make it in absolute values, but when it would be on larger / smaller screen, it would stretch and adjusted those values, to fit onto the screen but preserve the same look?
Relative layout still keens on exact values.
For example, I have a button and I want it to have it the width of 1/3 of the screen of every device.
Setting manualy in code the width of an element to (for example) screenWidth/3 works. Yet I don't think it's clean. But this technique works.
Find device dimensions at runtime and set width of button at runtime.
Display mDisplay = getWindowManager().getDefaultDisplay();
int deviceWidth = mDisplay.getWidth();
int deviceHeight = mDisplay.getHeight();
button.getLayoutParams().width = deviceWidth / 3;
Give the values in dpi of your layout and view and they will adjust themselves on any screen
Related
In my project, Button added to the view dynamically and how many number of button is also unknown. So to carry these button i have created linearlayout(horizonatal). Problem is when button exceeded several limit(say 5) then 6 button wont draw over the screen, i don't want scroll, i want all the button on screen, wont matter if buttons will created below the buttons that already created. How to achieve this. How to know that screen is filled with buttons?
It sounds like you will have to dynamically keep track of how much screen space is being occupied by buttons based on the height and potentially the width of the screen. To get the screen size in pixels you can do something like
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
or
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
// displayMetrics.heightPixels will give you the height
// displayMetrics.widthPixels will give you the width
Then you just have to compare this to the total number of pixels being used by buttons. Keep a counter of buttons created. Since you know the pixel height per button, just multiply that by the number of buttons created and you will know if it exceeds the height.
If you need the size in dp you can divide the values from displayMetrics.heightPixels or displayMetrics.widthPixels by the density
float dp = displayMetrics.heightPixels/displayMetrics.density
As seen in this image:
I have a rounded rectangle that is the size of 1082 x 1796, and I used getWindowManager().getDefaultDisplay() to get and print the pixel values of the screen as seen in the circle bottom left. However, when I draw this bitmap, this happens:
Is the screen size different from the getDefaultDisplay() method? How do I fix this?
I think your shape is seen by the system as a 9patch. It use the first and last lines/columns in order to know how to resize the image in order to display it consistently.
That's why there is a 2px difference in width and height. I think you can correct it by modifying the png and removing the first and last lines/columns, or just use another way to draw the image. Maybe using another format (jpg ? bmp ?) for the image could do the job. I'm not sure.
you can do that with calculation to fit all screen devices based on width and height of the device screen:
DisplayMetrics dm = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * imageHeight / imageWidth;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
Having some imageViews in a list row.
The count of the imageView in the row are different, i.e. could be three or two imageView.
For the width it could be specified using
layout_width="0dp"
layout_weight="1dp"
for every imageView in the row. But the problem is the height, it should be 90% of the width of the imageView if the row has two imageView, and 80% of the width is the row has three imageView.
Is there way to do that in xml?
If not, where is the best place to do calc and set the height without too much overhead on the drawing?
Thanks!
Not sure if there is a way to do in xml. But this works in code, do it in the adapter::getView(), after get the corresponding imagView, change its height layoutParam but keep the width one.
Point size = new Point();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getSize(size);
int screenWidth = size.x;
int columnNumber = (isItThree ? 3 : 2);
double ratio = (isItThree ? 0.8 : 0.9)
int desiredImageHeight = screenWidth/colunmNumber * ratio;
ViewGroup.LayoutParams imglayoutParams = imgView.getLayoutParams();
imglayoutParams.height = desiredImageHeight;
imgView.setLayoutParams(imglayoutParams);
In order to maintain the ratio between height and width the best practice is:
Defining one of its property with a value in dp and another with wrap content.
In your case:
android:layout_width="20dp"
android:layout_heignt="wrap_content"
By doing this the height should be adjusted according to the ratio of given width.
In my app, I download images from my web server (each having a different resolution) but I would like to show these images on a fragment screen consistently.
Ideally, I would like the image to have width="match_parent" and height to take 1/3 of the screen EXACTLY. Furthermore, the image should be shown in a as it's part of a content layout with other controls that could possibly grow larger than the screen height (hence the need for scrolling).
I have tried putting the image and the rest of the content in a LinearLayout and then setting the weight to 1 (with max weight being 3), but since the image and the other contents are in a scrollview, it doesn't quite seem to work. the image is either too large or too small (depending on the orientation and the resolution of the image) and my 1/3rd settings don't seem to be respected.
Is there any way to do this other than fixing the height to a pre-determined (dp) value? I would like to avoid that unless there's no other choice.
Many thanks,
You could always set the height for each of the images programatically to 1/3 the height of the screen, like so
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
ImageView img = (ImageView) findViewById(R.id.image);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) img.getLayoutParams();
params.height = (int)height / 3;
img.setLayoutParams(params);
I have a RelativeLayout that has a number of controls in it. On some screens, there might not be enough room for this layout as well as the other screen elements. In this case, I'd like to use an alternate layout that excludes some controls and has less padding.
How can I implement this in a performant manner? The smaller layout should only be loaded if the larger layout definitely won't fit. I don't want to use the various screen size qualifiers because whether the layout will fit is a function of the width and height of the screen and the additional visible controls and that seems like it will get too complicated.
I Personally think you should use the new screen size qualifiers. It's how Android is designed and I think you should follow those rules. It will very probably also make the whole development process a lot easier!
But if you insist on doing it yourself, the only way I can think of to "hardcode" it, would be something like this:
Step 1: obtain the screen size
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
or if you're targeting a lower API level:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Step 2: pick an alternative layout depending on the screen size
if(width < value_x_small && height < value_y_small){
setContentView(R.layout.main_small);
} else if(width > value_x_small && height > value_y_small){
setContentView(R.layout.main_large);
} else {
setContentView(R.layout.main_larged);
}
This code allows you to select your own resources depending on the screen size in px. Its probably better to convert the px values to DP (DPI) and use those to make your decisions.You might also want to add a check on the current screen orientation.
You can implement your controls as separate Fragments and position them depending on screen size. Here is more info about Fragments and their Design Philosophy .