FormBuilder


Source link: https://github.com/dariopellegrini/FormBuilder

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' 
}

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]

Resources

Custom view to pick up a value from a given range using click button or swiping.

ImageMap is a small android library for displaying multiple image on a view using an adapter, you can then retrieve tap event on defined image.

SpannableBar is a Grid-style spannable bar, that is useful when you need a way to span a bar over columns. The view allows you to set the starting column, the span, the number of columns, and more.

Custom EditText for Android to enter credit card numbers.

Provide Funky cuts on image (The Perfect Header for Profile UI)

Library manages lifecycle of Activities and Fragments, their Presenters and ViewStates.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes