How to Programmatically Include Layout in Android

How can I programmatically include layout in Android?

Use a ViewStub instead of include:

<ViewStub
android:id="@+id/layout_stub"
android:inflatedId="@+id/message_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.75" />

Then in code, get a reference to the stub, set its layout resource, and inflate it:

ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(R.layout.whatever_layout_you_want);
View inflated = stub.inflate();

Replace `layout` of an include/ tag programmatically for Android

I found the answer a couple of months ago. You just need to use a ViewStub and inflate the appropriate one.

Include layout in view programmatically within a fragment

I suppose the solution which you are looking for are Android ViewStubs. These are dynamically inflated layouts.

For more information you could refer these :

How to use View Stub in android

https://developer.android.com/reference/android/view/ViewStub.html

However, if you don't wish to inflate one layout inside another during runtime, you could try this the include tag:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/todayView">

<include layout="@layout/your_layout_file_name"/>
</LinearLayout>

Android programmatically include layout (i.e. without XML)

Personally, I'd probably write my Activity subclass to always setContentView to a layout file containing a vertical fill_parent LinearLayout containing only my title bar:-

<LinearLayout android:id="@+id/custom_titlebar_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--titlebar here-->
</LinearLayout>

Then I'd define an abstract getContentAreaLayoutId() method in CustomTitlebarActivity that returns the layout ID of the content below the titlebar for each subclass; the base onCreate() of CustomTitlebarActivity would then just call

setContentView(R.layout.custom_titlebar_activity_frame_from_above);
View.inflate(this, getContentAreaLayoutId(), findViewById(R.id.custom_titlebar_container));

Alternatively, you could have your abstract method for getting the content area return a View rather than an int, giving you more flexibility to construct your views dynamically (but forcing you to inflate them yourself in the simple just dump this XML layout here case).

add Layout programmatically inside another one

The inflate(int resource, ViewGroup root) method return root if root is not null, thus to_add.findViewById() equals options_layout.findViewById() and it always return the Views in the first position.
Change as following should help:

LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout);

TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));

}

to:

LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout,false);

TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));
options_layout.addView(to_add);
}

How to dynamically include a layout in Android?

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);

Include layout programmatically

You can look at Fragments. It will help you to opn various layouts in the same activity whenever they are called upon. Furthermore, you can reuse your Navigation drawer too.



Related Topics



Leave a reply



Submit