FAB not triggering inside of AlertDialog android kotlin - android

I have a LinearLayout with 7 FAB's inside, and I'm trying to change their background tint color by clicking, the thing is, the first FAB does trigger and change the color but the rest doesn't and also, if I click in the first FAB and then in the rest of them, they change the tint color, but doesn't trigger! I tried to debug but nothig..
This is what I did.
val monday: FloatingActionButton = dialogLayout.findViewById(R.id.monday);
val tuesday: FloatingActionButton = dialogLayout.findViewById(R.id.tuesday);
monday.setOnClickListener {
if(java.lang.String.format("#%06X", 0xFFFFFF and monday.backgroundTintList?.defaultColor!!) == "#C9C9C9"){
monday.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.accent));
}
else{
monday.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#c9c9c9"));
}
}
tuesday.setOnClickListener {
if(java.lang.String.format("#%06X", 0xFFFFFF and monday.backgroundTintList?.defaultColor!!) == "#C9C9C9"){
tuesday.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.accent));
}
else{
tuesday.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#c9c9c9"));
}
}
It's very weird, but I think it has to do with the view it self as if the first item registered surpass the other ones.. I don't know!

This is your problem:
tuesday.setOnClickListener {
if(java.lang.String.format("#%06X", 0xFFFFFF and ----------> monday.backgroundTintList?.defaultColor!!) == "#C9C9C9"){ <----------------------
tuesday.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.accent));
}
else{
tuesday.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#c9c9c9"));
}
}
You are comparing to Monday's state and not Tuesday's. I am guessing this happens in the rest of your buttons.

Related

Android resources.getColor() freezes code

I need to add views to LinearLayout dynamically on POST request finished inside a fragment.
Here is my function that creates and setups the view and returns it, then I add it to the chatsRoot via addView() method. No exceptions, but views are not added.
After some debug I noticed, that in logcat there is only one message "Banner created1" and no message "Banner created2", like if the program freezed on loading the color resource.
Log.d("VIEW", "Banner created1") // appears in logcat
val colorFrom = resources.getColor(R.color.banner_regular, null)
Log.d("VIEW", "Banner created2") // not appears in logcat
private fun generateBanner(chatData: ChatData):View? {
val banner = inflater.inflate(R.layout.chat_banner, null, false)
banner.run {
findViewById<TextView>(R.id.user_name).text = chatData.name
findViewById<TextView>(R.id.short_code).text = chatData.shortCode
findViewById<TextView>(R.id.short_message).text =
chatData.lastMessage.let { it.substring(0, min(40, it.length)) + "..." }
findViewById<ImageView>(R.id.unread_dot).visibility =
if (chatData.hasUnread) VISIBLE else GONE
findViewById<TextView>(R.id.date_time_text).text = chatData.lastMessageTime
}
Log.d("VIEW", "Banner created1")
val colorFrom = resources.getColor(R.color.banner_regular, null)
Log.d("VIEW", "Banner created2")
val colorTo = resources.getColor(R.color.banner_clicked, null)
val animation = ValueAnimator.ofObject(
ArgbEvaluator(), colorFrom, colorTo, colorFrom
).apply {
addUpdateListener {
banner.setBackgroundColor(animatedValue as Int)
}
}
banner.setOnClickListener {
animation.start()
openChat(chatData.uid)
}
return banner
}
generateBanner() is called here, same situation: top is printed, bottom is not
private fun addChatsToRoot(chats:List<ChatData>) {
Log.d("VIEW", "Adding chats to root") // printed to logcat
for(chatData in chats) {
chatsData += chatData.uid to chatData
val banner = generateBanner(chatData)
chatsRoot.addView(banner)
}
Log.d("VIEW", "Chats are added to the layout") // not in logcat
}
I tried removing null parameter for the theme, nothing changed.
I also changed resources call to raw number representing white colors, layout created, nothing freezed the code, but layout inflated wrongly, banner.height is always zero, even if in its drawable file I set minHeight.
So it looks like I just cant load any kind of a resource from this fragment
Try this
ContextCompat.getColor(applicationContext,R.color.banner_regular)
Turned out all the problems were because I had a fragment inside another fragment. This caused problems not only with resource access, but also with inflating additional layout views.
My solution is to move fragments to one level and navigate using one navController

Android/Kotlin - adding TextInputLayout.END_ICON_CLEAR_TEXT to textview doesn't perform the setOnFocusChangeListener

i've a problem in android app written in Kotlin.
I've a textview that has a setOnFocusChangeListener and it worked fine as long as i've added an EndIconMode.
Now in my layout i see the END_ICON_CLEAR_TEXT and this works, but nothing happens when this textview loses focus.
How can I made this works?
textInputLayout = LayoutInflater.from(activity).inflate(R.layout.textinputlayouttemplate, null) as TextInputLayout
val textView = CreateUI._TextView(castToField(field), textInputLayout.context)
if (field.validatefield || field.nextpage != 0)
{
textView.setOnFocusChangeListener { _, hasFocus ->
if (!hasFocus && !GlobalVar.drawerOpened)
{
if ((field.mandatory && !textView.text.toString().isNullOrEmpty()) || (!field.mandatory)) {
if (field.validatefield) {
validateField(field.fieldcode, textView.text.toString(), field.nextpage)
} else {
_goToNextPageNo = field.nextpage
goToNextPageIfExist()
}
}
}
}
}
textInputLayout.addView(textView, layout_wMATCHPARENT_hWRAPCONTENT)
textInputLayout.layoutParams = layout_wMATCHPARENT_hWRAPCONTENT
textInputLayout.setPadding(0, 0, 0, 0)
textInputLayout.endIconMode = TextInputLayout.END_ICON_CLEAR_TEXT
linearLayout.addView(textInputLayout)
Not sure if this is still relevant for you but apparently this is because setting the endIconMode programatically will currently override onFocusChangeListener.
You will even have issues if you set this property via xml and set your own onFocusChangeListener. If you not invoke the previous set listener aswell the clear-icon will perform not as intended.
Either you set it via xml and invoke the default listener or use endIconMode="custom" and write your own logic.
Here is a similar issue

How to loop a set of code on Kotlin? (Android)

I have a button that changes the colour of a text. I want to change between the three colours. I've tried a while loop but the app simply would be blank. I've done some searching around but nothing I found worked.
Here is my MainActivity.kt code for the button:
btnChangeColor.setOnClickListener {
txtGavriel.setTextColor(Color.RED)
txtGavriel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 100f)
btnChangeColor.setOnClickListener {
txtGavriel.setTextColor(Color.BLUE)
btnChangeColor.setOnClickListener {
txtGavriel.setTextColor(Color.BLACK)
}
}
}
You can do something like this:
enum class Color { // I suppose that you have already defined this enum
Blue, Red, Green
}
var state: Int = 0
val colors = Color.values()
txtGavriel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 100f)
btnChangeColor.setOnClickListener {
state = (state + 1) % colors.size
txtGavriel.setTextColor(colors.get(state))
}

Bottom OnClickListener always unresolved

I have a simple button in my recyclerview, when clicked the first time it should make the text editable, when clicked the second time, it should confirm the change. The problem I'm having is that I have the two onClickListeners set up, but they refer to each other, and the bottom one can always resolve the top one, but the top one can't resolve the bottom one.
Recyclerview: bindIngredient
fun bindIngredient(ingredient: ListIngredientsQuery.Item, clickListener: RecyclerViewClickListener) {
val ocl1 = View.OnClickListener{
//Text Editable
view.ingEditText.setText(view.ingNameTV.text.toString())
view.ingNameTV.visibility = View.GONE
view.ingEditText.visibility = View.VISIBLE
view.ingEditButton.text = "Confirm"
view.ingEditButton.setOnClickListener(ocl2)
}
var ocl2 = View.OnClickListener {
//Text Not Editable
view.ingNameTV.text = view.ingEditText.text
view.ingEditText.visibility = View.GONE
view.ingNameTV.visibility = View.VISIBLE
view.ingEditButton.setOnClickListener(ocl1)
clickListener.onConfirmSelect(ingredient)
}
this.ingredient = ingredient
view.ingNameTV.text = ingredient.name()
view.ingEditButton.setOnClickListener(ocl1)
view.veganSpinner.setSelection(Vegan.valueOf(ingredient.vegan().toString()).ordinal, false)
view.gfSpinner.setSelection(GlutenFree.valueOf(ingredient.glutenfree().toString()).ordinal, false)
}
In this example the line
view.ingEditButton.setOnClickListener(ocl2)
errors because ocl2 is unresolved. If I switch the order of the two onClickListeners being declared and initialized, the line
view.ingEditButton.setOnClickListener(ocl1)
errors because ocl1 is resolved. I take this to mean that it won't look further down to find what it needs, it'll only rely on objects that have already been initialized.
Is there a way to fix this? Is there a better way to do this? I'm tempted to just put two buttons in the same spot, give them each their own onclicklistener and swap their visibility, but this seems like a waste of resources.
You need to declare your objects before you use them.
fun bindIngredient(ingredient: ListIngredientsQuery.Item, clickListener: RecyclerViewClickListener) {
val ocl1: View.OnClickListener
val ocl2: View.OnClickListener
ocl1 = View.OnClickListener{
//Text Editable
view.ingEditText.setText(view.ingNameTV.text.toString())
view.ingNameTV.visibility = View.GONE
view.ingEditText.visibility = View.VISIBLE
view.ingEditButton.text = "Confirm"
view.ingEditButton.setOnClickListener(ocl2)
}
ocl2 = View.OnClickListener {
//Text Not Editable
view.ingNameTV.text = view.ingEditText.text
view.ingEditText.visibility = View.GONE
view.ingNameTV.visibility = View.VISIBLE
view.ingEditButton.setOnClickListener(ocl1)
clickListener.onConfirmSelect(ingredient)
}
this.ingredient = ingredient
view.ingNameTV.text = ingredient.name()
view.ingEditButton.setOnClickListener(ocl1)
view.veganSpinner.setSelection(Vegan.valueOf(ingredient.vegan().toString()).ordinal, false)
view.gfSpinner.setSelection(GlutenFree.valueOf(ingredient.glutenfree().toString()).ordinal, false)
}
However, it would be better if you just used one OnClickListener. You can simply save which state you are in, and when the button is clicked, you just check which state you are in, perform your action, and then change the state. This way you don't have to worry about switching your listeners, which can get messy.

Xamarin.Forms: wrong button text alignment after click (Android)

I have a problem with Xamarin.Forms (version 1.2.2) on Android (Nexus 5).
The alignment of Button.Text is often not centered after performing a click.
In a short project, I figured out, that updating the UI causes the problem.
public class App
{
public static Page GetMainPage()
{
var label = new Label {
Text = "label",
};
var buttonBad = new Button {
Text = "buttonBad",
Command = new Command(() => label.Text += "1"),
};
var buttonGood = new Button {
Text = "buttonGood",
};
return new ContentPage {
Content = new StackLayout {
Children = {
buttonBad,
buttonGood,
label,
}
}
};
}
}
A click on "buttonBad" (updating the label.Text) causes the text-alignment of this button to not be centered anymore. A click on "buttonGood" does not cause the problem.
Is there a good workaround to solve this problem?
This workaround seems to be too complicated:
http://forums.xamarin.com/discussion/20608/fix-for-button-layout-bug-on-android
edit:
A programatically edit of the UI also cases the bug. Changing the label.Text in an async method after a short waiting leads the "buttonGood" to align its text wrong after a click.
edit2:
I created an example / test project on GitHub:
https://github.com/perpetual-mobile/ButtonTextAlignmentBug.git
The alignment is correct, when the StackLayout is replaced by an AbsolutLayout, but i need the StackLayout to work well.
Ok, after hours of dealing with this silly bug, I resolved it by implementing a custom renderer and overriding ChildDrawableStateChanged:
public override void ChildDrawableStateChanged(Android.Views.View child)
{
base.ChildDrawableStateChanged(child);
Control.Text = Control.Text;
}

Categories

Resources