ButterKnife with Kotlin
Butter Knife With Kotlin (No Need)
Butter Knife
Butter knife is a little and light weight library used to inject android components using annotation processing
If
you android app using Kotlin then there is no need for butter knife or
findviewbyid , we can simple use view id as view object as in example
below
Textview layout
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="55dp"
android:gravity="center"
android:text="Hello World!"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@color/colorPrimary"/>
In the above textview we set textview as id for the textview view we can change color , settext etc,.
//you can use this on any methods in lifecycle no need to instantiate the view
textview.setTextColor(Color.RED)
textview.text = "this text setted programatically"
Button with onclicklistener with button as id
Button Layout
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="55dp"/>
Kotlin code for onclick listener
button.setOnClickListener {
Toast.makeText(this@MainActivity, "${button.text} clicked ", Toast.LENGTH_LONG).show()
button.setBackgroundColor(Color.RED)
}
ImageView with set image programatically with imageview as id
Layout for ImageView
<ImageView
android:id="match_parent"
android:layout_width="291dp"
android:layout_height="99dp"/>
Kotlin code for change background color and to set image drawable
imageView.setImageDrawable(ContextCompat.getDrawable(this@MainActivity, android.R.drawable.ic_menu_share))
imageView.setBackgroundColor(Color.GREEN)
Final Look of Layout
<?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.mrkotlin.mrkotlincustomtoast.MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textview"
android:layout_width="279dp"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="Hello World!"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.043" />
<ImageView
android:contentDescription="@string/app_name"
android:id="@+id/imageView"
android:layout_width="291dp"
android:layout_height="99dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview"
app:layout_constraintVertical_bias="0.277"
app:srcCompat="@mipmap/ic_launcher_round" />
<Button
android:id="@+id/button"
android:layout_width="294dp"
android:layout_height="55dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</android.support.constraint.ConstraintLayout>
Final Look Kotlin Activity
package com.mrkotlin.mrkotlincustomtoast
import android.graphics.Color
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textview.setTextColor(Color.RED)
textview.text = "this text setted programatically"
imageView.setImageDrawable(ContextCompat.getDrawable(this@MainActivity, android.R.drawable.ic_menu_share))
imageView.setBackgroundColor(Color.GREEN)
button.setOnClickListener {
Toast.makeText(this@MainActivity, "${button.text} clicked ", Toast.LENGTH_LONG).show()
button.setBackgroundColor(Color.RED)
}
}
}
Comments
Post a Comment