I have 2 activity, called screen1 , screen2 . I use them as fragments (but these are not fragments, but simply an activity), in which there is an ImageButton . screen1 , screen2 I throw in the viewFlipper , which is in activity_main to make Sliding. I did it all, Sliding works, but ... Buttons do not react at all, because there is no handler. I want to make the handlers of these buttons in one main class MainActivity . The question is, how do I get from screen1, screen2 ImageButton and put them handlers in the main class?

So added screen1 and screen2 to the main activity_main , to the ViewFlipper block

 flipper = (ViewFlipper) findViewById(R.id.viewFlipper); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); int layouts[] = new int[] { R.layout.screen1, R.layout.screen2 }; for (int layout : layouts){ flipper.addView(inflater.inflate(layout, null)); } 

  • No need to create a new question with clarification of the old. It was necessary to make the old edit. - Yuriy SPb
  • And you, it seems, are not 3 activations, but one. And the rest is just some kind of markup, which has nothing to do with activization ... The question is still not entirely clear. - YurySPb
  • In theory, you can simply after adding markups to the ViewFlipper through him and look for his insides by their ID - YuriySPb
  • Here you have a MainActivity, an activity_main XML file is attached to it, a container of other XML files (but these are not fragments, but just markup) that are enclosed in the viewFlipper block, which is located in activity_main, so that I can make Sliding . This is all I did. But the buttons that are in screen1 they do not work, because there is no class. I want to use the contents of screen1 in the MAIN CLASS MainActivity. How can I do it? - Edward
  • Do these buttons have your ID? - YurySPb

1 answer 1

In the activity you can get a link to any element of its markup by its ID. Even if you added elements manually from another markup.

 //после добавления во ViewFlipper ImageButton btnGoogle = (ImageButton) findViewById(R.id.btnGoogle); 

The main thing to remember is that if there is a View with the same ID in the markup, then there is only one of them, which can be fraught with errors.

And for such a task there is a ViewPager with fragments.

  • @Eduard, if one of the submitted answers solves the problem stated in the question, then you can vote for it and mark it "correct" by using the arrows and tick to the left of the response body. - YurySPb