Kotlin: setText not works - android

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?

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

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

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
}
}

Related

How do I use shared preferences when a button is clicked

I am working on an Android app using Android Studio.
I want to save the values input into some editText textFields.
These textFields are in a Fragment view? (sorry I am very new to Android coding).
I have tried to use SharedPreferences but keep getting the error.
(Unresolved reference: getSharedPreference).
Here is my code:
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
class ConnectFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_connect, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button_connect).setOnClickListener {
val serverURIFromEditText =
view.findViewById<EditText>(R.id.edittext_server_uri).text.toString()
val clientIDFromEditText =
view.findViewById<EditText>(R.id.edittext_client_id).text.toString()
val usernameFromEditText =
view.findViewById<EditText>(R.id.edittext_username).text.toString()
val pwdFromEditText =
view.findViewById<EditText>(R.id.edittext_password).text.toString()
val mqttCredentialsBundle = bundleOf(
MQTT_SERVER_URI_KEY to serverURIFromEditText,
MQTT_CLIENT_ID_KEY to clientIDFromEditText,
MQTT_USERNAME_KEY to usernameFromEditText,
MQTT_PWD_KEY to pwdFromEditText
)
findNavController().navigate(
R.id.action_ConnectFragment_to_ClientFragment,
mqttCredentialsBundle
)
val sharedPreferences: SharedPreferences =
getSharedPreferences("MyFacts", MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString("UserName", usernameFromEditText)
editor.apply()
}
}
}
Not sure what is up except to think that it is not in the MainActivity view but in a Fragment. But can't figure out how to resolve it. Could someone help me.

Incorrect fragment name

I am using fragments in my android application, when I run the program, the percentage name is displayed incorrectly, but after I switch to another fragment and perform some actions, everything becomes as it should be.
I set the title for the fragment on this line:
(activity as AppCompatActivity).supportActionBar?.title = "Soccer Quiz"
but it only applies when moving to another fragment.
My kotlin code:
package com.example.soccerquiz
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.navigation.Navigation
import com.example.soccerquiz.databinding.FragmentWelcomeScreenBinding
/**
* A simple [Fragment] subclass.
*/
class WelcomeScreenFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val binding: FragmentWelcomeScreenBinding =
DataBindingUtil.inflate(
inflater, R.layout.fragment_welcome_screen, container, false
)
binding.letsPlayButton.setOnClickListener { view: View ->
Navigation.findNavController(view)
.navigate(R.id.action_welcomeScreenFragment_to_quizFragment)
}
(activity as AppCompatActivity).supportActionBar?.title = "Soccer Quiz"
return binding.root
}
}
https://i.stack.imgur.com/LzNLk.png
https://i.stack.imgur.com/66NXk.png
Please help me with my problem.
It was necessary to change the title of fragment in navigation graph.
No need to install manually, because when you start the program it does not always work, you have to switch between fragments.

Method setCurrentState must be called on the main thread, Android, Kotlin

Android studio shows errors in the lines
REPOSITORY.insert(note){ onSuccess() }
in the "AddNewNoteFragmentViewModel" and in the lines
viewModel.insert(AppNote(name = name, text = text)){ view?.findNavController()?.navigate(R.id.action_addNewNoteFragment_to_mainFragment)}
in the file "AddNewNoteFragment"
I realized that the error is related to streams, but I do not know how to solve it
If anyone knows, please help, I could not find anything worthwhile on the Internet
AddNewNoteFragment
package com.example.notes.fragments.add_new_note
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import com.example.notes.R
import com.example.notes.databinding.FragmentAddNewNoteBinding
import com.example.notes.model.AppNote
import com.example.notes.utilits.showToast
class AddNewNoteFragment : Fragment() {
private var _binding: FragmentAddNewNoteBinding? = null
val binding get() = _binding!!
private lateinit var viewModel: AddNewNoteFragmentViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentAddNewNoteBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onStart() {
super.onStart()
initialization()
}
private fun initialization() {
viewModel = ViewModelProvider(this).get(AddNewNoteFragmentViewModel::class.java)
binding.buttonAddNote.setOnClickListener {
val name = binding.inputNameNote.text.toString()
val text = binding.inputTextNote.text.toString()
if (name.isEmpty()){
showToast("Введите имя заметки")
} else{
viewModel.insert(AppNote(name = name, text = text)){
view?.findNavController()?.navigate(R.id.action_addNewNoteFragment_to_mainFragment)
}
}
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
AddNewNoteFragmentViewModel
package com.example.notes.fragments.add_new_note
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.example.notes.model.AppNote
import com.example.notes.utilits.REPOSITORY
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class AddNewNoteFragmentViewModel(application: Application): AndroidViewModel(application) {
fun insert(note: AppNote,onSuccess:()-> Unit) =
viewModelScope.launch (Dispatchers.IO){
REPOSITORY.insert(note){
onSuccess()
}
}
}
Error screen
enter image description here
Problem solved
In AddNewNoteFragmentViewModel requires Dispatchers.IO to be changed to Dispatchers.Main

Viewmodel factory? is it something i need?

I am programing an app in kotlin using MVVM structur. i tried to instantiate my viewmodel through my factory. Though im getting an error when i start the app. It says this `
java.lang.RuntimeException: Cannot create an instance of class com.example.paperseller.ui.menu.ViewModels.AuthViewModel
I have no clue at all why im getting this trouble. My code looks like this
I have already tried to skip the factory implementation.
I would not use this way of implementing mvvm in kotlin nowadays. I would take a look at renewed tutorials 2021.
LoginRegister_fragment:
package com.example.paperseller.ui.menu
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.paperseller.Data.User
import com.example.paperseller.R
import com.example.paperseller.databinding.LoginRegisterFragmentBinding
import com.example.paperseller.ui.menu.ViewModels.AuthViewModel
class LoginRegister_fragment : Fragment(R.layout.login_register_fragment) {
private var _binding : LoginRegisterFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
{
_binding = LoginRegisterFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initializeUI()
}
private fun initializeUI()
{
val factory = InjectorUtils.providePaperSellerViewModelFactory()
val viewModel : AuthViewModel by viewmodels()
viewModel.message.observe(this)
{
message ->
Toast.makeText(requireActivity(), message, Toast.LENGTH_LONG).show()
}
binding.registerButton.setOnClickListener()
{
val user = User(binding.emailText.text.toString(), binding.passwordText.text.toString());
viewModel.register(user)
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
ViewModelFactory:
package com.example.paperseller.ui.menu.ViewModelFactory
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.paperseller.Data.PaperSellerRepository
import com.example.paperseller.ui.menu.ViewModels.AuthViewModel
class PaperSellerViewModelFactory(private val paperSellerRepository: PaperSellerRepository) : ViewModelProvider.NewInstanceFactory()
{
#Suppress("UNCHECKED_CAST")
override fun <T:ViewModel?> create(modelClass: Class<T>):T{
return AuthViewModel(paperSellerRepository) as T
}
}
I tried to follow this tutorial:
https://resocoder.com/2018/09/07/mvvm-on-android-crash-course-kotlin-
This tutorial is 4 years old. Tip to people seeing this question later. Check tutorials on youtube that is up to date this helped me alot.
Your ViewModel takes in a repository and since then You have to use the ViewModelFactory. You can either use ViewModelProvider:
val factory = InjectorUtils.providePaperSellerViewModelFactory()
val viewModel = ViewModelProvider(
this,
factory)
.get(AuthViewModel::class.java)
or, as #IR42 has pointed out, You can use viewModels delegate:
val viewModel: AuthViewModel by viewModels { factory }

Button In Kotlin will not work in Fragments

So I'm trying to create a tabbed app in Kotlin and I have chosen the default one they made for you to practice but I can't figure out how to get the buttons working `
package com.example.android_app.ui.home
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.example.android_app.R
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_home.*
class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Program Buttons
logout.setOnClickListener{
val intent = Intent(this, sign_in::class.java)
startActivity(intent)
}
}
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
})
return root
}
}
`
As far as I know this should work. The problem is not in the button but in Intent. My button is already defined and has no errors but there is a red line under Intent even though it's imported. The error message is below.
public constructor Intent(p0: Context!, p1: Class<*>!) defined in android.content.Intent
public constructor Intent(p0: String!, p1: Uri!) defined in android.content.Intent
You need to pass context into the constructor of Intent instead of this.
val intent = Intent(context, sign_in::class.java)

Categories

Resources