Android set layout depending screen size [duplicate] - android

This question already has answers here:
How to support different screen size in android
(9 answers)
Closed 8 years ago.
First, I must say that i am beginner. But have made an application, that works on my Nexus 7 tablet. But i want to make it to work in all devices. Yes, I know, that question has been there before. But I want your opinion, will it work if etc...
public void setLayout()
{
DisplayMetrics dm = new DisplayMetrics(); //avoid this it just gets screen size.
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double ss = Math.sqrt(x+y);
if(ss <7) //n7 If screen size is nexus 7, application will set nexus7 layout
{
setContentView(R.layout.screen7);
}
else
{
setContentView(R.layour.default); //user is not using nexus 7 so we will set default layout
}
But i need to run this method in every activity, that need to set layout. Like in MainActivity, instead of using
setContentView(R.layout.activity_main);
I want to use setLayout(); //so method checks what screen size user is using so we can show the best layout available.
I hope you understand what i want to make ;)
Thank you :)
Please let me know, if you want LogCat error.

I just wanted to say that this way you will rediscover fire. You are taking the wrong path. u don't need to do these things in java. You can do all this in just XML files , android than does it for you. Try to use relational positioning of views , not exact pixels. Android has a nice measure called dp and it signifies to some extent as if u are telling the system where to position things in percentage.
Here is a link that helped me understand better(ignore the small dimensions):
http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html
Another advice i have to give you is try to use RelativeLayout and LinearLayout , you can use match_parent , wrap_content or u can use layout_gravity atribute for your positioning.

Related

Narrow Dialog when starting new Activity in "Theme.Holo.DialogWhenLarge" mode

I feel that I have tried anything to get my dialog fill about 75% (width) of the screen, when it starts. It was always looking great, but since I upgraded to Android 4.4 I cannot get my dialogs to look "normal" but instead they are very small.
When I open a dialog I use the Theme.Holo.DialogWhenLarge as the theme for an activity. All my dialogs are started as activities. I want them to be fullscreen on mobile devices and a dialog (popup) on tablets.
I tried the tips from the following post, but none seem to work:
Resize dialog to take up more screen
Theme.Dialog creates too small screen
Custom dialog too small
This issue is driving me nuts... Any advice?
I don't know what's wrong with me... ;-) It looks like that as soon as I post a question, I find a working solution:
#Override
protected void onStart() {
super.onStart();
// In order to not be too narrow, set the window size based on the screen resolution:
final int screen_width = getResources().getDisplayMetrics().widthPixels;
final int new_window_width = screen_width * 75 / 100;
LayoutParams layout = getWindow().getAttributes();
layout.width = Math.max(layout.width, new_window_width);
getWindow().setAttributes(layout);
}
I'm still not 100% happy with my solution, because I would like to avoid custom code to get the right screen size, but I guess I do have to work more on my layout. But for now, this solution is working for me.

Image view resizing

So I have been facing this problem for a while and still I cant find solution for it. Hopefully you guys can give me a hand in this. I have a cards game that a player can see his cards like any other cards game.
The problem is that a lot of people complained that cards are too small.When I run it on my devicr (galaxy s2) the cards size are good. By chance someone sent me a screenshot from his HTC and the cards are indeed smaller than What I have.
Please note that I have this 3 different density folders and my Images view are set at wrap content.
I Have 2 solutions in mind and I appreciate if you can help further
1) instead of wrap content, I user a predefined width for each screen size (not density).This will garaunter a specifc size (space it occupies)on large screens for example
2) Make the imageview size relative to the width of the screen.I dont know how to do it so any pointers are appreciated.
3) any suggestions?
please help a programmer stuck in this delemma
You can use DesplayMetrics to get the dimensions of the device at run time and use the width and height you get.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
ImageView iv = (ImageView)findViewById(R.id.yourImageViewId);
iv.setWidth(width);
iv.setHeight(height * 2/3);
or something like that. this way you will never get such issues.
Do you have images in different sizes for different densities? Which scale type are you using for ImageViews?
You can create different layouts for densities but I would start with experimenting with scale types - http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

How to make my android component as size dependent for various display screen sizes?

I am making an android component which allows user to pick date from it. It can be helpful for developer who wants user to select date in his app. In my basic view, I have TextView where date from pop up will be populated into it and I have a button beside TextView. When a User clicks on the button, my component gets popped out and displays Dates. The component gets pops out in a Popup window and shows dates as month view and user also can switch from next-previous months, next-previous years just like we do in Calendar. Check the Image.
http://s15.postimage.org/ujw8py60b/stackoverflow.jpg (Sorry, I couldn't upload an image here because I am not allowed as I am new User here)
Each date is a TextView with the width of 35 and height as 30 set by me. DaysDisplayBar is also of some size set by me. So this component's whole width is 245 and height is around 200. Which is for mobile screen size.
I want to make this component as size dependent for various screen display sizes. For e.g. If it is being viewed on Tablet or Pad, it should be bigger in size than what its size on mobile phone screen. That is, For various displays its size should be changed to some value like max 1/3 of display size or like that something.
What can be the solution for this? According to me, some mathematics is needed here, some formula, equations etc. how about Parabola? Please help, I am dumb in maths totally. Thanks! :D
"Each date is a TextView with the width of 35 and height as 30 set by me. DaysDisplayBar is also of some size set by me. So this component's whole width is 245 and height is around 200. Which is for mobile screen size."
^^ is the problem. Sizes should be defined relative to the layout, not absolute. For example, the calendar has 7 columns (one for each day). Instead of making each one 35px, make each 1/7th of the screen.
SO:
I am assuming a DaysDisplayBar is a row containing 7 TextViews (one for each day). If that is true, why not call it a Week? Either way, The trick is in layout_wieght. Make all elements fill_parent, and all with the same weight of 1. This will evenly distrubate all elements in the parent. In your case, the parent is a DaysDisplayBar.
SO:
set DaysDisplayBar attribute `layout_width="fill_parent"
For each TextViewset attribute layout_width="fill_parent" ANDlayout_weight="1"`
hope that helps!
First of all, make sure you use density pixels (dip) instead of pixels.
Second, you can get the screen width and height, and from there, calculate your component size.
You can get the screen dimensions using the Display class getSize() method:
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int screenWith = screenSize.x;
Or you can get the parent view dimensions:
MarginLayoutParams params = (MarginLayoutParams)parentView.getLayoutParams();
int padding = parentView.getPaddingLeft() + parentView.getPaddingRight();
int margin = params.leftMargin + params.rightMargin;
int measuredWidth = parentView.getMeasuredWidth() - padding - margin;
That way you know how much space you have inside the parent view element for your component.
Remember to convert any hard coded value to dip, you can do it this way:
public static int getDensitySize(float size) {
float density = getResources().getDisplayMetrics().density;
return (int)(density * size);
}
You do all of this from your onMeasure method to set your view size, and later on the onDraw you'll use this measure to draw your component.

Screen Size Honeycomb Menu android

Hi I am trying yo get the screen size of screen minus the menu bar at the bottom.
I know I could just subtract a constant number according to the resolution of the device but it is a super ugly hack. I am sure google people are not dumb enough to forget to give the user a function to get the height of the bottom menu bar so I can subtract it from the full screen size
This is a similar post.
Size of android notification bar and title bar?
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
what_i_need =metrics.heightPixels-buttomMenuHeight();
I need buttomMenuHeight();
i could not find it in the API. I don't really care about the backward comparability at this point
Thanks
Why not use the height of the activity's top-level View? e.g. if your activity contains a full-height LinearLayout, use its height, so you don't have to know the menu height, or noticiation bar height.
I think you're treading a dangerous road doing this kind of calculation, as you don't know what the future layouts for Android might be, or how they might differ on Google TV, or whatever.
Try calling getHeight() after onLayout. The view is not sized on inflate.
EDIT:
Or since Activity does not have onLayout you might wait for onSizeChanged.
(Reference How to know when activity is laid out?)
I am drawing image in native code and I need to pass the size of the screen to C.
I tried to get the Height by getHeight() of the view but that returns 0 always even after setContentView is called.
RelativeLayout masterLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
masterLayout.setLayoutParams(params);
setContentView(masterLayout);
when I try masterLayout.getHeight(), it returns 0.
I expected to get the full screen height as I said fill_parent.

Programmatically resize of view depends on android version?

I am facing a weird behavior regarding the subject.
I have a very simple layout containing only an empty RelativeLayout.
Once I have input form the user this relative layout is filled with square tiles to achieve a mosaic-like effect. Each tile is made by a FrameLayout containing two images (only one is drawn at any given time). It is not possible for me to put the tiles in the XML layout because I do not know in advance how many of them I will need.
In the onSizeChanged of my relative layout, I force a resize on all the tiles to fit the new size.
The code is something like this:
public void resizeTiles(int w, int h) {
int l1 = w / X; int l2 = h / Y;
int tileS = (l1 <= l2 ? l1 : l2);
android.view.ViewGroup.LayoutParams lp;
for (Tile t : myTiles) {
lp = t.getLayoutParams();
lp.width = tileS;
lp.height = tileS;
}
}
In my manifest file I have the following:
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="4"
/>
Thus, target system is 1.6.
So far so good, this is working like a charm ... but only on 2.2.
The same binary placed on emulator 2.1-update1 or previous is giving me back an empty layout (I also tried a couple of physical devices, same result).
Debugging, I tracked down the problem is in the resize; commenting out width and height assignments I see the tiles but with distorted proportions.
Any suggestion on how to make this working ?
Thanks in advance.
onSizeChanged() is called late in the layout process, once the final size has been determined. There may already have been some layout passes that have happened down through the hierarchy at that point.
Basic answer is: layout params are for telling the view's parent its layout params prior to is performing a layout. If they change, the layout must be invalidated to perform a new complete layout with the new params. You should never change these in the middle of a layout.
The best thing to do is just write your own layout manager. (If you are doing layout of tiles, RelativeLayout does a ton more stuff that you don't need, making it a lot less efficient than necessary.) It is actually not very hard to write a simple layout manager.
You can use the simple layout managers in the framework as a guide:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/AbsoluteLayout.java
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/FrameLayout.java
(Note that even these are a lot more complicated than you probably need, since they are intended to be general purpose for the layouts they are implementing. We really should have an API demo of a custom layout that is truly simple.)

Categories

Resources