Last active
December 11, 2019 08:35
-
-
Save avisper/de198b08b9c2e7632c5bf4fb69da480b to your computer and use it in GitHub Desktop.
MultiRowsRadioGroup is a RadioGroups container with different columns or rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<your.project.package.MultiRowsRadioGroup xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/multiRowsRadioGroup_container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<RadioGroup | |
android:id="@+id/radioGroup1" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="AAA" /> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="AAA" /> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="AAA" /> | |
</RadioGroup> | |
<RadioGroup | |
android:id="@+id/radioGroup2" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="BBB" /> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="BBB" /> | |
</RadioGroup> | |
<RadioGroup | |
android:id="@+id/radioGroup3" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="CCC" /> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="CCC" /> | |
</RadioGroup> | |
</your.project.package.MultiRowsRadioGroup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package your.project.package | |
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.LinearLayout | |
import android.widget.RadioGroup | |
/** | |
* Created by Avishay.Peretz on 05/12/2019. | |
*/ | |
class MultiRowsRadioGroup @JvmOverloads constructor( | |
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 | |
) : LinearLayout(context, attrs, defStyleAttr) { | |
private var isChecking: Boolean = false | |
private var mCheckedId: Int = -1 | |
private lateinit var radioGroupCheckedChangedListener: RadioGroup.OnCheckedChangeListener | |
private var checkedChangedListener: OnCheckedChangeListener? = null | |
init { | |
initRadioGroupListener() | |
} | |
override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) { | |
super.addView(child, index, params) | |
setRadioGroupListener(child) | |
} | |
private fun setRadioGroupListener(view: View?) { | |
if (view != null && view is RadioGroup) { | |
view.setOnCheckedChangeListener(radioGroupCheckedChangedListener) | |
} | |
} | |
private fun initRadioGroupListener() { | |
radioGroupCheckedChangedListener = RadioGroup.OnCheckedChangeListener { group, checkedId -> | |
if (checkedId != -1 && !isChecking) { | |
isChecking = true | |
val size = childCount | |
for (i in 0 until size) { | |
val view = getChildAt(i) | |
if (view is RadioGroup) { | |
if (view != group) { | |
view.clearCheck() | |
} | |
} | |
} | |
mCheckedId = checkedId | |
checkedChangedListener?.onCheckedChanged(group, checkedId) | |
isChecking = false | |
} | |
} | |
} | |
fun setOnCheckedChangeListener(onCheckedChangeListener: OnCheckedChangeListener?) { | |
checkedChangedListener = onCheckedChangeListener | |
} | |
} | |
interface OnCheckedChangeListener { | |
fun onCheckedChanged(group: RadioGroup, checkedId: Int) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package your.project.package | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.RadioGroup | |
import android.widget.Toast | |
class RadioGroupActivity : AppCompatActivity() { | |
private lateinit var multiRowsRadioGroup: MultiRowsRadioGroup | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_radio_group) | |
multiRowsRadioGroup = findViewById(R.id.multiRowsRadioGroup_container) | |
multiRowsRadioGroup.setOnCheckedChangeListener(object : OnCheckedChangeListener { | |
override fun onCheckedChanged(group: RadioGroup, checkedId: Int) { | |
Toast.makeText(applicationContext, "groupId = ${group.id}\ncheckedId = $checkedId", Toast.LENGTH_SHORT).show() | |
} | |
}) | |
} | |
override fun onDestroy() { | |
multiRowsRadioGroup.setOnCheckedChangeListener(null) | |
super.onDestroy() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#Android #Kotlin #RadioGroup