Issue
Kotlin, I want to embed root_preferences.xml in a fragment with existing content. I navigate using <navigation..> I want to embed the preferences like this:
// ... existing items
<include
android:id="@+id/summaryPrefs"
layout="@layout/fragment_container_settings"
/>
// ... more items here
What I did
Create new Settings Fragment. This does NOT create a layout file, but an XML file at res/xml/root_preferences.xml, make no changes to code.
Create a 'container fragment' new Fragment (Blank) name it, ContainerSettingsFragment. it creates the layout file fragment_container_settings.
Delete code in class ContainerSettingsFragment and replace with this: // Make a container fragment that holds the preference xml.
class ContainerSettingsFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_container_settings, container, false) requireActivity().supportFragmentManager .beginTransaction() .replace( R.id.settings, // this is the id in fragment_container_settings <FrameLayout> SettingsFragment() // SR: ?? This loads the XML into this fragment ) .commit() // Inflate the layout for this fragment return view }
}
Replaced code in fragment_container_settings with this: FrameLayout android:id="@+id/settings" android:layout_width="250dp" android:layout_height="300dp"
Expected result: FrameLayout @+id/settings displays the contents of root_preferences Actual: Size is 250dp x250dp but none of the items are displayed
Solution
Was easy, if using nav_graph, don't mix with .supportFragmentManager
I removed all the code in the container settings fragment.
): View? { return inflater.inflate(R.layout.fragment_container_settings, container, false) }
Completed layout in fragment_container_settings.xml
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/settingsSummary" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.settingssummary.ContainerSettingsFragment">
</androidx.constraintlayout.widget.ConstraintLayout>
Used include to add to fragment_settings_summary
Answered By - Shimon Rothschild
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.