1

I have a layout with several views and one of these views have to be scaled based on screen size. As of now I have it set to 250dp as it looks good on a 5.1 inch device, but when used on smaller devices it isnt as good any more. I can't use wrap content as it will make the looks of it very bad and the size of buttons too small, so it has to be 1/3rd of the width of the screen. Is it possible to do that in the layout code, or does it have to be doen in java code?

  • from the links you gave... The actual measurement work of a view is performed in onMeasure(int, int), called by this method. Therefore, only onMeasure(int, int) can and must be overridden by subclasses. – silentsudo Jan 20 '16 at 19:36

3 Answers3

1

Is it possible to do that in the layout code

Yes it is. It is pretty simple by using android:layout_weight and android:weightSum attributes of LinearLayout :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:weightSum="3"
   android:orientation="horizontal"> 

   <View 
      android:id="@+id/thirdView" 
      android:layout_height="wrap_content" 
      android:layout_width="0dp" 
      android:layout_weight="1" />

</LinearLayout>
The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
0

You need to override onMeasure(...) then do your view resizing login inside this function. Here is the perfect explanation and solution. Some reference code from this link goes below...

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int desiredWidth = 100;
    int desiredHeight = 100;

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int width;
    int height;

    //Measure Width
    if (widthMode == MeasureSpec.EXACTLY) {
        //Must be this size
        width = widthSize;
    } else if (widthMode == MeasureSpec.AT_MOST) {
        //Can't be bigger than...
        width = Math.min(desiredWidth, widthSize);
    } else {
        //Be whatever you want
        width = desiredWidth;
    }

    //Measure Height
    if (heightMode == MeasureSpec.EXACTLY) {
        //Must be this size
        height = heightSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
        //Can't be bigger than...
        height = Math.min(desiredHeight, heightSize);
    } else {
        //Be whatever you want
        height = desiredHeight;
    }

    //MUST CALL THIS
    setMeasuredDimension(width, height);
}
Community
  • 1
  • 1
silentsudo
  • 6,730
  • 6
  • 39
  • 81
0

You could have a look at the PercentRelativeLayout from the Percent Support Library. This is a RelativeLayout in which you can specify it's Views sizes in percentages, e.g. set its width to 33%.

<android.support.percent.PercentRelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <Button
         app:layout_widthPercent="33%"
         app:layout_heightPercent="33%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%" />

</android.support.percent.PercentRelativeLayout>
Floern
  • 33,559
  • 24
  • 104
  • 119
  • i keep getting an exception that basicly is that it doesnt find the values in my game's package –  Jan 20 '16 at 19:50
  • @TheRealPolarbear What's the exact error message? Did you include the library correctly? – Floern Jan 20 '16 at 19:59
  • did i have to add a .jar? that explains the error –  Jan 20 '16 at 20:13
  • ok it wasnt as easy as that i see. im using eclipse, so do i have to make a build.gradle file and add it to whatever so it is imported like that or can i just add it to the java build path? –  Jan 20 '16 at 20:20
  • @TheRealPolarbear Would be easy with Android Studio.. For Eclipse you can try to include the library located at [android-sdk]/extras/android/compatibility/percent/ – Floern Jan 20 '16 at 20:25
  • that isnt a valid path. ill see if i have all the extras installed though –  Jan 20 '16 at 20:27
  • found it at [android-sdk]/extras/android/percent/ but i tried @AleksandrMedvedev 's weight system and it worked –  Jan 20 '16 at 20:30