An Android library to build form and form validations easily.

Overview

FormBuilder

An Android library to build form and form validations easily.

Example

COMING SOON

Requirements

Android 4.3+

Installation

Add edit your build.gradle file

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Then add as dependency to yout app/build.gradle

dependencies {
    ...
    compile 'com.github.dariopellegrini:FormBuilder:v0.9.2'
}

Usage

This library let you to create forms and add them in a LinearLayout. If you want to scroll this linear layout remember to add it inside a ScrollView. Here is an example:

LinearLayout mLinearLayout;
FormBuilder formBuilder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setContentView(R.layout.activity_main);
        mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        formBuilder = new FormBuilder(this, mLinearLayout);
        List<FormObject> formObjects = new ArrayList<FormObject>();
        
        formObjects.add(new FormElement()
            .setTag("text") // Tag is necessary in order to retrieve values
            .setHint("text") // Hint is the placeholder of the generated EditText
            .setType(FormElement.Type.TEXT) // Type of form
            .setEnabled(true) // Enable or not the EditText (default true)
            .setRequired(true) // For validation purpose (default false)
            );
        formObjects.add(new FormElement().setTag("email").setHint("email").setType(FormElement.Type.EMAIL));
        formObjects.add(new FormElement().setTag("phone").setHint("phone").setType(FormElement.Type.PHONE));
        
        formObjects.add(new FormButton()
                .setTitle("Go!")
                .setBackgroundColor(Color.RED)
                .setTextColor(Color.WHITE)
                .setRunnable(new Runnable() {
                    @Override
                    public void run() {
                        boolean isValid = formBuilder.validate();
                        Log.i("Forms", formBuilder.formMap.toString());
                    }
                })
        );

        formBuilder.build(formObjects);
    }

Details

It's possible to add different type of form.

FormBuilder formBuilder = new FormBuilder(this, mLinearLayout);
List<FormObject> formObjects = new ArrayList<FormObject>();

// Header
formObjects.add(new FormHeader().setTitle("Hello"));

// Simple text
formObjects.add(new FormElement().setTag("text").setHint("text").setType(FormElement.Type.TEXT));

// Simple text without placeholder animation (for long text for example)
formObjects.add(new FormElement().setTag("view").setHint("view").setType(FormElement.Type.TEXTVIEW));

// E-mail
formObjects.add(new FormElement().setTag("email").setHint("email").setType(FormElement.Type.EMAIL));

// Phone
formObjects.add(new FormElement().setTag("phone").setHint("phone").setType(FormElement.Type.PHONE));

// Number
formObjects.add(new FormElement().setTag("number").setHint("number").setType(FormElement.Type.NUMBER));

// Password
formObjects.add(new FormElement().setTag("password").setHint("password").setType(FormElement.Type.PASSWORD));

// Postal code
formObjects.add(new FormElement().setTag("zip").setHint("zip").setType(FormElement.Type.ZIP));

// Date type: it's possible to set date format (default is "ddMMyyyy")
formObjects.add(new FormElement().setTag("date").setHint("date").setType(FormElement.Type.DATE).setDateFormat("dd-MM-yyyy"));

// Time type: it's possible to set time format (default is "HH:mm:ss")
formObjects.add(new FormElement().setTag("time").setHint("time").setType(FormElement.Type.TIME).setTimeFormat("HH:mm"));

// Single selection
List<String> arrayList = new ArrayList<String>();
        arrayList.add("hello");
        arrayList.add("hi");
        arrayList.add("goodmorning");
formObjects.add(new FormElement().setTag("single").setHint("single").setType(FormElement.Type.SELECTION).setOptions(arrayList));

// Multiple selection
List<String> arrayList2 = new ArrayList<String>();
        arrayList2.add("hello2");
        arrayList2.add("hi2");
        arrayList2.add("goodmorning2");
formObjects.add(new FormElement().setTag("multiple").setHint("multiple").setType(FormElement.Type.MULTIPLE_SELECTION).setOptions(arrayList2));

formBuilder.build(formObjects);

// Button: it's possible to set background color, text color and a runnable that will be executed once the button is pressed.
formObjects.add(new FormButton()
                .setTitle("Go!")
                .setBackgroundColor(Color.RED)
                .setTextColor(Color.WHITE)
                .setParams(layoutParams)
                .setRunnable(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("Forms", formBuilder.formMap.toString());
                    }
                })
        );

Retrieve values

Values inserted are saved inside a map of the object FormBuilder, using tags as key.

String textValue = formBuilder.formMap.get("text").getValue();

Layout

Every object inside a form has a params attribute which represent LayoutParams of the generated form.

// This will create a form of type TEXTVIEW with a height of 320.
final FormElement formElement = new FormElement().setTag("view")
      .setHint("view")
      .setType(FormElement.Type.TEXTVIEW)
      .setParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 320));

Validation

To make a validation simply call

boolean isValid = formBuilder.validate();

This will show an error on all forms that has been set as required.

It's possible to change error content on each form element.

formObjects.add(new FormElement()
      .setTag("text")
      .setHint("text")
      .setType(FormElement.Type.TEXT)
      .setErrorMessage("You can learn from this error"));

Every form element can accept a customized code for its validation.

final FormElement formElement = new FormElement().setTag("view").setHint("view").setType(FormElement.Type.TEXTVIEW));

        formElement.setFormValidation(new FormValidation() {
                                          @Override
                                          public boolean validate() {
                                              return formElement.getValue().length() > 5;
                                          }
                                      }
        ).setErrorMessage("Too short");

TODO

  • Add different types of errors.
  • Add SPINNER type.
  • Testing.

Author

Dario Pellegrini, [email protected]

You might also like...
Android StackBlur is a library that can perform a blurry effect on a Bitmap based on a gradient or radius, and return the result. The library is based on the code of Mario Klingemann.
Android StackBlur is a library that can perform a blurry effect on a Bitmap based on a gradient or radius, and return the result. The library is based on the code of Mario Klingemann.

Android StackBlur Android StackBlur is a library that can perform a blurry effect on a Bitmap based on a gradient or radius, and return the result. Th

Postman is a reactive One-tap SMS verification library. This library allows the usage of RxJava with The SMS User Consent API
Postman is a reactive One-tap SMS verification library. This library allows the usage of RxJava with The SMS User Consent API

What is Postman? Postman is a reactive One-tap SMS verification library. This library allows the usage of RxJava with The SMS User Consent API Usage P

AppIntroAnimation is a set of code snippets to make cool intro screen for your app with special Image Translation and Transformation animation effects. It is very easy to use and customize without adding third party library integrations. FilePicker is a small and fast file selector library that is constantly evolving with the goal of rapid integration, high customization, and configurability~
FilePicker is a small and fast file selector library that is constantly evolving with the goal of rapid integration, high customization, and configurability~

Android File Picker 🛩️ 中文简体 Well, it doesn't have a name like Rocky, Cosmos or Fish. Android File Picker, like its name, is a local file selector fra

Android Country Picker is a Kotlin-first, flexible and powerful Android library that allows to integrate Country Picker with just a few lines.
Android Country Picker is a Kotlin-first, flexible and powerful Android library that allows to integrate Country Picker with just a few lines.

1. Add dependency dependencies { implementation 'com.hbb20:android-country-picker:X.Y.Z' } For latest version, 2. Decide your use-case

Android library. Flexible components for chat UI implementation with flexible possibilities for styling, customizing and data management.  Made by Stfalcon
Android library. Flexible components for chat UI implementation with flexible possibilities for styling, customizing and data management. Made by Stfalcon

ChatKit for Android ChatKit is a library designed to simplify the development of UI for such a trivial task as chat. It has flexible possibilities for

Android library to animate Floating Action Button to Bottom Sheet Dialog and vice-versa
Android library to animate Floating Action Button to Bottom Sheet Dialog and vice-versa

FabulousFilter Show some ❤️ and star the repo to support the project This library is the implementation of filter-concept posted on MaterialUp.com. It

FPSAnimator is very easy animation library for Android TextureView and SurfaceView.
FPSAnimator is very easy animation library for Android TextureView and SurfaceView.

FPSAnimator A simple but powerful Tween / SpriteSheet / ParabolicMotion / animation library for Android TextureView and SurfaceView. Features The cont

Continuous speech recognition library for Android with options to use GoogleVoiceIme dialog and offline mode.

Android Speech Recognition This library lets you perform continuous voice recognition in your android app with options to either use Google Voice Ime

Comments
  • Problems with Single Selection

    Problems with Single Selection

    When using single selection mode, if you try to change the value by clicking on the edittext it simply adds the new selection to the EditText making it act like Multiple Selection

    We can fix it by replacing the pickDialog() Method with the following

       private void pickDialog(final EditText selectedEditText, final FormElement selectedFormElement) {
            this.selectedEditText = selectedEditText;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("");
            builder.setSingleChoiceItems(selectedFormElement.getOptions().toArray(new CharSequence[selectedFormElement.getOptions().size()]), selectedFormElement.getCheckedValue(),     null);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                    int selectedPosition = ((AlertDialog) DialogInterface).getListView().getCheckedItemPosition();
                    String selectedElement = (selectedFormElement.getOptions().get(selectedPosition));
                    selectedFormElement.setValue(selectedElement);
                    selectedEditText.setText(selectedElement);
                }
            });
    
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            builder.show();
        }
    
    opened by idragon81 3
  • Required validation of SELECTION and MULTIPLE_SELECTION fails

    Required validation of SELECTION and MULTIPLE_SELECTION fails

    If you set a FormElement of type SELECTION or MULTIPLE_SELECTION as required, it will always fail validation because FormBuilder.validate() checks the value field on the FormElement but not the optionsSelected field that is set for these types.

    if (element.getRequired()) {
        // Should check optionsSelected if element.type == SELECTION or MULTIPLE_SELECTION
        if (element.getValue() == null || element.getValue().length() == 0) {
            isValid = false;
            if (view instanceof EditText) {
                ((EditText) view).setError(element.getErrorMessageOrDefault());
            }
        }
    }
    
    opened by danpalmer101 1
  • Errors do not appear for some FormElement types

    Errors do not appear for some FormElement types

    If the FormElement type is set to any of the following, then the error message does not show if validation fails:

    • SELECTION
    • MULTIPLE_SELECTION
    • DATE
    • TIME

    I believe this is because the FormBuilder.buildElement() method adds the wrong EditText object to the viewMap:

                    textInputLayout = new TextInputLayout(context);
                    final EditText selectionEditText = new EditText(context);
                    ....
                    // Wrong object added here!
                    viewMap.put(formElement.getTagOrToString(), editText);
                    addViewToView(textInputLayout, selectionEditText);
    

    The selectionEditText object is added to the screen, but the editText object is added to the map. So when FormBuilder.validate() is called then the error is added to editText instead of selectionEditText.

    opened by danpalmer101 0
Releases(v0.9.2)
Owner
Dario Pellegrini
Computer engineer from Italy with a complex and pathological addiction to cinema and music
Dario Pellegrini
[] An Android library which allows developers to easily add animations to ListView items

DEPRECATED ListViewAnimations is deprecated in favor of new RecyclerView solutions. No new development will be taking place, but the existing versions

Niek Haarman 5.6k Dec 30, 2022
EtsyBlur is an Android library that allows developers to easily add a glass-like blur effect implemented in the Etsy app.

EtsyBlur EtsyBlur is an Android library that allows developers to easily add a glass-like blur effect implemented in the past Etsy app. Try out the sa

Manabu S. 755 Dec 29, 2022
Android Library to create Lottie animation view dialog easily with a lot of customization

LottieDialog Android Library to create Lottie animation view dialog easily with a lot of customization Why you should use Lottie Dialog You have no li

Amr Hesham 39 Oct 7, 2022
Android Library to create Lottie animation view dialog easily with a lot of customization

Android Library to create Lottie animation view dialog easily with a lot of customization

Amr Hesham 39 Oct 7, 2022
Android Annotation Processor library to generate adapter class easily from your model with a lot of customization

Android Annotation Processing Library to generate your adapters only with Annotations on your model, support working with Kapt and KSP Processors

Amr Hesham 63 Nov 24, 2022
💠Metaphor is the library to easily add Material Motion animations

Metaphor Metaphor is the library to easily add Material Motion animations. Who's using Metaphor? ?? Check out who's using Metaphor Include in your pro

Ranbir Singh 132 Dec 25, 2022
Group of libraries to help you build better animations with Jetpack Compose

Group of libraries to help you build better animations with Jetpack Compose

null 36 May 12, 2022
[] Easily have blurred and transparent background effect on your Android views.

##[DEPRECATED] BlurBehind Easily have blurred and transparent background effect on your Android views. Before API level 14 there was a Window flag cal

Gokberk Ergun 516 Nov 25, 2022
You can easily access the top of the screen in Android. Like a iPhone 6 & 6 Plus.

Reachability on Android Easy access on top. Like a iPhone 6 & 6 Plus. demo apk Usage Add dependencies compile 'com.github.sakebook:Reachability:0.2.0@

sakebook 258 Nov 12, 2022
Easily add slide to dismiss functionality to an Activity

Slidr Easily add slide-to-dismiss functionality to your Activity by calling Slidr.attach(this) in your onCreate(..) method. Usage An example usage: pu

Drew Heavner 2.7k Jan 6, 2023