I read this article on changing the Toast message size in Java, but how would this be done in Kotlin?
Asked
Active
Viewed 553 times
1
-
This is how to increase the Toast size with spannables: [How can we increase the font size in Toast?](https://stackoverflow.com/a/26981183/15535190) – Miles Morales May 03 '21 at 22:18
1 Answers
1
The equivalent Kotlin code of the answer. Android Studio also transforms Java code into Kotlin automatically. Just copy and paste it.
Edit for activity :
val toast = Toast.makeText(this, "Text" , Toast.LENGTH_SHORT)
//the default toast view group is a relativelayout
val toastLayout = toast.getView() as ViewGroup
val toastTV = toastLayout.getChildAt(0) as TextView
toastTV.setTextSize(30)
toast.show()

Eren Tüfekçi
- 2,463
- 3
- 16
- 35
-
This caused the application to close, and said `java.lang.NullPointerException: null cannot be cast to non-null type android.widget.RelativeLayout`, referring to lines with `binding.fightButton.setOnClickListener { battle() }` and `val playerHitLayout = playerHitMonster.view as RelativeLayout`. Not sure what's going on. – Miles Morales Apr 26 '21 at 01:08
-
1I don't know about your context if it is activity or fragment, I just changed the code – Eren Tüfekçi Apr 26 '21 at 01:18
-
-
The error says "playerHitMonster.view" is null. Please check it if you declared it in correct way. – Eren Tüfekçi Apr 26 '21 at 21:43
-
Now I get `android.widget.Toast cannot be cast to android.widget.RelativeLayout`. Why would I want to cast the Toast to the Relative Layout? Because this doesn't seem to be working. – Miles Morales Apr 26 '21 at 22:01
-
I jusi converted the code to Kotlin the link you pointed, please try it with ViewGroup – Eren Tüfekçi Apr 26 '21 at 22:18
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231628/discussion-between-android-won-kenobi-and-eren-tufekci). – Miles Morales Apr 26 '21 at 22:57