Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically include a layout in Android?

Tags:

java

android

xml

I wanted to include a dynamic content to take advantage of all layout already created without having to have an xml with all the features for each actitivy.

I noticed that the android has an wanted to use this to determine what will be my content, according to the activity accessed.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="br.com.p21sistemas.certidao21.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include android:id="@+id/content" layout="@layout/content_??????" /> 

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

public class WizardActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // main xml

        // Something I do not know what may include my content content_wizard
    }

}
like image 233
Jerfeson Guerreiro Avatar asked Jun 13 '16 12:06

Jerfeson Guerreiro


People also ask

How to include another layout in Android?

Although Android offers a variety of widgets to provide small and re-usable interactive elements, you might also need to re-use larger components that require a special layout. To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout.

What is dynamic layout in Android Studio?

DynamicLayout is a text layout that updates itself as the text is edited. This is used by widgets to control text layout. You should not need to use this class directly unless you are implementing your own widget or custom display object, or need to call Canvas.


1 Answers

You could define an empty layout in your XML where you would like you dynamic content to be and add it's content from code.

Something like this:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:id="@+id/dynamic_content"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

And then, from your code:

// get ahold of the instance of your layout
LinearLayout dynamicContent = (LinearLayout) findViewById(R.id.dynamic_content);

// assuming your Wizard content is in content_wizard.xml
View wizardView = getLayoutInflater()
    .inflate(R.layout.content_wizard, dynamicContent, false);

// add the inflated View to the layout
dynamicContent.addView(wizardView);
like image 156
earthw0rmjim Avatar answered Sep 22 '22 07:09

earthw0rmjim