Change button’s text programmatically in android studio problem

Why is this code wrong? It doesn’t accept the setText function on a button.

findViewById(getResources().getIdentifier("button" + Integer.toString(i), "id", getPackageName()))

But if I write like this


it is ok.


Because findViewById returns a view. View does not have a method called setText(). You should define that the view is a Button which does have a method called setText(). You can do that this way.

((Button)findViewById(getResources().getIdentifier("button" + Integer.toString(i), "id", getPackageName()))).setText("abc");