Home > Net >  How to set text to TextView in LinearLayout in code?
How to set text to TextView in LinearLayout in code?

Time:09-22

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

CodePudding user response:

((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);

CodePudding user response:

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"

CodePudding user response:

In order to you to use a textView from the layout you either need to have to include the layout on the View you are inflating or put the textView that you want to access in the correct layout, you can do this with fragments and/or activities.

The piece of code you provided seems like you dont have any textView inside the linearLayout with the id "temp_container", instead you have them in another linearLayout.

To add/initialize the textView you just need to

val txtDayTime = findViewById(R.id.txt_daytime) as TextView

or

val txtTemp = findViewById(R.id.txt_temp) as TextView

  • Related