Now let’s see some basic widgets of Android :
First, Create a new project with any name you wish to name it. In this tutorial, I am naming my project as Project_Text and named the Activity as MainActivity.kt (you don’t have put the extention “.kt”).
Creating the widgets are as same is in android studio using java.
Your MainActivity should look like this :
MainActivity.kt
package com.a4akhilsudha.project_text import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Find the ids of EditText, TextView & Button val editText = findViewById(R.id.editText) as EditText val button = findViewById(R.id.button) as Button val textView = findViewById(R.id.textView) as TextView // Set on click listener to button in order to get the text in textView entered in the EditText. button.setOnClickListener { textView.text = editText.text.toString() } } }
and your activity_main.xml should look like this :
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.a4akhilsudha.project_text.MainActivity"> <EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Enter the text here" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="44dp" app:layout_constraintHorizontal_bias="0.0" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@+id/editText" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:layout_marginTop="24dp" android:text="Show Text"/> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" android:hint="sample text" android:gravity="center" android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/button" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.282" /> </android.support.constraint.ConstraintLayout>
Now run the above code on a device.
Screenshot of the output
Great extension to Hello World programme.