/* 2022-09-19 File created by Kari Laitinen 2022-09-22 Last modification. (c) naturalprogramming.com This simple app contains a TextView and a Button. You can see how to use ConstraintSet to create the UI without XML. This is an Android app made of this single file. If you want to test this app, create an Android Kotlin project with the package name shown below. Then, store this file to the folder where MainActivity.kt is and make the following change to AndroidManifest.xml: android:name=".ButtonDemoNoXMLActivity" Useful links: https://www.youtube.com/watch?v=PyJRfHXUNbU Some 'warnings': - tab size in this file is 3 spaces - underscores are used in the names invented by the programmer */ package buttondemo.noxml import android.content.Context import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintSet import com.google.android.material.button.MaterialButton class ButtonDemoConstraintLayout( context : Context) : ConstraintLayout( context ) { init { id = generateViewId() // id for parent val basic_layout_params = ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) //setLayoutParams( basic_layout_params ) layoutParams = basic_layout_params setBackgroundColor( Color.LTGRAY ) val constraint_set = ConstraintSet() constraint_set.clone( this ) val changing_text = TextView( context ) changing_text.text = "HELLO!" changing_text.textSize = 32F changing_text.id = View.generateViewId() addView( changing_text ) constraint_set.constrainWidth( changing_text.id, ConstraintSet.WRAP_CONTENT ) constraint_set.constrainHeight( changing_text.id, 300 ) constraint_set.centerHorizontally( changing_text.id, ConstraintSet.PARENT_ID ) val button_to_press = MaterialButton( context ) button_to_press.id = generateViewId() button_to_press.setBackgroundColor( 0xFF808000.toInt() ) // Olive color. button_to_press.setTextColor( Color.WHITE ) button_to_press.textSize = 24F button_to_press.text = "PRESS" button_to_press.cornerRadius = 40 // Works only with MaterialButton, I guess. button_to_press.setOnClickListener{ if ( changing_text.text == "HELLO!" ) { changing_text.text = "WELL DONE!" } else { changing_text.text = "HELLO!" } } addView( button_to_press ) constraint_set.constrainWidth( button_to_press.id, 400 ) constraint_set.constrainHeight( button_to_press.id, 300 ) /* // The following two connections center the button horizontally. constraint_set.connect( button_to_press.id, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT ) constraint_set.connect( button_to_press.id, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT ) */ // The following seems to be an easier way for horizontal centering. constraint_set.centerHorizontally( button_to_press.id, ConstraintSet.PARENT_ID ) // Instead of connecting, we'll do chaining. //constraint_set.connect( button_to_press.id, ConstraintSet.TOP, // changing_text.id, ConstraintSet.BOTTOM, 300 ) val views_to_chain = intArrayOf( changing_text.id, button_to_press.id ) constraint_set.createVerticalChain( ConstraintSet.PARENT_ID, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, views_to_chain, null, ConstraintSet.CHAIN_PACKED ) constraint_set.applyTo( this ) } } class ButtonDemoNoXMLActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val button_demo_constraint_layout = ButtonDemoConstraintLayout( this ) setContentView( button_demo_constraint_layout ) } }