Daryl
Daryl

Reputation: 95

How to generate ID i created in java class to r.java

im new to android programming.

im wondering if it is possible to generate ID i created in java class to r.java

The ID i want to generate is nBtnLayoutlist.

public class classABC extends Activity {
    private int nBtnLayoutlist = 20;

    private OnClickListener onClick() {
        return new OnClickListener() {

            public void onClick(View v) {

            nBtnLayout = (LinearLayout)findViewById(R.id.nBtnLayoutList);

            }
        };
    }

    private LinearLayout newBtnLayout(){
    LinearLayout nBtnLayout = new LinearLayout(this);
    final LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT,       LayoutParams.WRAP_CONTENT);
    nBtnLayout.setOrientation(LinearLayout.VERTICAL);
    nBtnLayout.setLayoutParams(lparams); 
    nBtnLayout.setWeightSum(100);
    nBtnLayout.setId(nBtnLayoutList);
    return nBtnLayout;

}

Upvotes: 0

Views: 689

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234867

The ids in R.java are generated at build time from the contents of your .xml files. R.java cannot be modified by hand and it cannot be modified at run time.

If you want, you can define an id resource by declaring it in some file in res/values (the file can have any name):

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

You can always set the id of a view in your layout by calling setId(int) (as you are already doing).

EDIT: If you want to use a constant in the call such as

nBtnLayout.setId(nBtnLayoutList);

then you need to use the same constant in calls such as

nBtnLayout = (LinearLayout)findViewById(nBtnLayoutList); // NOT R.id.nBtnLayoutList

Upvotes: 2

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

- nBtnLayoutList is a reference to the LinearLayout.

- You can set id to the LinearLayout by passing integer in setId() method.

Eg:

nBtnLayout.setId(1);

Upvotes: 0

Related Questions