1

These is how I set id in XML android:id="@+id/checkBox1" assuming i don't have that in XML, what I want is to set multiple id in Java, these is my expected output if I want to call them:

findViewById(R.id.checkBox1)
findViewById(R.id.checkBox2)
findViewById(R.id.checkBox3)
findViewById(R.id.checkBox4)

I know my question is vague but anyone have an idea?? This is my code..

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_information);

    Resources res = getResources(); 
    String[] planets = res.getStringArray(R.array.planets_array);   

    LinearLayout layout = (LinearLayout) findViewById(R.id.lMainIng);
    for(int x=0; x<planets.length; x++){
        CheckBox testView = new CheckBox(this);
        testView.setText(planets[x]);
        testView.setLayoutParams(new LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
  //--->        testView.setId(x);
  //just a test what would be the id result
  //--->        testView.setText(R.id.lMainIng);
        layout.addView(testView);
    }

  }

I think my question is somewhat wrong,.. What i really want to ask is how to identify the id that I've just generate for the specific components, in this particular activity.. i want to save those id's in an array but i don't know how to identify them..

johnguild
  • 435
  • 1
  • 5
  • 25

1 Answers1

0

You can do something like this :

Enter getTag/setTag:

testView.setTag(1);
testView.setTag(2);

to retrive u can use :

myValue = textView.getTag();

OR use

findViewByTag method
KOTIOS
  • 11,177
  • 3
  • 39
  • 66