Add maven jitpack.io
and dependencies
in build.gradle (Project)
:
// build.gradle project
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
// build.gradle app/module
dependencies {
...
implementation 'com.github.gzeinnumer:DynamicRadioButton:version'
}
- Widget on
xml
<com.gzeinnumer.dr.DynamicRadioButton
android:id="@+id/dr"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
if you want to custom your RadioButton
use app:style="@style/radioButtonStyle"
on view, and make style on your res->value->style.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="def_radioButtonStyle" parent="Base.Theme.AppCompat">
<item name="android:textColor">#FFE500</item>
</style>
</resources>
<com.gzeinnumer.dr.DynamicRadioButton
...
app:orientationRadioButton="horizontal"
app:style="@style/def_radioButtonStyle"/>
- Content Item there is 2 type list that you can sent to this
RadioButton
.
Type 1
DynamicRadioButton dynamicRadioButton = findViewById(R.id.dr);
ArrayList<String> listString = new ArrayList<String>();
listString.add("Satu");
listString.add("Dua");
listString.add("Tiga");
listString.add("Empat");
dynamicRadioButton.setItemList(listString).setOnCheckedChangeListener(new DynamicRadioButton.OnCheckedChangeListener<String>() {
@Override
public void onCheckedChanged(String item) {
Log.d(TAG, "onCheckedChanged: "+item);
}
});
Type 2 for this type you should override function toString()
in your model pojo
public class ExampleModel {
private int id;
private String name;
private String address;
//constructor
//getter
//setter
@Override
public String toString() {
return "ExampleModel{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
Use your own model. And dont forget to declare your model pojo
in onCallBack
. Example DynamicRadioButton.OnCheckedChangeListener<ExampleModel>
DynamicRadioButton dynamicRadioButton = findViewById(R.id.dr);
ArrayList<ExampleModel> listObject = new ArrayList<>();
listObject.add(new ExampleModel(1, "Zein", "Balbar"));
listObject.add(new ExampleModel(2, "Zein2", "Balbar2"));
listObject.add(new ExampleModel(3, "Zein3", "Balbar3"));
listObject.add(new ExampleModel(4, "Zein4", "Balbar4"));
dynamicRadioButton.setItemList(listObject).setOnCheckedChangeListener(new DynamicRadioButton.OnCheckedChangeListener<ExampleModel>() {
@Override
public void onCheckedChanged(ExampleModel item) {
Log.d(TAG, "onCheckedChanged: "+item.toString());
}
});
Set setSelectedItem(index)
use before setItemList(list)
.
dynamicRadioButton.setSelectedItem(1).setItemList(list). ...
Preview :
Preview Single Object |
Preview Model Pojo |
Output data Single Object |
Output data Model Pojo |
FullCode MainActivity & ExampleModel & XML
- 1.0.0
- First Release
- 2.0.0
- Support SDK 16
- 2.0.2
- orientation to orientationRadioButton
- 2.1.0
- setSelectedItem()
You can sent your constibution to branch
open-pull
.
Copyright 2020 M. Fadli Zein