Skip to content

anggit97/FormBuilder

Repository files navigation

FORM BUILDER

Never this easy to easy to create form in Android

Build Status

this library to create form in andorid

INSTALLATION

Add this in your project's build.gradle file:

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

Add this in your app's build.gradle file:

dependencies {
    implementation 'com.github.anggit97:FormBuilder:0.1.0'
}

Usage

  1. Create parent layout to accomodate views you want to show
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"/>
  1. initialize FormBuilder Class in your actvity
private val formBuilder: FormBuilder by lazy {
    FormBuilder(this, rootLayout)
}
  1. initialize list of form object
private val formElementMutableList = mutableListOf<FormObject>()
  1. add form element to list of form object
formElementMutableList.add(
    FormElement().setTitle("Name").setType(FormElement.Type.TEXT).setHint("Enter your name")
)

formElementMutableList.add(
    FormElement().setTitle("Password").setType(FormElement.Type.PASSWORD).setHint("Enter your password")
)

formElementMutableList.add(
    FormElement().setTitle("Email").setType(FormElement.Type.EMAIL).setHint("Enter your email")
)

formElementMutableList.add(
    FormButton().setBackgroundColor(R.color.colorPrimary).setTitle("Enter")
)
  1. add form element to FormBuilder build method
formBuilder.build(formElementMutableList)
  1. Thats it, here full code
class MainActivity : AppCompatActivity() {

    private val formBuilder: FormBuilder by lazy {
        FormBuilder(this, rootLayout)
    }

    private val formElementMutableList = mutableListOf<FormObject>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        formElementMutableList.add(
            FormElement().setTitle("Name").setType(FormElement.Type.TEXT).setHint("Enter your name")
        )

        formElementMutableList.add(
            FormElement().setTitle("Password").setType(FormElement.Type.PASSWORD).setHint("Enter your password")
        )

        formElementMutableList.add(
            FormElement().setTitle("Email").setType(FormElement.Type.EMAIL).setHint("Enter your email")
        )

        formElementMutableList.add(
            FormButton().setBackgroundColor(R.color.colorPrimary).setTitle("Enter")
        )

        formBuilder.build(formElementMutableList)
    }
}
  1. result Result 1

Retrieve Values

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

val textValue = formBuilder.formMap.get("tagKey").getValue()

Validation

To make a validation simply call

val 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(FormElement()
      .setTag("tagKey")
      .setHint("Text")
      .setType(FormElement.Type.TEXT)
      .setErrorMessage("You can learn from this error"))

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

val formElement = FormElement().setTag("view").setHint("view").setType(FormElement.Type.TEXTVIEW))
formElement.setFormValidation(object : FormValidation() {
				  
				  override validate(): Boolean {
				      return formElement.getValue().length() > 5;
				  }
			      }
).setErrorMessage("Too short")

Features!

  • Support create field like (TEXT, TEXTVIEW, EMAIL, PASSWORD, PHONE, NUMBER, URL, SPINNER,ZIP,SELECTION, MULTIPLE_SELECTION, DATE, TIME), Button, and Header
  • Support Field Validation

Todos

  • Add styling on form element

License

MIT

Free Software, Hell Yeah!