How to initialise controls and it's event in kotlin example (Simple kotlin example)

Guys, Here i am going to show you simple kotlin example, how we can define buttons, textview in kotlin and also how can we access it's properties.

1) Create layout xml file activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.chirag.kotlindemo.MainActivityKotlin">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/textView"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout>

2) Java file for activity : MainAcitivityJava.java

public class MainActivityJava extends AppCompatActivity {

    private Button button;
    private TextView textView;
    private boolean changeLanguage = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intializeUiControls();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeText();
            }
        });
    }

    private void changeText() {
        if(changeLanguage){
            textView.setText("Kotlin");
            textView.setTextColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        }else{
            textView.setText("Java");
            textView.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
        }
        changeLanguage = !changeLanguage;
    }

    private void intializeUiControls() {
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView);
    }
}

3) Kotlin file for code : MainActivityKotlin.kt

class MainActivityKotlin : AppCompatActivity() {

    private var button: Button? = null
    private var textView: TextView? = null
    private var changeLanguage = false

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

        intializeUiControls()

        button!!.setOnClickListener { changeText() }
    }

    private fun changeText() {
        if (changeLanguage) {
            textView!!.text = "Kotlin"
            textView!!.setTextColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
        } else {
            textView!!.text = "Java"
            textView!!.setTextColor(ContextCompat.getColor(this, R.color.colorAccent))
        }
        changeLanguage = !changeLanguage
    }

    private fun intializeUiControls() {
        button = findViewById(R.id.button) as Button
        textView = findViewById(R.id.textView) as TextView
    }
}

Note : Above is the java file and it's kotlin source code file for the same functionality.


Comments