Android view without xml
If you haven't already declared your view and put it in the layout XML file, you won't be able to get it with
findViewById(int)
If you are creating your view without XML, you must manage it without a resource ID. You just create a view and name it and then use the name to work with it.
You can always pass a reference to other classes, or make it available through the class that created it.
However, would you like NOT to declare it in XML? Usually when you start a project, you cannot see how you can do anything with the XML layout, and select the programmatic representation of most views. After you move forward with a project or gather more experience, in almost all cases you see that you could do it with XML.
source to share