MainActivity.kt
package com.example.jetpack
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// text to reverse
val text = "ANDROID DEVELOPERS"
// switch button checked change listener
switchButton.setOnCheckedChangeListener { compoundButton, b ->
if (b){
// when switch button is checked
textView.text = "Switch is on."
textView.append("\n\n${text.reversed()}")
}else{
// if switch button is unchecked
textView.text = "Switch is off."
textView.append("\n\n$text")
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EDEAE0"
tools:context=".MainActivity">
<Switch
android:id="@+id/switchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reverse Text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-condensed-medium"
android:padding="8dp"
tools:text="TextView"
android:textColor="#1034A6"
android:gravity="center"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switchButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - Volley JsonArrayRequest
- android kotlin - ChipGroup single selection
- android kotlin - NumberPicker string values
- android kotlin - NumberPicker text size
- android kotlin - NumberPicker text color
- android kotlin - EditText space validation
- android kotlin - EditText email validation
- android kotlin - Show soft keyboard when EditText is focused
- android kotlin - EditText hide error
- android kotlin - EditText allow only numbers
- android kotlin - EditText allow only characters
- android kotlin - Material switch button
- android kotlin - Get raw resource uri
- android kotlin - Button with icon
- android kotlin - Resize ImageView programmatically