Jesse Galdamez
Jesse Galdamez

Reputation: 81

In Eclipse, How does one specify the "android:id" for a button when creating the layout dynamically in the .java file

I looked at: How to set Id of dynamic created layout?.

and did the following:

//in my .java file (in OnCreate)
submitButton.setId(R.id.dynSubmitButton);

in my listener method:

public void addListenerOnButton() {

    final Context context = this;
    submitButton = (Button) findViewById(R.id.dynSubmitButton);

    submitButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, ActivityResults12.class);
            startActivity(intent);

        }
    });
}

I then got an error on submitButton.setId(R.id.dynSubmitButton); which I fixed by clicking on it and choosing Create constant 'dynSubmitButton' in type 'id'. I get the following error:

[2015-01-09 14:13:18 - Application] R.java was modified manually! Reverting to generated version!

Upvotes: 1

Views: 54

Answers (1)

Prem
Prem

Reputation: 4831

You can set any integer value in setID method. But remember the following, while you set the ID.

  1. Manually set ids using someView.setId(int);
  2. The int must be positive.

For example, if creating and numbering several views representing items, you could use their item number.

Example -

 int i=10;
 submitButton.setId(i);

Upvotes: 2

Related Questions