-1
 <TableRow
            android:id="@+id/Row3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center">

            <TextView
                android:id="@+id/cell31"
                android:layout_height="fill_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="50sp"
                android:textStyle="bold"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:background="@drawable/back"
                android:onClick="cellClick"
                android:clickable="true"/>
            <TextView
                android:id="@+id/cell32"
                android:layout_height="fill_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="50sp"
                android:textStyle="bold"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:background="@drawable/back"
                android:onClick="cellClick"
                android:clickable="true"/>
    .... this is 5x5 row column table. with 25 cells in it.

My .xml file looks like this. I have bunch of rows in LinearLayout contains TableLayouts which contains TextViews as cells. I am making a basic game with them. I am trying to assign each cell from .xml to TextView array in .java file. But i can't figure out how to make it. I need to change color of each cell based on some algorithm i made so i want to access of cells.

private TextView colorBoard[][];
this is my .java array.

I can check if they are clicked with

public void cellClick(View v) {
        TextView cell = (TextView) findViewById(v.getId());
 switch (cell.getId()) {

                case R.id.cell11:
                    xloc = 0;
                    break;
                case R.id.cell12:
                    xloc = 1;
                    break ;
                   ...rest of cells

But i only can assign which cell clicked at moment.In my game color of cells change differently. Not the one you just clicked. Assume you clicked 1x3 and 4x4 may change colors.

I'm new to android and this is my first project so sorry if i'm missing something basic.

stacknotflow
  • 15
  • 1
  • 1
  • 3

2 Answers2

1

I think you should use ListView instead of TextView. In ListView you can use an array for assigning those things where-ever you want to use them.

You can create layout file like this:

<ListView>
  android:weight="match_parent"
  android:height="match_parent"
  android:id="+id/lst_view"
</ListView>

And Your Java Code looks like this

 String[] array={"stack","queue","arraylist"};
    ListView lst;
    //In onCreate function
    lst=(ListView)findViewById(R.id.lst_view);
     ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,R.layout.what_ever_your_xml,array);
    lst.setAdapter(adpt);

  //function of listview 
  //arrayadapter is used for converting string type array into listview

       lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String s;

                    s=((TextView)view).getText().toString();
                    Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
                }
            });
Nitin
  • 1,280
  • 1
  • 13
  • 17
0

If you want to assign values to TextView Array. Then do it like this in oncreate method after setContentView

  colorBoard[0][0] = (TextView) findViewById(R.id.cell11); 
 colorBoard[0][1] = (TextView) findViewById(R.id.cell12); 

You can use findviewbyId to get the TextView from xml to Java.

Suppose if you want to access cell44 when you tap on cell11, then you could do something like this.

public void cellClick(View v) {
        TextView cell = (TextView) findViewById(v.getId());
 switch (cell.getId()) {

                case R.id.cell11:
                    xloc = 0;
                    colorBoard[0][0].setBackground()//As you wish
                    break;
                case R.id.cell12:
                    xloc = 1;
                    break ;
                   ...rest of cells

Once you have access to cell44 you can set the background color or any other properties to it.

lib4backer
  • 3,337
  • 3
  • 18
  • 16
  • That's not what i mean though. It is randomly assigned so i kinda have to control all in. I mean first time tapping cell11 could mean cell 44 but another time it could also mean cell23. – stacknotflow Feb 04 '18 at 17:24
  • Do you need to populate the colorBoard[][] ? – lib4backer Feb 04 '18 at 17:27
  • Not necessarily. But i must able to access whichever cell i want and change color of it. Wihout tapping it. I mean i can get that populating with ViewID but isnt that bit of code duplication. Is there any way better ? I am not insisting on creating TextViews (cells) on XML but i don't quite sure how to make them programmatically – stacknotflow Feb 04 '18 at 17:33
  • Could you please check my updated answer. You could change the color at any time, even without tapping as you are having access to each individual TextView. – lib4backer Feb 04 '18 at 17:34
  • I checked your answer and replied with my answer before please check it. – stacknotflow Feb 04 '18 at 17:35
  • A proper way of doing this kind of layout would be use RecylerView with GridLayoutManager , So It will look like Matrix instead of list. Please refer this https://stackoverflow.com/questions/40587168/simple-android-grid-example-using-recyclerview-with-gridlayoutmanager-like-the – lib4backer Feb 04 '18 at 17:41