Android creates an identifier programmatically

I am looking for creating an identifier in code without using an XML declaration. For example, I have code where I programmatically create a View like this

@Override
protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.new_layout);

  LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
  View v = new View(this);
  v.setBackgroundColor(0xFFFF0000);
  ll.addView(v, 100, 100);

}

and I can add v.setId(50), but I would like to add v.setId(R.id.some_id), and I would not want to add some_idto the xml file, I know this option.

My question is how to create R.id.some_idprogrammatically without installing it in an XML file. Thanks

+5
source share
1 answer

Refer to this fantastic answer: fooobar.com/questions/28127 / ...

R. * . , , , , , - :

XML android:id

ViewGroup XML ( , ), XML, , :

/ids.xml id:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="reservedNamedId" type="id"/>
</resources>

, ViewGroup View ,

myViewGroup.setId(R.id.reservedNamedId);
+18

All Articles