2

Now it's time to explore Android for me. I have designed one ui which is shown below, The thing is I am getting confused with what should I do in the first frame in order to get 9x9 grid. Actually the same thing I developed in core java by placing 81 JTextFields in each cell using for loops and with the help of gridbaglayout and I got my application perfectly working. But in android how can I define my 9x9 cells in xml, so that it should accept numbers and entered numbers should be retrieved too in order to validate the sudoku rules. Following is my code and diagram. Can anyone suggest how to make the frame 1 to have 9x9 cells? If I use TextEdit in my xml file, it would be 81 textfields, and if I use Canvas(I am thinking, don't know whether it is possible), it seems to me impossible to make it accept numbers and to retrieve from that canvas board. I really appreciate if anyone could help. Please go easy, if it seems silly question(which I hope not, as I tried 2 days for it), as I am new to android. Thanks in advance.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <FrameLayout
                android:id="@+id/fLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="2"
                android:background="#0f0"
        />
        <FrameLayout
                android:id="@+id/fLayout2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="3"
                android:background="#00f">

        <TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keypad"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*">

<TableRow >
    <Button android:id="@+id/cancel"
        android:text="cancel"
        android:layout_span="3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
</TableRow>
<TableRow>
    <Button android:id="@+id/submit"
        android:text="validate"
        android:layout_span="3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

    </Button>
</TableRow>
</TableLayout>

        </FrameLayout>
</LinearLayout>

Here is the UI

enter image description here.

Kanth
  • 6,681
  • 3
  • 29
  • 41
  • Either [TableLayout](http://developer.android.com/reference/android/widget/TableLayout.html) or [GridView](http://developer.android.com/guide/topics/ui/layout/gridview.html) both are useful for you.. – user370305 Aug 27 '12 at 10:56

3 Answers3

5

Look at this Open Sudoku Project from Git Hub . Here you can find layout and source code for Sudoku.

Check res/layout/sudoku_edit.xml for Sudoku View.

Chirag
  • 56,621
  • 29
  • 151
  • 198
4

I suggest you to use Grid View

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.

Pang
  • 9,564
  • 146
  • 81
  • 122
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Even I thought to apply GridView, but my doubt is how to arrange things, so that it should accept numbers and that numbers should be retrieved. I am thinking to arrange EditTexts, but in xml it's very lengthy to arrange 81 EditTexts. – Kanth Aug 28 '12 at 03:07
  • Use Adapter and create dynamically EditText at runtime.. You don't need to maintain xml file for 81 EditText.. – user370305 Aug 28 '12 at 05:07
  • Even I got one more problem, I don't want my frame1 to be scrollable. The entire 9x9 cells should automatically get fit to the frame1. – Kanth Aug 28 '12 at 06:41
  • Hey, I created 81 edittexts by using two for loops with two dimensional array edittexts. which adapter I have to use in order to attach 2 dimensional edittexts to gridview? Because i am getting errors if add them with ListAdapter. – Kanth Aug 28 '12 at 07:41
  • Please don't mind, I might be troubling you. But hope you help with this last question. – Kanth Aug 28 '12 at 07:52
  • Look at the links http://developer.android.com/guide/topics/ui/layout/gridview.html and http://collegewires.com/android/2012/06/android-grid-view-example/ find example from it. And modified according your requirements. – user370305 Aug 28 '12 at 08:05
  • There is no such method as setScrollable(false); that can be applied to gridview. – Kanth Aug 28 '12 at 11:22
  • Look at http://stackoverflow.com/questions/4852867/how-to-disable-gridview-scrolling-android/ – user370305 Aug 28 '12 at 11:31
  • I tried all those before asking you, but they don't have effect on my application. Still there is scrolling. – Kanth Aug 28 '12 at 11:39
3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="9" 

>
</GridView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

Jackson Chengalai
  • 3,907
  • 1
  • 24
  • 39