androidviewandroid-identifiers

Android change color and text of changed buttons


My app have a layout with 50 buttons that the user can touch and change text and color. The informations are stored in a database for keep the change in the future. I'm using the ID but the problem appear when the app is compiled and the R class change all int ids.

this is the code

public class setTableActivity extends ActionBarActivity {

static int clickedButtonViewId; // Declare TextView as class level member field


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_table);

    MySQLiteHelper db = new MySQLiteHelper(this);

    //Get all materie inside database
    List<Materia> materia = db.getAllMaterie();
    //change all TextView inputed from user
    if(materia.isEmpty()){
        //do nothing
    }else {
        for (Materia mat : materia) {
            //Change all the Button with values stored inside the database
            final Button changedButton = (Button) findViewById(mat.getID());
            changedButton.setText(mat.getMateria());
            changedButton.setBackgroundColor(mat.getColor());
        }
    }
}

//Take back data from ActivityAddMateria
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 1) {
        if (resultCode == RESULT_OK) {

            MySQLiteHelper db = new MySQLiteHelper(this);
            String result = data.getStringExtra("result"); //Take the materia from Dialog
            int color = data.getIntExtra("color", 1); //Take the color from Dialog

            //Here i need to recognize row and column
            db.addMateriaToDb(new Materia(clickedButtonViewId, result, color));

            final Button clickedtextView = (Button) findViewById(clickedButtonViewId);

            clickedtextView.setText(result);
            clickedtextView.setBackgroundColor(color);
        }

        if (resultCode == RESULT_CANCELED) {
            //Nessuna materia inserita
        }
    }
}//onActivityResult

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_set_table, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.draw_orario:
            //addMateria();
            MySQLiteHelper db = new MySQLiteHelper(this);
            db.deleteMateria();
            //This for refresh the acrivity after deleted all objects
            Intent intent = getIntent();
            finish();
            startActivity(intent);
            return true;
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public void addMateria(View v){
    //To get ID of your TextView do this

    clickedButtonViewId = v.getId();

    //StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
    Intent myIntent = new Intent(setTableActivity.this, ActivityAddMateria.class);
    setTableActivity.this.startActivityForResult(myIntent, 1);
    onStop();
}

}

As you can see i take the id of the selected Button, change text and color and i save the id, text, and color. If i compile again the app all the ids change in the R class and my work is useless. I need something for find the changed views and for store in the DB the changed buttons.

I try with the android:tag="tag1" but the findViewWithTag doesn't work. Some suggestions?

The XML of the Activity

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue_orario"
    tools:context="com.ddz.diarioscolastico.setTableActivity">

    <TableRow
        android:id="@+id/giorni"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Lun"
            android:id="@+id/textView15"
            android:layout_column="1" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Mar"
            android:id="@+id/textView16"
            android:layout_column="2" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Mer"
            android:id="@+id/textView17"
            android:layout_column="3" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Gio"
            android:id="@+id/textView18"
            android:layout_column="4" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Ven"
            android:id="@+id/textView19"
            android:layout_column="5" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Sab"
            android:id="@+id/textView20"
            android:layout_column="6" />
    </TableRow>

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="1"
            android:id="@+id/ora1"
            android:layout_column="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m11"
            android:tag="m11"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m12"
            android:tag="m12"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m13"
            android:tag="m13"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m14"
            android:tag="m14"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="4" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m15"
            android:tag="m15"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="5" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m16"
            android:tag="m16"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="6" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="2"
            android:id="@+id/textView22"
            android:layout_column="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m21"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m22"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m23"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m24"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="4" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m25"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="5" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m26"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="6" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="3"
            android:id="@+id/ora_3"
            android:layout_column="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m31"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m32"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m33"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m34"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="4" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m35"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="5" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m36"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="6" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="4"
            android:id="@+id/ora_4"
            android:layout_column="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m41"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m42"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m43"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m44"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="4" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m45"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="5" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m46"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="6" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="5"
            android:id="@+id/ora_5"
            android:layout_column="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m51"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m52"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m53"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m54"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="4" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m55"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="5" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m56"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="6" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="6"
            android:id="@+id/ora_6"
            android:layout_column="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m61"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m62"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m63"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m64"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="4" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m65"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="5" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m66"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="6" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="7"
            android:id="@+id/ora_7"
            android:layout_column="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m71"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m72"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m73"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m74"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="4" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m75"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="5" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="@string/new_button"
            android:id="@+id/m76"
            android:onClick="addMateria"
            android:textSize="12sp"
            android:layout_column="6" />
    </TableRow>
    </LinearLayout>
    </ScrollView>
</TableLayout>

Solution

  • Just save the name of the button, and then use the following to get the resource ID of the button by its name:

    int resId = getResources().getIdentifier(resourceName, "id", getPackageName());