Reputation: 6629
I would like to set an id to my layout programmatically but it always shows an error even thoug it executes just fine, How do i end the error displayed in the code:
RelativeLayout newlayout = new RelativeLayout(getContext());
newlayout.setBackgroundColor(Color.GREEN);
newlayout.setId(int 12);
The execution is okay but my code always shows an error
Upvotes: 1
Views: 48
Reputation: 549
Create a res folder for ids:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="id1" />
<item type="id" name="id2" />
<item type="id" name="id3" />
</resources>
Then add that resource ID to the setID() method:
newLayout.setId(R.id.id1);
Upvotes: 1