Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Set gradient programmatically not working when set during setClickListener. Why?

Posted on November 11, 2022 By

Solution 1 :

Apparently, I need to invalidate it after setting it

    private fun setTextGradient() {
        val paint: TextPaint = text_happy.paint
        val width = paint.measureText(text_happy.text.toString())

        val textShader: Shader = LinearGradient(
            0f, 0f, width, text_happy.textSize, intArrayOf(
                Color.parseColor("#F97C3C"),
                Color.parseColor("#FDB54E"),
                Color.parseColor("#64B678"),
                Color.parseColor("#478AEA"),
                Color.parseColor("#8446CC")
            ), null, TileMode.CLAMP
        )
        text_happy.paint.shader = textShader
        text_happy.invalidate()  // Add the invalidation here
    }

Problem :

I programmatically set the gradient (as per the approach shared in https://stackoverflow.com/a/52289927/3286489)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setTextGradient()  // Set here, and it is working
    btn_press_me.setOnClickListener {
       // Do nothing
    }
}

private fun setTextGradient() {
    val paint: TextPaint = text_happy.paint
    val width = paint.measureText(text_happy.text.toString())

    val textShader: Shader = LinearGradient(
        0f, 0f, width, text_happy.textSize, intArrayOf(
            Color.parseColor("#F97C3C"),
            Color.parseColor("#FDB54E"),
            Color.parseColor("#64B678"),
            Color.parseColor("#478AEA"),
            Color.parseColor("#8446CC")
        ), null, TileMode.CLAMP
    )
    text_happy.paint.shader = textShader
}

This works. However, if I move setTextGradient() into the setOnCLickListner, why it is not working?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    btn_press_me.setOnClickListener {
        setTextGradient()  // Move here and it is not working.
    }
}

private fun setTextGradient() {
    val paint: TextPaint = text_happy.paint
    val width = paint.measureText(text_happy.text.toString())

    val textShader: Shader = LinearGradient(
        0f, 0f, width, text_happy.textSize, intArrayOf(
            Color.parseColor("#F97C3C"),
            Color.parseColor("#FDB54E"),
            Color.parseColor("#64B678"),
            Color.parseColor("#478AEA"),
            Color.parseColor("#8446CC")
        ), null, TileMode.CLAMP
    )
    text_happy.paint.shader = textShader
}
READ  [FIXED] java - How to retrieve using the Client code the Access Token generated on the Server-side?
Powered by Inline Related Posts
Android Tags:android, gradient

Post navigation

Previous Post: [FIXED] React Native floating animation
Next Post: [FIXED] java – maintain single database between customer and seller of android app

Related Posts

[FIXED] java – How to access apis from service in localhost (Mac) using android device Android
[FIXED] java – How to make an Android App have a folder containing images on Installation? Android
[FIXED] android studio – im learning flutter and using my pc and its working fine .. on the new pc when i try to debug it show this error only when i use AVD .. chrome is fine Android
[FIXED] More than one file was found with OS independent path ‘META-INF/INDEX.LIST’ (Android / Gradle) Android
[FIXED] android – How to make 2 buttons stick together side by side? Android
[FIXED] android – View pager with tabs layout is out of screen bounds Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme