Geoff
Geoff

Reputation: 6629

Setting id in android layout fails

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 enter image description here

Upvotes: 1

Views: 48

Answers (1)

Daniel Rust
Daniel Rust

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

Related Questions