SwitchCompat onClick not firing from XML layout - android

We use databinding in out project thus not instantiating controls in code, but doing that within the layout xml. We've done that throughout the app with all UI controls and it works great.
Problem is that I've now added a SwitchCompat control to my layout. It only has onClick (not firing) and it doesn't have onCheckedChange which you would expect from a SwitchCompat, right?
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="music.queue_ui.list.QueueListViewModel" />
</data>
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="#{() -> viewModel.toggleHeader()}" />
</layout>
The toggleHeader never get called :-(
package music.queue_ui.list
import android.widget.Toast
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
class QueueListViewModel(
) : ViewModel() {
private val disposables = CompositeDisposable()
fun toggleHeader() {
// This never gets hit by SwitchCompat `onClick`.
// Why??????
Toast.makeText(
context, "SwitchCompat Clicked",
Toast.LENGTH_LONG
).show()
}
override fun onCleared() {
super.onCleared()
disposables.clear()
}
}
Anyone one knows of a good solution?

Use:
android:onCheckedChanged="#{(v,checked)->viewModel.toggleHeader(checked)}"

Related

Why my code is not saving data using shared preferences?

I try to save the state of a Number Text View but is not working. At first I was thinking that the app was not reading my data because I copy the code from the oficial documentation. But after investigate a little more I look in android/data/ and there was not a folder called sad.whyIsNot.Saving, I tried many tutorials on youtube but I do not know why I am doing wrong. Can some one of you guys explain to me what is happening and how can I solve it?
This is the code for my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/vplayeras1"
android:layout_width="45dp"
android:layout_height="30dp"
android:layout_gravity="center_horizontal"
android:text="#string/valorDefectoVeces"
android:textSize="24sp" />
<Button
android:id="#+id/agrega1"
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/playera1" />
</LinearLayout>
And this is the code of my MainActivity.
package sad.whyIsNot.Saving
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadData()
val btnPlayera1: Button = findViewById(R.id.agrega1)
btnPlayera1.setOnClickListener{aumenta1()}
}
private fun loadData(){
val sharedPref = getPreferences(Context.MODE_PRIVATE)
val valPlayera1 = sharedPref.getInt("VECES_PLAYERA1", 0)
}
private fun aumenta1() {
val resultText: TextView = findViewById(R.id.vplayeras1)
val resultInt = resultText.text.toString().toInt() + 1
resultText.text = resultInt.toString()
val sharedPref = getPreferences(Context.MODE_PRIVATE) ?: return
with (sharedPref.edit()) {
putInt("VECES_PLAYERA1", resultInt)
commit()
}
Toast.makeText(this,"Se ha guardado!", Toast.LENGTH_LONG).show()
}
}
I don't see anything wrong with your code, I assume you know that loadData doesn't do anything with the data and you already tested if you have access to it?
Accessing the preferences outside of MainActivity might not work because getPreferences is bound to a specific activity.
If aumenta1 doesn't return I doubt that the value isn't put inside of the File, so I'd rather check if resultInt isn't 0 and that loadData really can't access the value

BindingAdapter is not working as expected

I am trying to add a custom binding to my TextView. Please find my code below
MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.builders.bindingadaptertest.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val model = ViewModelProvider(this).get(MainViewModel::class.java)
}
}
MainViewModel.kt
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class MainViewModel: ViewModel() {
var data = MutableLiveData<String>()
init {
data.value = "This works"
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.builders.bindingadaptertest.MainViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:customBind="#{viewModel.data}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
BindingAdapters.kt
#BindingAdapter("app:customBind")
fun customBind(view: TextView, data: String?) {
Log.d("DEBUG_CUSTOM", "Setting custom [$data] listner")
view.text = data
}
Doubt: When I run this project, The log cat shows the data passing to customBind is null.
2020-11-30 09:37:33.351 4682-4682/com.builders.bindingadaptertest D/DEBUG_CUSTOM: Setting custom [null] listner
Can anyone help me to get the correct data from ViewModel into the binding adapter?
It's null, because in your databinding viewModel is not set.
You forgot to add binding.viewModel = model
Also, some possible suggestions:
No need to leave the LiveData reference mutable, just use val
I'd prefer an extension function for the binding adapter: fun TextView.customBind(data: String?)
You should set a lifecycleOwner on your binding, otherwise your activity may keep an indirect reference on your vm, which will cause leaks on orientation changes

Android - DialogFragment doesn't show the ImageView that I put into it

I faced an error that actually mustn't occur, because I do exactly the same thing in my project in another screen, and it works there, but doesn't want to work in another screen.
The problem is the following: from an Activity in a result of some action I open up a DialogFragment which contains an image and other views in its layout file. Now I can't understand why, but it works in the first case (you'll see below) but doesn't work in the second...
First case:
Layout XML file (dialog_character_selector.xml):
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="dialogViewModel"
type="neptun.jxy1vz.cluedo.ui.menu.character_selector.CharacterSelectorViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="#+id/spinnerCharacter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/ivCharacterCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="20dp"
android:src="#drawable/szereplo_hatlap"
app:layout_constraintBottom_toTopOf="#+id/btnStart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinnerCharacter" />
<Button
android:id="#+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:onClick="#{()->dialogViewModel.startGame()}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
The DialogFragment's Kotlin source code (CharacterSelectorDialog.kt):
package neptun.jxy1vz.cluedo.ui.menu.character_selector
import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.appcompat.app.AlertDialog
import androidx.core.animation.doOnEnd
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import neptun.jxy1vz.cluedo.R
import neptun.jxy1vz.cluedo.databinding.DialogCharacterSelectorBinding
class CharacterSelectorDialog : DialogFragment(), AdapterView.OnItemSelectedListener {
private lateinit var dialogCharacterSelectorBinding: DialogCharacterSelectorBinding
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
dialogCharacterSelectorBinding = DataBindingUtil.inflate(
LayoutInflater.from(context),
R.layout.dialog_character_selector,
null,
false
)
dialogCharacterSelectorBinding.spinnerCharacter.adapter = ArrayAdapter<String>(
context!!,
android.R.layout.simple_spinner_dropdown_item,
resources.getStringArray(R.array.characters)
)
dialogCharacterSelectorBinding.spinnerCharacter.onItemSelectedListener = this
//I do this due to a card flipping animation, it's not important, not part of my problem
val scale = resources.displayMetrics.density
dialogCharacterSelectorBinding.ivCharacterCard.cameraDistance = 8000 * scale
dialogCharacterSelectorBinding.dialogViewModel = CharacterSelectorViewModel(context!!)
return AlertDialog.Builder(context!!, R.style.Theme_AppCompat_Light_Dialog).setView(dialogCharacterSelectorBinding.root).setTitle(resources.getString(R.string.dialog_character_title)).create()
}
override fun onNothingSelected(parent: AdapterView<*>?) {
dialogCharacterSelectorBinding.ivCharacterCard.setImageResource(R.drawable.szereplo_hatlap)
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
dialogCharacterSelectorBinding.ivCharacterCard.setImageResource(R.drawable.szereplo_hatlap)
(AnimatorInflater.loadAnimator(context, R.animator.card_flip) as AnimatorSet).apply {
setTarget(dialogCharacterSelectorBinding.ivCharacterCard)
start()
doOnEnd {
dialogCharacterSelectorBinding.dialogViewModel!!.setPlayer(position)
val img = when (position) {
0 -> R.drawable.szereplo_ginny
1 -> R.drawable.szereplo_harry
2 -> R.drawable.szereplo_hermione
3 -> R.drawable.szereplo_ron
4 -> R.drawable.szereplo_luna
else -> R.drawable.szereplo_neville
}
dialogCharacterSelectorBinding.ivCharacterCard.setImageResource(img)
}
}
}
}
Second case:
Layout XML file (dialog_helper_card.xml):
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="helperCardDialogViewModel"
type="neptun.jxy1vz.cluedo.ui.dice.card_dialog.helper.HelperCardViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ivHelperCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="20dp"
android:src="#drawable/mento_hatlap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
I think in the main parts it's just the same as the previous one.
Kotlin source file (HelperCardDialog.kt):
package neptun.jxy1vz.cluedo.ui.dice.card_dialog.helper
import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import androidx.core.animation.doOnEnd
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import neptun.jxy1vz.cluedo.R
import neptun.jxy1vz.cluedo.databinding.DialogHelperCardBinding
class HelperCardDialog(private val cardResource: Int) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog {
val dialogHelperCardBinding = DataBindingUtil.inflate<DialogHelperCardBinding>(LayoutInflater.from(context), R.layout.dialog_helper_card, null, false)
dialogHelperCardBinding.helperCardDialogViewModel = HelperCardViewModel()
(AnimatorInflater.loadAnimator(context, R.animator.card_flip) as AnimatorSet).apply {
setTarget(dialogHelperCardBinding.ivHelperCard)
start()
doOnEnd {
dialogHelperCardBinding.ivHelperCard.setImageResource(cardResource)
}
}
return AlertDialog.Builder(context!!, R.style.Theme_AppCompat_Dialog).setTitle(resources.getString(R.string.got_helper_card)).setNeutralButton(resources.getString(R.string.ok)
) { dialog, _ ->
dialog.dismiss()
}.create()
}
}
That's it. These are my most important files in my problem. Sorry for the lot of code...
I hope you will see where the problem is and tell me what's the solution for it.
Finally I found the error in my code. I left the setView() function call from the second AlertDialog.Builder().
The correct code snippet is:
return AlertDialog.Builder(context!!, R.style.Theme_AppCompat_Dialog)
.setView(dialogHelperCardBinding.root)
.setTitle(resources.getString(R.string.got_helper_card)).setNeutralButton(
resources.getString(R.string.ok)
) { dialog, _ ->
dialog.dismiss()
}.create()

How to use TextInputLayout in preferenceScreen

I am trying to use TextInputLayout in a preference screen so as have a nice neat password box that has a show password option. I know there are ways and means of doing it with checkboxes and scripting but I would really love to be to just use the TextInputLayout, maybe wrapped or in a custom widget. Gradle dependencies are correct as it works fine in other activity screens.
The following preferences.xml crashes with an "Error inflating class com.google.android.material.textfield.TextInputLayout"
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
</androidx.preference.PreferenceScreen>
Ok I worked out the answer (and learned a lot in the process). Took a lot of research and trial and error but I hope the following might prove helpful to someone similarly frustrated.
The custom preference layout:
password.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textLay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="66dp"
android:hint="#string/Set_Password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#android:id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
The preference screen layout:
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.toggen.myapplication.PasswordPreference
android:key="pref_password"
android:layout="#layout/password" />
</PreferenceScreen>
SettingsActivity.kt
package com.toggen.myapplication
import kotlinx.android.synthetic.main.password0.view.*
import androidx.appcompat.app.AppCompatActivity
import android.util.AttributeSet
import android.widget.EditText
import android.content.Context
import androidx.preference.*
import android.os.Bundle
class PasswordPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs){
private var passW: EditText? = null
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
passW = holder?.itemView?.textLay?.editText?.apply{ setText(getPersistedString("admin")) }
}
override fun onDetached() {
super.onDetached()
passW?.apply{ persistString("$text") }
}
}
class SettingsActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if(savedInstanceState == null) {
supportActionBar?.title = "Settings"
supportFragmentManager.beginTransaction()
.replace(android.R.id.content, SettingsFragment()).commit()
}
}
override fun onSupportNavigateUp() = onBackPressed().run { true }
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) = setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
Finally the answer that I've been looking for!! THANK YOU!!
Only modification that I would make is to use:
android:dialogLayout="#layout/password"
instead of:
android:layout="#layout/password"

Kotlin Android - Unable to get #BindingAdapter to work

I keep getting the binding error when trying to use the #BindingAdapter. Try for 3 days and follow numerous online articles on this subject, but still getting the below error.
#BindingAdapter("focusableColor")
fun setFocusableColor(v:CardView, color:Int) {
println("hello")
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding:ActivityMainBinding =
DataBindingUtil.setContentView(this,R.layout.activity_main)
etc...
}
In current_task_layout.xml
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="task" type="com.edenhan.simplytask.Task">
</variable>
</data>
<android.support.v7.widget.CardView
android:id="#+id/card_view">
.....
focusableColor="#{1}"/>
Error encountered:
Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute ‘focusableColor’ with parameter type int on
android.support.v7.widget.CardView.
file:D:\…….\app\src\main\res\layout\current_task_layout.xml
Have you tried moving the binding out of the companion object?
You should put it in a kotlin file and make it a top level function. For example:
Bindings.kt
#BindingAdapter("focusableColor")
fun setFocusableColor(v:CardView, color:Int) {..}
And put the binding xml in the app namespace
Also, see Kotlin custom attribute databinding
edit: full example
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
#BindingAdapter("focusableColor")
fun setColor(card: CardView, #ColorInt color: Int) {
// or whatever
card.setBackgroundColor(color)
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.CardView
android:id="#+id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:focusableColor="#{1}"/>
</android.support.constraint.ConstraintLayout>
</layout>
Below defined full code for load image using BindingAdapter with Kotlin
ImageLoader.kt
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableField
class ImageLoader {
val imageResource = ObservableField(R.drawable.ic_launcher_background)
companion object {
#JvmStatic
#BindingAdapter("android:src")
fun setImage(imageView: ImageView, imageRes: Int) {
imageView.setImageResource(imageRes)
}
}
}
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="imageLoader" type="com.sample.testdemo.ImageLoader"/>
</data>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#{imageLoader.imageResource}"
android:layout_centerInParent="true"/>
</RelativeLayout>
</layout>
HomeActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.sample.testdemo.databinding.ActivityHomeBinding
class HomeActivity : AppCompatActivity() {
lateinit var binding: ActivityHomeBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_home)
binding.imageLoader = ImageLoader()
}
}
Note: Don't forget to add below line at the top of app level build.gradle
apply plugin: 'kotlin-kapt'

Categories

Resources