For example, I want to create an instance of a button:

Button btn = (Button) findViewById(R.id.button); 

Can I follow R.id. set the variable in which the button value will be stored. And how can I get an instance of the button findViewById , but if I have a variable in which the button id is stored.

  • try to explain in more detail what exactly you want to get in the end, give an example. The fact is that R.id.button itself is a variable that stores a number - the unique identifier of the button. this number is constant for each widget and cannot change its value in the project. What other variable do you want to create and for what purposes? - pavlofff
  • I already understood everything, thanks. I did not know that R.id.button is a variable in itself. - Winter Fox

2 answers 2

 int btnID= R.id.button; Button btn = (Button) findViewById(btnID); 

    This can be done in the following way:

     int resId = getResources().getIdentifier("button", "id", getPackageName()); Button btn = (Button) findViewById(resId);