A button is dynamically created in the application and the ID is set to it:

butAss = new Button(this); butAss.setId(++id); 

I want to add an action to the newly created button, but I cannot register it:

 Button dayz; dayz = (Button)findViewById(R.id.______); 
  • one
    Is it about the fragment (judging by the tags)? - pavlofff
  • I was also interested in this issue. This can be done by passing the tag to the created button: butAss = new Button (this); butAss.setTag (++ id); further find it by the desired tag number, the answer is here: ru.stackoverflow.com/questions/699450/… - VolhaGomel

1 answer 1

You have to do it differently.

As you add dynamically from the ID code for the button, it will not be contained in the resource class that is generated when building the application!

You need to remember the id that you assigned:

 Button butAss = new Button(this); //создаем кнопку butAss.setId(100); //назначаем id=100 somelayout.addView(butAss); //добавляем в somelayout 

How to access:

 Button dayz; dayz = (Button)somelayout.findViewById(100); // ищем элемент в somelayout с id=100 
  • I faced such a problem myself, in the way you suggested, Id not add it. Found the answer, how to add an id to the programmatically created view here at stackoverflow.com/questions/1714297/ ... The answer, which received the most votes, is very detailed, "what and where and why" - VolhaGomel