0

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

2 Answers 2

0

"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!

8
  • Well above popup size is suitable for mobiles having resolutions like 480 x 854. For Resolution of Tablet or Pad, it should automatically become bigger and even more bigger for Google TV etc. So how can I do that? Hope you got my problem! Thanks:)
    – noobnicks
    Mar 2, 2012 at 16:03
  • You set the width of each TextView to 35 and the Height to 30. Can post the line or two of code you used to do this?
    – edthethird
    Mar 2, 2012 at 16:18
  • I have created DateView as a class which extends TextView. In DateView, this.setHeight(30); this.setWidth(35); After that in my Calendar class I wrote DateView dtView = new DateView(this.getContext()); and these are added into TableRow and then that TableRow into TableLayout .
    – noobnicks
    Mar 2, 2012 at 16:27
  • You are just concentrating on this Textviews only, by the way I have some buttons around this TableLayout and some other views which I can't show in an image. So I do not want the approach which first check screen size, if true, then set size for textviews and other views xxx or for screen size yyy set zzz. I want this automatic and I think this can be achieved by some equation, can not getting that..
    – noobnicks
    Mar 2, 2012 at 16:27
  • OK, then apply layout_wieght=1 and layout_width="fill_parent" to every element on the horizontal access. This will evenly scale and every element with a matching layout_wieght. It's tough without code samples, but the fix is honestly to use layout_wieght
    – edthethird
    Mar 2, 2012 at 16:45
0

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.

5
  • Thanks! I know getting screen size is one of the solution and I can use those height and width variables on which size of my component will be dependent. But for that I need some equation which will take input as screen height and width, will do some calculation and will provide me height and width of my own component. From smaller to bigger screen, Component size will increase gradually but at some period, though screen size gets bigger, by component size will be constant just like Parabola graph. Hope you understood what I am trying to say..
    – noobnicks
    Mar 2, 2012 at 16:09
  • What do you want is to scale your component until a certain size, is that right? If so, it's pretty simple, you set the component scale to, let's say, 1/3 of the screen size. But on a large or xlarge screen this is to much, so you set a limit, hard coded, like 300dip. That way, if you are showing your component on a large screen, it'll have 300dip of witdh, instead of 1/3.
    – Mokkun
    Mar 2, 2012 at 17:53
  • OKay cool, nice, that's also the way. thanks! But u look at graph of parabola, as soon as x increases, curve of parabola also increases and after some time that curve and x axes almost seems as parallel to each other. Same concept I wanna apply here, for small screen, component size will change but for larger, it will change extreme slightly or will remain constant. Something like this can you make it out? Any maths equation can do the job for me here?
    – noobnicks
    Mar 2, 2012 at 18:12
  • Don't see the need for a parabola, but if it's what you want, just apply the parabola math inside the onMeasure, using the screen values. :) This might help: intmath.com/plane-analytic-geometry/4-parabola.php
    – Mokkun
    Mar 2, 2012 at 18:22
  • Also, if one of the answer's helped you to solve your problem, check it as correct. :)
    – Mokkun
    Mar 2, 2012 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.