Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include layout inside layout?

How to include layout inside layout in Android?

I am creating common layout. I want to include that layout in another page.

like image 461
mohan Avatar asked Apr 12 '11 07:04

mohan


People also ask

How do I create a layout in another layout?

To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.

What is a nested layout?

By the term of Nested we mean one Layout inside of other Layout. In Android all layout can be nested one another. In this example we create a Registration Form with multiple fields using Nested Linear Layouts.

Can we use linear layout in RelativeLayout inside?

We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.

What is the correct way to attach a layout into your activity?

You can declare a layout in two ways: Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.


2 Answers

Edit: As in a comment rightly requested here some more information. Use the include tag

<include    android:layout_width="match_parent"    android:layout_height="wrap_content"    layout="@layout/yourlayout" /> 

to include the layout you want to reuse.

Check this link out...

like image 67
Michael Rose Avatar answered Sep 26 '22 12:09

Michael Rose


Note that if you include android:id... into the <include /> tag, it will override whatever id was defined inside the included layout. For example:

<include    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:id="@+id/some_id_if_needed"    layout="@layout/yourlayout" /> 

yourlayout.xml:

<LinearLayout    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:id="@+id/some_other_id">    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/button1" />  </LinearLayout> 

Then you would reference this included layout in code as follows:

View includedLayout = findViewById(R.id.some_id_if_needed); Button insideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button1); 
like image 24
Phileo99 Avatar answered Sep 25 '22 12:09

Phileo99