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
CodePudding user response:
Try this
ContextCompat.getColor(applicationContext,R.color.banner_regular)
CodePudding user response:
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