Skip to content

Simple Form is an Android library purely written in Kotlin. This library will help you create forms at ease. Try it in your next project. 😉

License

Notifications You must be signed in to change notification settings

lspradeep/simple-form

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-form

Add this in your project's build.gradle (project level)
	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
Add this in your project's build.gradle (app level)
	dependencies {
	        implementation 'com.github.lspradeep:simple-form:v1.0.1'
	}

Table of content

1. How to create a simple form

2. How to create a sectioned form

3. How to create a sectioned form and show only one section at a time




1. How to create a simple form

Alt Text Alt Text

Step 1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.pradeep.form.simple_form.SimpleFormView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Step 2: (just describe your form item)

import com.pradeep.form.simple_form.form_items.FormTypes
import com.pradeep.form.simple_form.form_items.NumberInputType
import com.pradeep.form.simple_form.form_items.SingleLineTextType
import com.pradeep.form.simple_form.model.Form

    fun getFormData(): List<Form> {
        val forms = mutableListOf<Form>()
        forms.add(
            Form(
                formType = FormTypes.SINGLE_LINE_TEXT,
                question = "Name",
                hint = "please enter your name",
                singleLineTextType = SingleLineTextType.TEXT,
                errorMessage = "Please provide an answer"
            )
        )
        forms.add(
            Form(
                formType = FormTypes.SINGLE_LINE_TEXT,
                question = "Email",
                hint = "please enter your Email address",
                singleLineTextType = SingleLineTextType.EMAIL_ADDRESS,
                errorMessage = "Please provide a valid email address"
            )
        )
        forms.add(
            Form(
                formType = FormTypes.MULTI_LINE_TEXT,
                question = "Bio",
                description = "tell us about you",
                hint = "I'm from India. I love ice cream.",
                errorMessage = "Please provide an answer"
            )
        )
        forms.add(
            Form(
                formType = FormTypes.SINGLE_CHOICE,
                question = "Gender",
                hint = "please choose your gender",
                choices = listOf(
                    "Male",
                    "Female",
                    "Others"
                ),
                errorMessage = "Please select an answer"
            )
        )

        forms.add(
            Form(
                formType = FormTypes.NUMBER,
                question = "Do you earn or still studying",
                description = "if yes, care to tell us how much you earn per year?",
                numberInputType = NumberInputType.NUMBER,
                errorMessage = "Please provide an answer"
            )
        )
        forms.add(
            Form(
                formType = FormTypes.NUMBER,
                question = "What's  your score in college",
                numberInputType = NumberInputType.DECIMAL_NUMBER,
                errorMessage = "Please provide an answer"
            )
        )
        forms.add(
            Form(
                formType = FormTypes.NUMBER,
                question = "Phone number",
                hint = "please provide your phone number",
                numberInputType = NumberInputType.PHONE_NUMBER,
                errorMessage = "Please provide a valid phone number"
            )
        )
        forms.add(
            Form(
                formType = FormTypes.MULTI_CHOICE,
                question = "Your favourite TV shows",
                description = "you can pick more than one",
                hint = "hint 3",
                choices = listOf(
                    "friends",
                    "tom and jerry",
                    "pokemon",
                    "rick and morty"
                ),
                errorMessage = "Please choose an answer"
            )
        )
        return forms
    }

Step 3:

binding.simpleForm.setData(getFormData(), callback = this)

Step 4:

implement FormSubmitCallback in your activity and override the following function 

    override fun onFormSubmitted(forms: List<Form>) {
         //do something with the result
    }



2. How to create a sectioned form

Alt Text Alt Text Alt Text

Step 1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.pradeep.form.simple_form.SimpleFormView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Step 2: (just describe your form item)

import com.pradeep.form.simple_form.form_items.FormTypes
import com.pradeep.form.simple_form.form_items.NumberInputType
import com.pradeep.form.simple_form.form_items.SingleLineTextType
import com.pradeep.form.simple_form.model.Form

fun getSection1FormData(): List<Form> {
      val forms = mutableListOf<Form>()
      forms.add(
            Form(
                formType = FormTypes.SINGLE_LINE_TEXT,
                question = "Name",
                hint = "please enter your name",
                singleLineTextType = SingleLineTextType.TEXT,
                errorMessage = "Please provide an answer"
            )
        )
        forms.add(
            Form(
                formType = FormTypes.SINGLE_LINE_TEXT,
                question = "Email",
                hint = "please enter your Email address",
                singleLineTextType = SingleLineTextType.EMAIL_ADDRESS,
                errorMessage = "Please provide a valid email address"
            )
        )
	forms.add(
            Form(
                formType = FormTypes.NUMBER,
                question = "Phone number",
                hint = "please provide your phone number",
                numberInputType = NumberInputType.PHONE_NUMBER,
                errorMessage = "Please provide a valid phone number"
            )
        )
        return forms
    }

    fun getSection2FormData(): List<Form> {
        val forms = mutableListOf<Form>()
        forms.add(
            Form(
                formType = FormTypes.SINGLE_LINE_TEXT,
                question = "College Name",
                hint = "please enter your name",
                singleLineTextType = SingleLineTextType.TEXT,
                errorMessage = "Please provide an answer"
            )
        )
        forms.add(
            Form(
                formType = FormTypes.NUMBER,
                question = "Your score in college",
                hint = "example: 7.6",
                numberInputType = NumberInputType.NUMBER,
                errorMessage = "Please provide an answer"
            )
        )
        return forms
    }

Step 3:

   private var sectionedForms = mutableMapOf<String, List<Form>>()
   sectionedForms["Personal Information"] = getSection1FormData()
   sectionedForms["Education"] = getSection2FormData()

   binding.simpleForm.setData(sectionedForms, callback = this)

Step 4:

implement FormSubmitCallback in your activity and override the following function 

    override fun onFormSubmitted(forms: List<Form>) {
         //do something with the result
    }




3. How to create a sectioned form and show only one section at a time

note: user can go to next section only after filling all the mandatory fields in the current section

Alt Text Alt Text Alt Text

Step 1:

Create a sectioned form using the 4 steps shown in previous section

Step 2:

after creating a sectioned form, to show one section at a time to user, you can do the following

binding.simpleForm.setData(sectionedForms, callback = this, showOnSectionAtATime = true)

OR

<?xml version="1.0" encoding="utf-8"?>

    <com.pradeep.form.simple_form.SimpleFormView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:showOneSectionAtATime="true" />

About

Simple Form is an Android library purely written in Kotlin. This library will help you create forms at ease. Try it in your next project. 😉

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages