Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android: Loading Fragments dynamically at runtime depending on layout provided for the device configuration.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main_root_container">
<FrameLayout
android:id="@+id/activity_main_image_selector_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_root_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/activity_main_image_selector_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40" />
<FrameLayout
android:id="@+id/activity_main_image_rotate_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_root_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/activity_main_image_selector_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40" />
<FrameLayout
android:id="@+id/activity_main_image_rotate_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60" />
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate: savedInstanceState " + (savedInstanceState == null ? "==" : "!=") + " null");
setContentView(R.layout.activity_main);
// Restore state
if (savedInstanceState != null) {
// The fragment manager will handle restoring them if we are being
// restored from a saved state
}
// If this is the first creation of the activity, add fragments to it
else {
// If our layout has a container for the image selector fragment,
// create and add it
mImageSelectorLayout = (ViewGroup) findViewById(R.id.activity_main_image_selector_container);
if (mImageSelectorLayout != null) {
Log.i(TAG, "onCreate: adding ImageSelectorFragment to MainActivity");
// Add image selector fragment to the activity's container layout
ImageSelectorFragment imageSelectorFragment = new ImageSelectorFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(mImageSelectorLayout.getId(), imageSelectorFragment,
ImageSelectorFragment.class.getName());
// Commit the transaction
fragmentTransaction.commit();
}
// If our layout has a container for the image rotator fragment, create
// it and add it
mImageRotatorLayout = (ViewGroup) findViewById(R.id.activity_main_image_rotate_container);
if (mImageRotatorLayout != null) {
Log.i(TAG, "onCreate: adding ImageRotatorFragment to MainActivity");
// Add image rotator fragment to the activity's container layout
ImageRotatorFragment imageRotatorFragment = ImageRotatorFragment.newInstance(
StaticImageData.getImageItemArrayInstance()[0].getImageResId());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(mImageRotatorLayout.getId(), imageRotatorFragment,
ImageRotatorFragment.class.getName());
// Commit the transaction
fragmentTransaction.commit();
}
}
}
@rajashri73

This comment has been minimized.

Copy link

rajashri73 commented Nov 14, 2013

Hello sir,

I refer this code and tried to do a fragment but i am having error at line ImageSelectorfragment please give me a guidance i am awating...

thanks and regards

@StevenByle

This comment has been minimized.

Copy link
Owner Author

StevenByle commented Jan 9, 2014

These are just snippets that I refer to from my blog post in order to give a quick code example, so many of the details are left out. The full implementation is in my AndroidPhoneTabletExample.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.