-2

I have this layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@color/white"
    android:orientation="vertical">

        <TextView
            android:id="@+id/txt_daytime"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fontFamily="sans-serif-medium"
            android:text="-"
            android:textColor="@color/gray"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/txt_temp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fontFamily="sans-serif-medium"
            android:text="-"
            android:textColor="@color/black"
            android:textSize="18sp" />

</LinearLayout>

I want to add it to my linear layout in main layout:

<LinearLayout
                android:id="@+id/temp_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:orientation="horizontal">

in code. I'm using this:

val dayTime = listOf(DayTime.NIGHT, DayTime.MORNING, DayTime.DAY, DayTime.EVENING)

        val tempContainer = binding.tempContainer

        for (temp in dayTime) {
            val weather = weatherData.getWeather(temp)
            val view = LayoutInflater.from(requireContext()).inflate(R.layout.temp_layout, tempContainer) as LinearLayout

            view.apply {


                layoutParams = (layoutParams as LinearLayout.LayoutParams).apply {
                    weight = 1f
                }
            }

but how can i set values to those two TextViews in my reusing layout in code? When i'm trying to get children in this view i can't choose TextView and also i can't set values to this children

SoulReaver313
  • 365
  • 1
  • 11

2 Answers2

0
((TextView) view.findViewById(R.id.txt_daytime)).setText("Custom1");

you can use this below code also

LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
this.m_vwJokeLayout.addView(tv);
Saiful Sazib
  • 451
  • 1
  • 3
  • 14
0

It's very simple to assign a value on text view.

1 ) you need to initialize textview like this:

val textView = findViewById(R.id.text_view_id) as TextView

2 ) set a text like this

textView.text = "Your text that you want to set"

Mohak Shah
  • 518
  • 3
  • 12