0

How to create a control array in Android? How to create a multidimensional (2D) array in Android?

for buttons, edit text

also how can we access the elements of edit text in a 2 dimensional array

How are multidimensional arrays created in Android? And in particular a 2 dimensional array to contain Buttons or EditText?

Also, how does one access elements of these arrays once they have been created?

3
  • 1
    What is the class name of a single control? create Array<List> of that item to create a array of controls Commented Jan 1, 2012 at 9:17
  • 2
    @CSK: Do you know how to program in Java? If you do then it's exactly the same. If you don't then learn Java before starting with Android. Commented Jan 1, 2012 at 9:32
  • m trying it with grid view n by setting click listener to each one it worked for imageview ,but not working on edittext............i need it for creating Sudoku table Commented Jan 2, 2012 at 14:06

1 Answer 1

0

Look at this sample what I created:

Button[] buttons = new Button[10];
EditText[] textboxes = new EditText[10];

View[][] buttonsAndEditboxes = new View[2][5];

// adds 10 buttons to one-dimensional array
for (int i = 0; i < 10; i++) {
   buttons[i] = new Button(this);
}

// adds 10 editexts to one-dimensional array
for (int i = 0; i < 10; i++) {
   textboxes[i] = new EditText(this);
}

for (int i = 0; i < 2; i++) {
   for (int j = 0; j < 5; j++) {
      if (i == 0) {
         // adds 5 buttons to 2D array
         buttonsAndEditboxes[i][j] = new Button(this);
      } else {
         // adds 5 edittexts to 2D array
         buttonsAndEditboxes[i][j] = new EditText(this);
      }
   }
}

// access first button from the one-dimensional array           
Button fromOneDimensionalButton = buttons[0];

// access first EditText from the one-dimensional array
EditText fromOneDimensionalEdit = textboxes[0];

// access Button from the two-dimensional array
Button fromTwoDimensionalButton = (Button) buttonsAndEditboxes[0][0];

// access EditText from the two-dimensional array
EditText fromTwoDimensionalEditText = (EditText) buttonsAndEditboxes[1][0];

EDIT:

Add some code as an example for you:

main.xml layout file:

<?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"
    android:id="@+id/test" >
</LinearLayout>

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layout = (LinearLayout) findViewById(R.id.test);

        View[][] buttonsAndEditboxes = new View[2][5];

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 5; j++) {
                if (i == 0) {
                    // adds 5 buttons to 2D array
                    buttonsAndEditboxes[i][j] = new Button(this);
                } else {
                    // adds 5 edittexts to 2D array
                    buttonsAndEditboxes[i][j] = new EditText(this);
                    buttonsAndEditboxes[i][j].setOnClickListener(this);

                    // you need to create unique id here
                    buttonsAndEditboxes[i][j].setId(j);
                    layout.addView(buttonsAndEditboxes[i][j]);

                }
            }
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case 0:
            Toast.makeText(this, "Editext0 clicked", Toast.LENGTH_SHORT).show();
            break;

        case 1:
            Toast.makeText(this, "Editext1 clicked", Toast.LENGTH_SHORT).show();
            break;

        case 2:
            Toast.makeText(this, "Editext2 clicked", Toast.LENGTH_SHORT).show();
            break;

        default:
            break;
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

in one activity i copied it n paste it ...forced close
yep, you need to use layout.addView too (layout is your parent view), to add those editexts and buttons to current activity. Also you need to position them programmatically.
Added some code to EDIT section in my post. There's a example that's working fine. Added xml layout file code and MainActivity file where edittexts and buttons are created and some of them are added to the linearlayout. Also onClick method that handles three editext's click events. Hope it helps!
i dunno abt seting layout using LAYOUT , parent layout,viewgroup send mi some link ,i will study it
thnx dude................actuly m creating tables of edit text for Sudoku game using grid layout....sudoku sheet whr we givs inputs to game creating tat sheet......thnx 4 helping ..if u gt any new idea for creating sudoku sheet plzz tell mi ...................
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.