Search code examples
androidandroid-activityandroid-button

How can I display a button in the onCreate method of an activity based on a given condition?


I want to display a button in an activity, but the text and background of the button are different if a specific condition is met by the user. How can I initialise the same button in the OnCreate method, having two different designs based on a condition?


Solution

  • Here Maybe this help

     val btnGet = findViewById<Button>(R.id.btnGet)
    
        if (id == 1){   //Your condition 
    
            btnGet.text = id.toString()
            btnGet.visibility = View.VISIBLE    //Make Sure in xml it is gone 
            btnGet.setTextColor(Color.parseColor("#bdbdbd"))
            btnGet.setBackgroundColor(Color.parseColor("#ffffff"))
        }else{
            btnGet.text = id.toString()
             btnGet.visibility = View.VISIBLE
            btnGet.setTextColor(Color.parseColor("#FFBB86FC"))
            btnGet.setBackgroundColor(Color.parseColor("#FF03DAC5"))
    
        }
    

    If you want the full code I will share it with you Happy coding