user13222595
user13222595

Reputation:

Kotlin: setText not works

I'm trying change some text after load a fragment. It will be location tracker in future. So I'm testing change of the text.

build.gradle module:

android {
      buildFeatures{
        dataBinding = true
        viewBinding = true
    }
}

start.xml:

    <TextView
        android:id="@+id/location"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:text="Current GPS Location"
        android:textColor="#FF0000"
        android:textSize="30sp"
        android:textStyle="bold" />

Start.kt:

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil.setContentView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.basic.databinding.StartBinding

import android.util.Log


class Start : Fragment() {

    private lateinit var binding: StartBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = StartBinding.inflate(layoutInflater)
        binding.location.setText("Hello World")
    }

}

Application runs without problem but setText Not works. Do you know why?

Upvotes: 0

Views: 1952

Answers (3)

user13222595
user13222595

Reputation:

Thanks for answers!

Solution is very simple.

Here is solution for people who will find this question:

class Start : Fragment() {

        lateinit var binding : StartBinding //this must be at the begin of your class

     override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = StartBinding.inflate(layoutInflater,container,false) //this must be here in onCreateView 
        return binding.root
    }

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.textView.setText("Hello World") // and now setText works in onViewCreated
    }

}

Upvotes: 0

Zain
Zain

Reputation: 40840

You should do the binding in onCreateView instead of onCreate as in onCreate() the fragment view is not created yet.

So transfer below in onCreateView

binding = StartBinding.inflate(layoutInflater)
binding.location.setText("Hello World")

For more info check fragment lifecycle

Upvotes: 0

cutiko
cutiko

Reputation: 10517

You are initializing your binding but not adding it to the view

onCreateView(inflater... container...) {
    binding = StartBinding.inflate(layoutInflater, container, false)
    return binding.root
}

then following the life cycle usage previous to binding, you have to make the change after the view is created

onViewCreated(...) {
    binding.location.setText("Hello World")
}

A small comment, your naming doesn't follow conventions:

  • StartFragment
  • fragment_start

Upvotes: 1

Related Questions