0

I've been trying to get a gridlayout to work using:

<GridLayout
        android:layout_width="match_parent"
        android:layout_height="70dp">

        <TextView
            android:text="sdfgh"
            android:layout_height="wrap_content"
            android:id="@+id/AvgGrade"
            android:layout_row="2"
            android:layout_column="1"
            android:layout_rowWeight="0.2"
            android:layout_columnWeight="0.5"
            android:layout_gravity="fill"
            android:textAlignment="gravity"
            android:layout_columnSpan="1" />

        <TextView
            android:text="words"
            android:layout_height="wrap_content"
            android:id="@+id/Totals"
            android:layout_row="1"
            android:layout_column="1"
            android:textAlignment="gravity"
            android:layout_rowWeight="0.2"
            android:layout_columnWeight="0.5"
            android:layout_gravity="fill"
            android:layout_columnSpan="1" />

        <TextView
            android:text="words"
            android:layout_height="wrap_content"
            android:id="@+id/textView3"
            android:layout_row="2"
            android:layout_column="0"
            android:textAlignment="center"
            android:layout_rowWeight="0.2"
            android:layout_columnWeight="0.5"
            android:layout_gravity="fill"
            android:layout_columnSpan="1" />

        <TextView
            android:text="words"
            android:layout_height="wrap_content"
            android:id="@+id/textView4"
            android:layout_row="1"
            android:layout_column="0"
            android:layout_rowWeight="0.2"
            android:layout_columnWeight="0.5"
            android:layout_gravity="fill"
            android:textAlignment="center"
            android:layout_columnSpan="1" />

        <TextView
            android:text="words"
            android:layout_height="wrap_content"
            android:id="@+id/TotalAvg"
            android:layout_row="0"
            android:layout_column="1"
            android:textAlignment="gravity"
            android:layout_rowWeight="0.2"
            android:layout_columnWeight="0.5"
            android:layout_gravity="fill"
            android:layout_columnSpan="1" />

        <TextView
            android:text="words"
            android:layout_height="wrap_content"
            android:id="@+id/textView6"
            android:layout_row="0"
            android:layout_column="0"
            android:textAlignment="center"
            android:layout_columnWeight="0.5"
            android:layout_gravity="fill"
            android:layout_columnSpan="1" />

    </GridLayout>

My issue is that what the data is loads in the second column the size of the columns changes ever so slightly. I've tried left and center alignment and nothing seems stop the columns from changing. The user will be updating these values repeatedly and the slight 'jiggle' of the columns shifting looks really bad.

Diesel
  • 519
  • 1
  • 6
  • 24

1 Answers1

0

After much frustration I was unable to find a solution in the XML layout. However I did find a solution that seems to be working fairly well programatically.

I added a method called at the end of my OnCreate

public void ResizeTexts(){
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics ();
    display.getMetrics(outMetrics);

    float density  = getResources().getDisplayMetrics().density;
    float dpWidth  = (outMetrics.widthPixels / density);

    text1.getLayoutParams().width = (int)dpWidth;
    text2.getLayoutParams().width = (int)dpWidth;
    text3.getLayoutParams().width = (int)dpWidth;
    text4.getLayoutParams().width = (int)dpWidth;
    text5.getLayoutParams().width = (int)dpWidth;
    text6.getLayoutParams().width = (int)dpWidth;
}

Which i adapted from this link re-setting a TextView height programmatically

then in my XML layout I set it up with:

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="70dp">

    <TextView
        android:text="points"
        android:layout_height="wrap_content"
        android:layout_width="10dp"
        android:id="@+id/text1"
        android:layout_row="0"
        android:layout_column="0"
        android:textAlignment="textEnd"
        />


    <TextView
        android:text="totals"
        android:layout_height="wrap_content"
        android:layout_width="10dp"
        android:id="@+id/text2"
        android:layout_row="1"
        android:layout_column="0"

        android:textAlignment="textEnd"/>


    <TextView
        android:text="score"
        android:layout_height="wrap_content"
        android:id="@+id/text3"
        android:layout_width="10dp"
        android:layout_row="2"
        android:layout_column="0"
        android:textAlignment="textEnd"/>


    <TextView
        android:text="123.4"
        android:layout_height="wrap_content"
        android:id="@+id/text4"
        android:layout_width="10dp"
        android:layout_row="1"
        android:layout_column="1"
        android:textAlignment="textEnd"/>

    <TextView
        android:text="1.23"
        android:layout_height="wrap_content"
        android:id="@+id/text5"
        android:layout_width="10dp"
        android:layout_row="2"
        android:layout_column="1"
        android:textAlignment="textEnd"/>

    <TextView
        android:text="1234 "
        android:layout_height="wrap_content"
        android:id="@+id/text6"
        android:layout_width="10dp"
        android:layout_row="0"
        android:layout_column="1"
        android:textAlignment="textEnd"/>

</GridLayout>

*the layout widths in the xml layout I was using to try and 'break' it, not actually used.

Admittedly, i'm not sure if this is the 'best' way to do this. Personally I feel like the GridLayout XML should be able to do this on it's own. But, I couldn't figure it out.

Diesel
  • 519
  • 1
  • 6
  • 24