Issue
I have subclassed a Button in Kotlin. There are about 10 buttons in the UI that will inherit this new button class, CustomButton
.
For a very simple proof of concept, I am trying to set some of the buttons' halfAlpha
property to true, to change the alpha to 0.5f
.
class CustomButton : Button {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs){
if (halfAlpha){
this.alpha = 0.5f
}
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs,
defStyleAttr)
var halfAlpha = false
}
Inside of my MainActivity class, I am calling this function in onCreate:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lockButtons()
}
private fun lockButtons(){
button1.halfAlpha = true
button2.halfAlpha = false
button3.halfAlpha = true
button4.halfAlpha = false
button5.halfAlpha = true
// etc...
}
The problem is... button 1, button 3 & button 5's alpha never changes to 0.5.
If I set var halfAlpha = true
in CustomButton, then all of the buttons alpha are 0.5.
How can I change the alpha on button 1,3, 5 using this subclassed Button class?
Solution
add this function in your CustomButton
fun setAlpha(isHalfAlpha: Boolean) {
if (halfAlpha){
this.alpha = 0.5f
}
}
invoke it
button1.setAlpha(true)
button2.setAlpha(false)
button3.setAlpha(true)
button4.setAlpha(false)
button5.setAlpha(true)
Hope this helps.
Answered By - GHH
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.