Last active
April 9, 2021 03:58
-
-
Save StevenByle/5729426 to your computer and use it in GitHub Desktop.
Android: Loading Fragments dynamically at runtime depending on layout provided for the device configuration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); | |
} | |
} | |
} |
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
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