javaandroid-studioinflateviewstub

In the installed “Layout” on “ViewStub”, the button found via “findViewById” does not work


I set setLayoutResource(R.layout.content_no_connection) for ViewStub, then inflate(), and in onInflate() set the logic of the button which is in content_no_connection. And the button does not respond to clicks. Code:

public void setRebootIncludeLoad(ViewStub includeLoad) {
    includeLoad.setLayoutResource(R.layout.content_no_connection);
    includeLoad.setOnInflateListener(new ViewStub.OnInflateListener() {
        @Override
        public void onInflate(ViewStub viewStub, View view) {

          AppCompatButton btnRebootInternet = view.findViewById(R.id.btn_reboot_internet);
          btnRebootInternet.setFocusable(true);
          btnRebootInternet.setClickable(true);
          btnRebootInternet.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                         
               Reboot();
             }
          });
        }
   });

   includeLoad.inflate();
}

Please tell me what the problem may be and how to solve it, thank you! I understand that the matter is that I can not find the button I need, but on the button is color selector with color change when you press, and even the color does not change, the button does not respond at all.


Solution

  • Problem solved, you should have added:

    ((ViewGroup) view.getParent()).removeView(view);
    activity.setContentView(view);
    

    in onInflate() before AppCompatButton btnRebootInternet = view.findViewById(R.id.btn_reboot_nternet);