I’ve an app with multiple activities and I’m using a SlidingDrawer in each one to pass from one activity to another. I started this app when I had no idea of Android, so in every activity I’m declaring all the elements inside the SlidingDrawer, e.g: I’ve Act1.java and Act2.java and inside the SlidingDrawer I’ve two buttons: one that targets to the Act1.java and the other targets to the Act2.java but when I’m in Act1.java, the button that targets to this activity is INVISIBLE.
Ok, my question is: Can I create a slider.xml with all the elements and a Slider.java to control the elements behavior so that every time I start a new activity, the content of the Slider.java is ‘loaded’ and the SlidingDrawer is shown correctly?
Thank you very much in advance!
EDIT: Like in PHP when we call requiered_once(slider.php); but for Java-Android
Yes, you can do this using
Fragments(read more here). They are designed to let you reuse certain UI elements in multiple activities. They were introduced in Honeycomb, but they are included in the support library, making it possible to use them all the way back to API level 4 (Donut).If you just want to reuse the layout, you could just use the
<include>xml tag in your layout. Here’s a really good blog post by Romain Guy about including layouts: http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/