Search code examples
androidandroid-tablelayoutr.java-file

Adding setBackgroundResource(R.drawable.cell_shape)


I'm sure this is probably a simple thing to ask, but I've been scratching my head at it for a while now and don't seem to be getting anywhere.

Basically I am following some examples to build a table in android, when I come to setBackgroundResource(R.drawable.cell_shape) it shows an error and I'm unsure where to add this in my xml sheets. Can anyone put me out of my misery please? :-/

Here is the snippet:

for (int j = 1; j <= cols; j++) {

TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  LayoutParams.WRAP_CONTENT));
tv.setBackgroundResource(R.drawable.cell_shape); // This is my problem here
tv.setPadding(5, 5, 5, 5);
tv.setText("R " + i + ", C" + j);

row.addView(tv);

Solution

    1. make sure that cell_shape is present inside your /res/drawable folder
    2. make sure you import the proper R.java yourpkgname.R not android.R
    3. if still not working then clean your project.

    Let me know if it workes for you.