I have layouts which will be repetitive added in main layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/txt_temp_feels_like"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:text="-"
android:textColor="@color/gray"
android:textSize="18sp" />
I want it to be added to this container:
<LinearLayout
android:id="@ id/temp_feels_like_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
</LinearLayout>
And I want to do it in code. I'm using this method:
private fun setData(
layout: List<Int>,
container: List<ViewGroup>,
) {
for (i in layout.indices) {
container[i].addView(layoutInflater.inflate(layout[i], null))
}
}
where layout is link to resource of layout.
And it's fill it but i have some problems:
- I can't set text to this added view
- I can't set weight to this view
Maybe my algorithm is wrong. Can you help?
CodePudding user response:
Could something like this help?
<LinearLayout
android:id="@ id/temp_feels_like_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:orientation="horizontal"
>
<TextView
android:id="@ id/txt_temp_feels_like1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:visibility="gone"
android:text="-"
android:textColor="@color/gray"
android:textSize="18sp"
/>
<TextView
android:id="@ id/txt_temp_feels_like2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:visibility="gone"
android:text="-"
android:textColor="@color/gray"
android:textSize="18sp"
/>
<TextView
android:id="@ id/txt_temp_feels_like3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:visibility="gone"
android:text="-"
android:textColor="@color/gray"
android:textSize="18sp"
/>
<TextView
android:id="@ id/txt_temp_feels_like4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:visibility="gone"
android:text="-"
android:textColor="@color/gray"
android:textSize="18sp"
/>
</LinearLayout>
You can have the gravity set appropriately (and the text if it's not dynamic), and then set the visibility in code rather than try to add the view.
findViewById<View>(R.id.txt_temp_feels_like1).visibility = View.VISIBLE
CodePudding user response:
It depends on when should you set the text and gravity. If you can set it before adding to layout - you can do something like that:
val view = layoutInflater.inflate(layout[i], null) as TextView
view.apply {
text = "abc"
params = (params as LinearLayout.LayoutParams).apply {
weight = *value you need*
}
}
container[i].addView(view)
Or if you need to set text to view after views are added to layout, you can reference to view via container.children
/container.getChildAt(i)
UPD
you can get the child in the following way:
val count = layout.getChildCount()
var v: TextView? = null
for (i in 0..count) {
val view = layout.getChildAt(i)
if (view is TextView) {
v = view
}
}
Or one more way - set tag to view while adding it to layout and then you can get it by tag in any time you need