1

I am new to android. I am starting to try and build a methods library. I am much more comfortable in java, and since everything in run time seems to happen in java, I am trying to build methods dealing with the GUI in java. I do not have much so far and I am looking for away to define at what x and y pos to draw objects. This is what I have so far:

//method to get width or Xpos that is a one size fits all
public int widthRatio(double ratioIn){
    DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenWidth = dm.widthPixels;      //gets screen height
    double ratio = screenWidth/100;           //gets the ratio in terms of %
    int displayWidth = (int)(ratio*ratioIn);  //multiplies ratio desired % of screen 
    return displayWidth;
}

//method to get height or Ypos that is a one size fits all
public int heightByRatio(double ratioIn){
    DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenHeight = dm.heightPixels;    //gets screen height
    double ratio = screenHeight/100;          //gets the ratio in terms of %
    int newHeight = (int)(ratio*ratioIn);     //multiplies ratio by desired % of screen
    return newHeight;
}

//sets size of any view (button, text, ...) to a one size fits all screens
public void setSizeByRatio(View object, int width, int height){
    ViewGroup.LayoutParams params = object.getLayoutParams(); // gets params of view
    params.width = widthRatio(width);                         // sets width by portion of screen
    params.height = heightByRatio(height);                    // sets height by portion of screen
}

So if I have a Button named button, and I say setSizeByRatio(button, 25, 50); its sets the button height to 25% percent of the screen and its height to 50% of any screen.

My main question is how do you set the x and y position that you want it to start drawing like you would in reg java? I ran across layout(l, t, r, b); but it only sets the x and y relative to the parent.

Next questions are what else as far as GUI methods should I learn? and I know this is going to kill alot of people but how do I comment in XML? I am as new to XML as I am to android.

JBreezy901
  • 197
  • 2
  • 8
  • Check answers for this question: http://stackoverflow.com/questions/3441475/android-change-absolute-position-of-a-view-programmatically This might help. – Reena Mar 15 '12 at 05:28
  • Well on further thought. I guess everything has to be in a parent. even if that parent is the main layout. I still would like to know how to set the x and y pos without having to do the height and width though. – JBreezy901 Mar 15 '12 at 05:33
  • @Reena Thanks that answers my question. I guess I need to get better at looking. I looked for a good while and did not see that. – JBreezy901 Mar 15 '12 at 05:44
  • Comments in xml are created like this: – Krøllebølle Mar 15 '12 at 09:41

1 Answers1

1

This isn't really relevant but it cleans up one line of code (can be used as a reference to clean up more lines of code if you have more things like this).

This way, the DisplayMetrics applies to both of them rather than having to input it each time you get another axis.

//method to get width or Xpos that is a one size fits all
DisplayMetrics dm = new DisplayMetrics() {    // This line now applies to both int and gets screen properties
public int widthRatio(double ratioIn){
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenWidth = dm.widthPixels;      //gets screen height
    double ratio = screenWidth/100;           //gets the ratio in terms of %
    int displayWidth = (int)(ratio*ratioIn);  //multiplies ratio desired % of screen 
    return displayWidth;
}

//method to get height or Ypos that is a one size fits all
public int heightByRatio(double ratioIn){
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenHeight = dm.heightPixels;    //gets screen height
    double ratio = screenHeight/100;          //gets the ratio in terms of %
    int newHeight = (int)(ratio*ratioIn);     //multiplies ratio by desired % of screen
    return newHeight;
    }
}
Kurty
  • 475
  • 2
  • 16
  • Thanks it is useful and I am glad someone looked at my code objectivly. I need all the help I can get Lol. I orig thought of doing it that way but my teacher always makes a big deal of declaring variables globaly. I guess it just stuck. And I want the methods to be able to stand alone with out alot of outside variables so I can just copy and past them in new projects. But I am going to be calling it alot and it is better that creating a new one throwing it away and creating a new on over and over – JBreezy901 Mar 15 '12 at 05:42