Im trying to add some fragments to a FragmentContainerView dynamically using kotlin. The issue I have is each fragment is drawn on top of each other in the top corner whereas I would like them to be next to one another so you can see them all. Is there a standard way to do this? I have tried googling but i'm new to android so im probably using the wrong terms.
The layout;
<HorizontalScrollView
android:id="@+id/external_threat_container_scroll"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/external_threat_container"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</androidx.fragment.app.FragmentContainerView>
</HorizontalScrollView>
The activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
if(savedInstanceState == null) {
supportFragmentManager
.beginTransaction().apply {
this.add(R.id.ship_container, ShipFragment.newInstance(), "ship")
(1..50).map { Pair(it, Threat.newInstance()) }.forEach { this.add(R.id.external_threat_container, Threat.newInstance(), "thrre$it") }
(1..50).forEach { this.add(R.id.internal_threat_container, Threat.newInstance(), "the$it") }
}.commit()
}
}
The fragment xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/threat_height"
android:layout_height="@dimen/threat_width"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Threat">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/threat_title"
android:layout_width="match_parent"
android:gravity="center_horizontal"
app:layout_constraintTop_toTopOf="parent"
android:text="@string/exampleFlagshipTitle"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:src="@drawable/die_two"></ImageView>
<TextView
android:id="@+id/threat_effect_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
android:text="@string/exampleFlagshipText" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
The fragment code
class Threat : Fragment() {
companion object {
fun newInstance() = Threat()
}
private lateinit var viewModel: ThreatViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.internal_threat_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this).get(ThreatViewModel::class.java)
// TODO: Use the ViewModel
}
}