Android Library MultiChoiceForm
Easy implementation of a multi selection form element. It runs on API level 16 and upwards.
Here's a list of the MultiChoiceForm library core features as of the current version.
- Include any amount of
MCFStep
s in your layout. - Three types of fields: single selection, text input and date.
- Support for dependent fields.
- Supports searchable single select fields.
- Supports regex validation for text input fields.
- Change any
MCFStep
's options data in runtime. - Accepts
MCFStep
data asArrayList<String>
orString[]
. - Set required fields (with validation animations).
- Customize validation animation.
- Customize activity's toolbar.
- Customize options empty view text.
- Customize
MCFStepView
colors and drawable.
Check out the videos of our sample app:
Using Gradle, import the dependency into your project, add this into your project's build.gradle file:
allprojects {
repositories {
...
maven {
url 'https://dl.bintray.com/hypernovalabs/maven'
}
}
}
Then, in your app's build.gradle file:
implementation 'com.hypernovalabs:multichoiceform:1.10.0@aar'
You can access the full javadoc here.
Here is a simple implementation of the MultiChoiceForm library:
- Add a
MCFStepView
into your layout as follows:
<com.hypernovalabs.multichoiceform.view.MCFStepView
android:id="@+id/form_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mcf_title="Test" />
- Define your
MCFStep
s in your activity class. The parameters are as follows:
- data - your ArrayList containing the MCFStep's options
- view - your MCFStepView
- required (optional) - default is false
Example:
ArrayList<String> data = ... //your data
MCFSingleSelectStep step = new MCFSingleSelectStep(data, (MCFStepView) findViewById(R.id.form_test), true);
- Create an
ArrayList<MCFStep>
and add all of yourMCFStep
s into it:
mSteps = new ArrayList<>();
mSteps.add(step);
mSteps.add(step2);
mSteps.add(step3);
...
- Instantiate a
MultiChoiceForm
helper using the class'Builder
:
MultiChoiceForm.Builder builder = new MultiChoiceForm.Builder(mContext, mSteps);
mForm = builder.build(); //build your instance
mForm.setupForm(); //required in order to enable the MCFSteps
- Add this in your
onActivityResult
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Obligatory call
mForm.handleActivityResult(requestCode, resultCode, data);
}
- Add the definition of
OptionsActivity
andTextInputActivity
in yourAndroidManifest.xml
:
<activity android:name="com.hypernovalabs.multichoiceform.OptionsActivity"
android:screenOrientation="portrait"/>
<activity android:name="com.hypernovalabs.multichoiceform.TextInputActivity"
android:screenOrientation="portrait"/>
Optionally, to validate your data, call mForm.validate();
in your "Submit" button,
if it returns true, all your required fields are filled.
That's it, you are all set!
As of the current version, these are the types of MCFStep
s you can use (for more information, check
the javadoc:
Provides a list of options and returns only one selected option. Example:
ArrayList<String> data = ... //your data
MCFSingleSelectStep step = new MCFSingleSelectStep(data, (MCFStepView) findViewById(R.id.form_test), true);
step.setSearchable(true); //to enable the SearchView
Provides a single text input field with regex validation. Example:
MCFTextInputStep textInputStep = new MCFTextInputStep(
(MCFStepView) findViewById(R.id.form_test),
true,
new Regex("^[a-zA-Z0-9]*$", "Only alphanumeric characters"));
textInputStep.setExplanatoryText("Please enter your name"); //text below the EditText
textInputStep.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); //treat it as the regular InputType
Provides an AlertDialog
containing a DatePicker
to select a date.
Date format is customizable, along with min and max date, and the dialog buttons.
MCFDateStep dateStep = new MCFDateStep((MCFStepView) findViewById(R.id.form_date), true);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US)
dateStep.setDateFormat(sdf); //optional
Works as a visual step only, the onClickListener
should be manually set on the MCFStepView
.
This step allows you to add a required step to MultiChoiceForm.validate()
without needing to use it
as a MCFSingleSelectStep
MCFButtonStep step2 = new MCFButtonStep((MCFStepView) findViewById(R.id.form_test), true);
step2.getView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do stuff
}
});
This is all optional, adds increased customization to your form.
Customize the OptionsActivity
and other MultiChoiceForm
settings:
MultiChoiceForm.Builder builder = new MultiChoiceForm.Builder(mContext, mSteps)
.setToolbarColors(
ContextCompat.getColor(mContext, R.color.toolbar),
ContextCompat.getColor(mContext, R.color.toolbar_text)) //optional
.setRequiredText("Fill out everything, please") //optional
.setValidationColor(ContextCompat.getColor(mContext, R.color.bluet)) //optional
.setValidationAnimation(ValidateAnimation.SHAKE_HORIZONTAL) //optional
.setValidationDuration(ValidateAnimation.SHORT) //optional
.setEmptyViewTexts("Attention!", "Fill out all of the required fields, please") //optional
.setSearchViewHint("Search here...") //optional
.setToolbarIconTint(Color.BLACK) //optional
.setHasAutoFocus(true); //optional
mForm = builder.build(); //build your instance
You can fully customize the view in XML:
<com.hypernovalabs.multichoiceform.view.MCFStepView
android:id="@+id/form_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mcf_arrowDrawable="@drawable/ic_arrow"
app:mcf_enabled="false"
app:mcf_titleColor="@color/blue"
app:mcf_separatorColor="@color/gray"
app:mcf_selectionColor="@color/blue"
app:mcf_title="Empty" />
You can also customize it in your Java class:
MCFStepView view = (MCFStepView) findViewById(R.id.form_test);
view.setTitle("Title");
view.setTitleColor(Color.BLACK);
view.setSelection("Selection");
view.setSelectionColor(Color.RED);
view.setSeparatorColor(ContextCompat.getColor(mContext, R.color.red));
view.setArrowImageView(ContextCompat.getDrawable(mContext, R.drawable.ic_action_arrow));
view.setEnabled(false);
MCFSingleSelectStep formStep = new MCFSingleSelectStep(data, view, true);
If you need to set any dependent step (the data depends on the selection of a previous step),
you can change the MCFStep
data at runtime on your onActivityResult
method.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Obligatory call
mForm.handleActivityResult(requestCode, resultCode, data);
//In case a step depends on another step
if (resultCode == RESULT_OK && requestCode == MultiChoiceForm.REQUEST_SELECTION) {
int tag = data.getIntExtra(MCFConfig.EXTRA_TAG_KEY, 0); //gets the tag of the selected MCFStep
MCFStep currentStep = MCFStep.getStepFromTag(mForm.getSteps(), id);
//If you need to handle several dependent steps
switch (id) {
case R.id.form_test3:
if (currentStep != null) {
mDependentStep.setData(getDataFromSelection(currentStep.getView().getSelection(), 5));
// Removes any previous selection from both of the dependent MCFSteps fields
// and enables them
mDependentStep.getView().deselect(true); // direct child, enables it
mDependentStep2.getView().deselect(); // only deselects, still has a parent deselected
}
break;
case R.id.form_dependent:
if (currentStep != null) {
mDependentStep2.setData(getDataFromSelection(currentStep.getView().getSelection(), 5));
//Removes any previous selection and enables it.
mDependentStep2.getView().deselect(true);
}
break;
}
}
}
MCFButtonStep
.
ValidateAnimation
class.@IntDef
magic constants forMCFStepType
,ValidationAnim
andDuration
instead of Enum classes.
- Enum classes.
- Deprecated methods.
getDisabledTitleColor(int)
.setDisabledTitleColor(int)
.getDisabledSelectionColor(int)
.setDisabledSelectionColor(int)
.
getDisabledTextColor(int)
.setDisabledTextColor(int)
.
MCFStep.tag
parameter, which has a default ofMCFStep.getView().getId()
. You can now use this parameter instead of the view id to identify theMCFStep
. This is preferable when working with dynamic lists ofMCFStep
where the view id is the same for all of theMCFSteps
, such asViewHolder
s.
- Method to retrieve a certain
MCFStep
from the list ofMCFStep
s, it is now based on itstag
parameter.
getStepFromId(@IdRes int)
.
MCFStep
constructors.
- Text size parameters.
- Disabled color parameters.
- Adaptability of the step views to adjust their size according to the text length.
- Reduced the text size by
2dp
.
- Crash when there was not any
Regex
specified.
- Support for
InputType
password variants forMCFTextInputStep
hasAutoFocus
parameter ofMultiChoiceForm
to control whether theEditText
will have focus onTextInputActivity
creation.
MCFTextInputStep
.
MultiChoiceFormConfig
toMCFOptionsConfig
.
- Implementation of
setTitleMaxLines
andsetSelectionMaxLines
- Performance on the step views when the texts are too long.
- Builder method to set the
SearchView
's hint. - Builder method to set the
SearchView
's icon tint color.
- Android Oreo issue with the SearchView where it would not collapse the ActionView.
- Definition of
setSelection(@StringRes int)
on anyMCFStep
.
SearchView
onOptionsActivity
based on the parameterisSearchable
- Toast appearing multiple times.
- Validation error causing the app to crash.
- Space next to the
MCFStepView
value when the step is disabled.
- Arrow image dimensions
- Support multilines on title and selection
- The title is shown completely in the number of lines established if nothing has been selected
- Default selectableItemBackground
- Title Toast functionality.
getSelection()
returning null issue
- Support of
MCFStep
options data asString[]
.
ScrollView
issue
getSteps()
method fromMultiChoiceForm
- Bug with
MCFDateStep
- Resource prefixes.
- OptionsActivity EXTRA constants.
MCFStep
and derived classes prefixes.- MCFStepView's
enable(boolean)
tosetEnabled(boolean)
.
MultiChoiceForm.Builder.setSteps(ArrayList<MCFStep>)
onLongItemClickListener
to clear the selection.
- General improvements on the FormSteps.
FormDateStep
andMCFSingleSelectStep
.- This CHANGELOG section.
FormDateStep
SimpleDateFormat.