I want to build generic 5X5 views that baced on table layout (all the view have equal width and height)
as you can see my code isn’t generic. what to do? i tried to play with fill parent and match parent … please help.
and i want the tableLaayout width and height will be equal.
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
tools:context=".MainActivity" >
<TableLayout
android:id="@+id/tableBoard"
android:layout_width="wrap_content"
android:layout_height="313dp"
android:layout_centerHorizontal="true" >
<TableRow
android:id="@+id/tableRow1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.com.worfield.barak.Square
android:id="@+id/view1"
android:layout_width="60dp"
android:layout_height=""
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
</TableRow>
<TableRow
android:layout_weight="1"
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.com.worfield.barak.Square
android:id="@+id/view6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:layout_weight="0.24"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view7"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view10"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
</TableRow>
<TableRow
android:layout_weight="1"
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.com.worfield.barak.Square
android:id="@+id/view11"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view12"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view13"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view14"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view15"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
</TableRow>
<TableRow
android:layout_weight="1"
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.com.worfield.barak.Square
android:id="@+id/view16"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view17"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view18"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view19"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view20"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
</TableRow>
<TableRow
android:layout_weight="1"
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.com.worfield.barak.Square
android:id="@+id/view21"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view22"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view23"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view24"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
<com.example.com.worfield.barak.Square
android:id="@+id/view25"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="1dp"
android:background="@drawable/square" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/bStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tableBoard"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Do Something" />
<TextView
android:id="@+id/tvError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bStart"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="TextView" />
Create a special view which can only be square. You should override onMeasure method accordingly. For exmaple you can do the following:
TableRow is LinearLayout. So you can use layout_weight to spread 5 childs equally horizontally. Set layout_width = “0” and layout_weight = “1” for all childs of a row. When measured each child’s width will be 1/5 of tableRow width and child’s height will be equal it’s width.