Create Image radio button in Android (Kotlin)
Step 1(Import gradle)
Add Dependecy in Build.gradle and sync it.
implementation 'com.github.raheemadamboev:image-radio-button-android:1.0.4'
Step 2(Xml Code)
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<xyz.teamgravity.imageradiobutton.GravityRadioGroup
android:id="@+id/gravity_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<xyz.teamgravity.imageradiobutton.GravityImageRadioButton
android:id="@+id/Male"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20dp"
app:girbImage="@drawable/male"
app:girbPressedTextColor="@color/white"
app:girbText="Male"
app:girbUnpressedTextColor="?attr/colorPrimary" />
<xyz.teamgravity.imageradiobutton.GravityImageRadioButton
android:id="@+id/Female"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20dp"
app:girbImage="@drawable/Female"
app:girbPressedTextColor="@color/white"
app:girbText="Female"
app:girbUnpressedTextColor="?attr/colorPrimary" />
<xyz.teamgravity.imageradiobutton.GravityImageRadioButton
android:id="@+id/Other"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20dp"
app:girbImage="@drawable/other"
app:girbPressedTextColor="@color/white"
app:girbText="Other"
app:girbUnpressedTextColor="?attr/colorPrimary" />
</xyz.teamgravity.imageradiobutton.GravityRadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3(JavaCode)
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import xyz.teamgravity.imageradiobutton.GravityImageRadioButton
import xyz.teamgravity.imageradiobutton.GravityRadioGroup
import xyz.teamgravity.imageradiobuttondemo.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.gravityGroup.setOnCheckedChangeListener { _, radioButton, _, _ ->
Toast.makeText(this@MainActivity, (radioButton as GravityImageRadioButton)
.text(), Toast.LENGTH_SHORT).show()
}
}
}