How to Reuse a Layout for Your Android Apps - dummies

By Barry Burd

Once you have created a reusable layout, you can apply it to multiple Android apps. This helpful trick can cut down your workload quite a bit. It’s simple. Here’s how:

  1. Make sure you have created a reusable layout.

    Be sure to create a reusable_layout.xml file and to populate the file with a few widgets.

  2. In the Project tool window, select the project’s reusable_layout.xml file.

  3. In Android Studio’s main menu, choose Edit → Copy.

    You don’t see much happening, but now your Clipboard contains a copy of the reusable_layout.xml file.

  4. Start a new Android project.

  5. In the Project tool window, select the new project’s app/res/layout branch.

  6. In Android Studio’s main menu, choose Edit → Paste.

    Now the app/res/layout branch contains a reusable_layout.xml file.

  7. Open your project’s res/layout/activity_main.xml file.

    When you do, you see your new project’s preview screen (which is mostly empty).

  8. In the Custom group in the Designer tool’s palette, click the <include> item.

    The palette has its own scroll bar. Use this scroll bar to find the palette’s Custom group.

    When you do this, a Resources dialog box appears.

  9. In the Resources dialog box, select your reusable layout; then click OK.

    The Resources dialog box closes.

  10. 10.Click in the Designer tool’s preview screen.

    As if by magic, the stuff that you created appears on the preview screen. This stuff appears as one group, so you can drag that group around in the preview screen.

  11. If necessary, reshape the newly included group (two check boxes and a button) by dragging its edges in the preview screen. Also, drag the entire group so that it’s centered inside the preview screen.

  12. From the palette, place a Large Text element on the preview screen.

  13. Replace the Large Text element’s placeholder text with the word Plain.

  14. Run your app.

    Ambitious or not, you have a decent-looking layout with a reusable component. Nice work!

    The Resources dialog box in Android Studio.
    The Resources dialog box.