shtkuh
shtkuh

Reputation: 459

getID for button returns -1

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button b = new Button(this);
        String id = Integer.toString(b.getId());
        Toast.makeText(getApplicationContext(), id, 1).show();
    }
}

Why does it return -1?

Upvotes: 0

Views: 1029

Answers (1)

Codemonkey
Codemonkey

Reputation: 3412

Because that's the ID views are given when you create them yourself.

Just set the ID with

b.setId(int)

http://developer.android.com/reference/android/widget/Button.html

Upvotes: 3

Related Questions