I am trying to display a list a items in my Kotlin app using compose by default, and using AndroidView in a Composable.
Currently, I have this working piece of code which is a composable function inflating my XML layout:
- MainActivity.kt:
@Composable
fun TableWithXML() {
AndroidView(
factory = { View.inflate(it, com.example.myapplication.R.layout.my_table_layout, null)
},
modifier = Modifier.fillMaxWidth(),
update = {}
)
}
- my_table_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mytesttableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ICC Ranking of Players:"
android:textSize="20dp"
android:textStyle="bold"></TextView>
<TableRow
android:background="#51B435"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rank" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Player" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Team" />
</TableRow>
<TableRow
android:background="#F0F7F7"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Virat Kohli" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="IND" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="895" />
</TableRow>
</TableLayout>
Nom, I'd like to have the table set dynamically from a list of items.
How could I achieve that ?