0

I am a new programmer. I having trouble with some of the screens my grid new screen shot appears as wired rows (maybe because the width of the screen is narrow ) . I've uploaded picture of my problem and my code. where did I go wrong? hw can I fix it?

my problem

   <GridLayout
    android:id="@+id/gameTableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="80dp"
    android:layout_marginTop="10dp"
    android:gravity="center"

    android:orientation="horizontal"
    android:visibility="invisible">

    <!-- the 16 buttons -->
    <!-- 1:     (0,0) -->
    <Button
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/gridLayout"
        android:layout_alignStart="@+id/gridLayout"
        android:layout_column="0"
        android:layout_columnWeight="1"
        android:layout_gravity="fill"
        android:layout_row="0"
        android:layout_rowWeight="1"
        android:background="@drawable/border"
        android:onClick="choosePlace"
        android:tag="1"
        android:text="1"
        android:textSize="20sp" />
michal
  • 17
  • 6

1 Answers1

0

Create Different values folder for different screens as in following answer: How to define dimens.xml for every different screen size in android?

Then write different sizes for buttons in each dimen.xml

<dimen name="btn_width">50dp</dimen>

<dimen name="btn_width">30dp</dimen>

<dimen name="btn_width">20dp</dimen>

And then in your xml use

    android:layout_width="@dimen/btn_width"
    android:layout_height="@dimen/btn_width"
  • in those folders: /res/values-sw320dp/dimens.xml /res/values-sw480dp/dimens.xml /res/values-sw700dp/dimens.xml /res/values-sw800dp/dimens.xml . For examle. "sw480dp" says that folder will be used for screens which have 480dp+ screen width – Andrey Zhukov Aug 31 '17 at 09:56
  • I opened new value resource file name values-sw480dp, how do I open inside of it the dimen folder? I tried to stand on the values-sw480dp file and than add the dimen I tried all the 3 options (resource file/ file/ directory ) but I can't see it. I did not understand the link how to define dimens you have entered and try to search it myself and I still confuse – michal Aug 31 '17 at 11:17
  • In Android Studio expand values folder, then right button click on it, then new->Values resource file. File name: **dimen**, then add in Available qualifiers "Screen Width" set screen width to 420 for example, directory name must change to "values-w420dp" then press "Ok". In new "dimen.xml" declare `SIZE_TO_420DP_SCREEN` – Andrey Zhukov Aug 31 '17 at 14:31
  • thank you for your help. I did open a dimen folder but don't know how to add in Available qualifiers "Screen Width" – michal Aug 31 '17 at 16:46
  • Right click on "values" folder in Android studio, then new->Values resource file – Andrey Zhukov Sep 01 '17 at 08:22
  • really that you for trying to help. I did what you wrote (see new screen shot in my question) but now I have this kind of problem in all of my screens (only rows).can you take a look in the new screen shot I uploaded? maybe I did something wrong there.. – michal Sep 01 '17 at 14:05
  • I think you should declare diferent GridLayout margins for all screens too – Andrey Zhukov Sep 01 '17 at 14:17
  • Thank you Andrey your helped me a lot! :) – michal Sep 01 '17 at 15:54
  • Thank you very much Andrey. could you please advice if there is anyway to center the grid not by margin in every screen? I tried to center it by code but it didn't help to center the grid – michal Sep 02 '17 at 10:24
  • Try put your GridLayout in FrameLayout and add atribute `android:layout_gravity="center" to GridLayout – Andrey Zhukov Sep 04 '17 at 09:50