-1

I'm developing an app, the have 100 buttons. And I want to put all the buttons inside the screen. I'm using a GridLayout with all the buttons inside the problem that I am having is that the buttons are getting out of the screen.

This is the problem that I am having

This is the code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:layout_column="1">

 <GridLayout
    android:id="@+id/GridLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnCount="6"
    android:rowCount="17"
    android:orientation="vertical">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/numero_1"
        android:id="@+id/button1"/>

The question that I have is: Is this possible to do? And this is the best approach?

I made other test where I changed the size of the image. The result is not what i expected header

Result with a different size image

Image with other size

As you can see in the second image the buttons in the rows are inside the screen but the position of the buttons is not ok, there are a lot of space between the last button of the row and the end of the screen.

The solution that I am looking for, is to have a layout that don't allow the buttons to get out of the screen, and that resizes the space between the buttons and the edges automatically, if the screen size is different the layout will automatically adapt to the size of the screen.

user3005486
  • 179
  • 1
  • 3
  • 9

1 Answers1

0

I don't think that is possible. I would suggest you to use 6 vertical LinearLayouts in one horizontal, with weight 1 on inner ones. I know that that is not most optimized way, but it's easiest. Another would be to keep GridLayout and calculate size of images and set their size from Java code.

Vladimir Jovanović
  • 2,261
  • 2
  • 24
  • 43