├── app
├── .gitignore
├── questionnaire.gif
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable
│ │ │ │ └── ic_arrow_back.xml
│ │ │ └── layout
│ │ │ │ ├── footer.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── fragment_radio_boxes.xml
│ │ │ │ ├── fragment_check_boxes.xml
│ │ │ │ ├── activity_question.xml
│ │ │ │ └── activity_answers.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── spk
│ │ │ │ └── questionnaire
│ │ │ │ ├── questions
│ │ │ │ ├── questionmodels
│ │ │ │ │ ├── Data.java
│ │ │ │ │ ├── QuestionDataModel.java
│ │ │ │ │ ├── AnswerOptions.java
│ │ │ │ │ └── QuestionsItem.java
│ │ │ │ ├── widgets
│ │ │ │ │ └── NoSwipeViewPager.java
│ │ │ │ ├── adapters
│ │ │ │ │ └── ViewPagerAdapter.java
│ │ │ │ ├── qdb
│ │ │ │ │ ├── QuestionDao.java
│ │ │ │ │ ├── QuestionEntity.java
│ │ │ │ │ ├── QuestionChoicesDao.java
│ │ │ │ │ └── QuestionWithChoicesEntity.java
│ │ │ │ ├── database
│ │ │ │ │ └── AppDatabase.java
│ │ │ │ ├── AnswersActivity.java
│ │ │ │ ├── QuestionActivity.java
│ │ │ │ └── fragments
│ │ │ │ │ ├── RadioBoxesFragment.java
│ │ │ │ │ └── CheckBoxesFragment.java
│ │ │ │ ├── application
│ │ │ │ └── QuestionnaireApp.java
│ │ │ │ └── MainActivity.java
│ │ ├── AndroidManifest.xml
│ │ └── assets
│ │ │ └── questions_example.json
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── spk
│ │ │ └── questionnaire
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── spk
│ │ └── questionnaire
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
├── build.gradle
└── schemas
│ ├── com.spk.question.questions.database.AppDatabase
│ └── 1.json
│ └── com.spk.questionnaire.questions.database.AppDatabase
│ └── 1.json
├── _config.yml
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
├── gradlew
└── LICENCE.txt
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-cayman
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/questionnaire.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/questionnaire.gif
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShashiPrasadKushwaha/Questionnaire/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | /.idea
8 | .DS_Store
9 | /build
10 | /captures
11 | .externalNativeBuild
12 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Sep 26 02:18:40 IST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Questionnaire
3 | Start Questionnaire
4 | Show Result
5 | Finish
6 | Next
7 | Previous
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_arrow_back.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #009688
4 | #00796B
5 | #FF4081
6 |
7 | #000
8 | #cacaca
9 |
10 | #585858
11 | #FFFFFF
12 |
13 |
--------------------------------------------------------------------------------
/app/src/test/java/com/spk/questionnaire/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest
13 | {
14 | @Test
15 | public void addition_isCorrect()
16 | {
17 | assertEquals(4, 2 + 2);
18 | }
19 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/questionmodels/Data.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.questionmodels;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 |
5 | import java.util.List;
6 |
7 | public class Data
8 | {
9 | @SerializedName("questions")
10 | private List questions;
11 |
12 | public List getQuestions()
13 | {
14 | return questions;
15 | }
16 |
17 | public void setQuestions(List questions)
18 | {
19 | this.questions = questions;
20 | }
21 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/widgets/NoSwipeViewPager.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.widgets;
2 |
3 | import android.content.Context;
4 | import androidx.viewpager.widget.ViewPager;
5 | import android.util.AttributeSet;
6 | import android.view.MotionEvent;
7 |
8 | public class NoSwipeViewPager extends ViewPager
9 | {
10 | public NoSwipeViewPager(Context context) {
11 | super(context);
12 | }
13 |
14 | public NoSwipeViewPager(Context context, AttributeSet attrs) {
15 | super(context, attrs);
16 | }
17 |
18 | @Override
19 | public boolean onInterceptTouchEvent(MotionEvent ev) {
20 | return false;
21 | }
22 |
23 | @Override
24 | public boolean onTouchEvent(MotionEvent ev) {
25 | return false;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | android.enableJetifier=true
10 | android.useAndroidX=true
11 | org.gradle.jvmargs=-Xmx1536m
12 | # When configured, Gradle will run in incubating parallel mode.
13 | # This option should only be used with decoupled projects. More details, visit
14 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
15 | # org.gradle.parallel=true
16 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/adapters/ViewPagerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.adapters;
2 |
3 | import java.util.ArrayList;
4 |
5 | import androidx.fragment.app.Fragment;
6 | import androidx.fragment.app.FragmentManager;
7 | import androidx.fragment.app.FragmentPagerAdapter;
8 |
9 | public class ViewPagerAdapter extends FragmentPagerAdapter
10 | {
11 | private final ArrayList fragments;
12 |
13 | public ViewPagerAdapter(FragmentManager fm, ArrayList fragments) {
14 | super(fm);
15 | this.fragments = fragments;
16 | }
17 |
18 | @Override
19 | public Fragment getItem(int position) {
20 | return this.fragments.get(position);
21 | }
22 |
23 |
24 | @Override
25 | public int getCount() {
26 | return this.fragments.size();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/spk/questionnaire/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire;
2 |
3 | import android.content.Context;
4 |
5 | import org.junit.Test;
6 | import org.junit.runner.RunWith;
7 |
8 | import androidx.test.InstrumentationRegistry;
9 | import androidx.test.runner.AndroidJUnit4;
10 |
11 | import static org.junit.Assert.assertEquals;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest
20 | {
21 | @Test
22 | public void useAppContext()
23 | {
24 | // Context of the app under test.
25 | Context appContext = InstrumentationRegistry.getTargetContext();
26 |
27 | assertEquals("com.spk.question", appContext.getPackageName());
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/qdb/QuestionDao.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.qdb;
2 |
3 | import androidx.room.Dao;
4 | import androidx.room.Insert;
5 | import androidx.room.Query;
6 |
7 | import java.util.List;
8 |
9 | @Dao
10 | public interface QuestionDao
11 | {
12 | @Insert
13 | void insertAllQuestions(List questions);
14 |
15 | //@Query("UPDATE questions SET q_option_state = :selectState WHERE question_id = :questionId AND q_option_id =:optionId")
16 | //void updateQuestionWithChoice(String selectState, String questionId, String optionId);
17 |
18 | //@Query("SELECT q_option_state FROM questions WHERE question_id = :questionId AND q_option_id =:optionId")
19 | //String isChecked(String questionId, String optionId);
20 |
21 | @Query("SELECT * FROM questions")
22 | List getAllQuestions();
23 |
24 | @Query("DELETE FROM questions")
25 | void deleteAllQuestions();
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/application/QuestionnaireApp.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.application;
2 |
3 | import android.app.Application;
4 |
5 | import com.facebook.stetho.Stetho;
6 |
7 | public class QuestionnaireApp extends Application
8 | {
9 | private static QuestionnaireApp sInstance;
10 |
11 | public static synchronized QuestionnaireApp getInstance()
12 | {
13 | return sInstance;
14 | }
15 |
16 | @Override
17 | public void onCreate()
18 | {
19 | super.onCreate();
20 |
21 | sInstance = this;
22 |
23 | //Stetho is used to view the structure and values in Tables of Database.
24 | //Connect a device/emulator, run the app and complete the Questionnaire
25 | //then open Chrome browser and type this in address bar "chrome://inspect/#devices",
26 | //you will find connected device/emulator in below section of screen.
27 | Stetho.initializeWithDefaults(this);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/questionmodels/QuestionDataModel.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.questionmodels;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 |
5 | public class QuestionDataModel
6 | {
7 | @SerializedName("data")
8 | private Data data;
9 |
10 | @SerializedName("message")
11 | private String message;
12 |
13 | @SerializedName("status")
14 | private boolean status;
15 |
16 | public Data getData()
17 | {
18 | return data;
19 | }
20 |
21 | public void setData(Data data)
22 | {
23 | this.data = data;
24 | }
25 |
26 | public String getMessage()
27 | {
28 | return message;
29 | }
30 |
31 | public void setMessage(String message)
32 | {
33 | this.message = message;
34 | }
35 |
36 | public boolean isStatus()
37 | {
38 | return status;
39 | }
40 |
41 | public void setStatus(boolean status)
42 | {
43 | this.status = status;
44 | }
45 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/qdb/QuestionEntity.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.qdb;
2 |
3 | import androidx.room.ColumnInfo;
4 | import androidx.room.Entity;
5 | import androidx.room.PrimaryKey;
6 |
7 | @Entity(tableName = "questions")
8 | public class QuestionEntity
9 | {
10 | @PrimaryKey(autoGenerate = true)
11 | private long id;
12 | @ColumnInfo(name = "question_id")
13 | private int questionId;
14 | private String question;
15 |
16 | public long getId()
17 | {
18 | return id;
19 | }
20 |
21 | public void setId(long id)
22 | {
23 | this.id = id;
24 | }
25 |
26 | public int getQuestionId()
27 | {
28 | return questionId;
29 | }
30 |
31 | public void setQuestionId(int questionId)
32 | {
33 | this.questionId = questionId;
34 | }
35 |
36 | public String getQuestion()
37 | {
38 | return question;
39 | }
40 |
41 | public void setQuestion(String question)
42 | {
43 | this.question = question;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/qdb/QuestionChoicesDao.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.qdb;
2 |
3 | import androidx.room.Dao;
4 | import androidx.room.Insert;
5 | import androidx.room.Query;
6 |
7 | import java.util.List;
8 |
9 | @Dao
10 | public interface QuestionChoicesDao
11 | {
12 | @Insert
13 | void insertAllChoicesOfQuestion(List choices);
14 |
15 | @Query("UPDATE answer_choices SET ans_choice_state = :selectState WHERE question_id = :questionId AND ans_choice_pos =:optionId")
16 | void updateQuestionWithChoice(String selectState, String questionId, String optionId);
17 |
18 | @Query("SELECT ans_choice_state FROM answer_choices WHERE question_id = :questionId AND ans_choice_pos =:optionId")
19 | String isChecked(String questionId, String optionId);
20 |
21 | @Query("SELECT * FROM answer_choices WHERE ans_choice_state =:selected")
22 | List getAllQuestionsWithChoices(String selected);
23 |
24 | @Query("DELETE FROM answer_choices")
25 | void deleteAllChoicesOfQuestion();
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
14 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
26 |
27 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/database/AppDatabase.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.database;
2 |
3 | import androidx.room.Database;
4 | import androidx.room.Room;
5 | import androidx.room.RoomDatabase;
6 | import android.content.Context;
7 |
8 | import com.spk.questionnaire.questions.qdb.QuestionChoicesDao;
9 | import com.spk.questionnaire.questions.qdb.QuestionDao;
10 | import com.spk.questionnaire.questions.qdb.QuestionEntity;
11 | import com.spk.questionnaire.questions.qdb.QuestionWithChoicesEntity;
12 |
13 | @Database(entities = {QuestionWithChoicesEntity.class, QuestionEntity.class}, version = 1)
14 | public abstract class AppDatabase extends RoomDatabase
15 | {
16 | private static final String DB_NAME = "question_db";
17 |
18 | private static AppDatabase INSTANCE;
19 |
20 | public static synchronized AppDatabase getAppDatabase(Context context)
21 | {
22 | if (INSTANCE == null)
23 | {
24 | INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DB_NAME)
25 |
26 | .fallbackToDestructiveMigration()
27 | .build();
28 | }
29 | return INSTANCE;
30 | }
31 |
32 | public abstract QuestionChoicesDao getQuestionChoicesDao();
33 | public abstract QuestionDao getQuestionDao();
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/footer.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
24 |
25 |
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/questionmodels/AnswerOptions.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.questionmodels;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 |
6 | import com.google.gson.annotations.SerializedName;
7 |
8 | public class AnswerOptions implements Parcelable
9 | {
10 | public static final Creator CREATOR = new Creator()
11 | {
12 | @Override
13 | public AnswerOptions createFromParcel(Parcel in)
14 | {
15 | return new AnswerOptions(in);
16 | }
17 |
18 | @Override
19 | public AnswerOptions[] newArray(int size)
20 | {
21 | return new AnswerOptions[size];
22 | }
23 | };
24 | @SerializedName("answer_id")
25 | private String answerId;
26 | @SerializedName("name")
27 | private String name;
28 |
29 | protected AnswerOptions(Parcel in)
30 | {
31 | answerId = in.readString();
32 | name = in.readString();
33 | }
34 |
35 | public String getAnswerId()
36 | {
37 | return answerId;
38 | }
39 |
40 | public void setAnswerId(String answerId)
41 | {
42 | this.answerId = answerId;
43 | }
44 |
45 | public String getName()
46 | {
47 | return name;
48 | }
49 |
50 | public void setName(String name)
51 | {
52 | this.name = name;
53 | }
54 |
55 | @Override
56 | public int describeContents()
57 | {
58 | return 0;
59 | }
60 |
61 | @Override
62 | public void writeToParcel(Parcel dest, int flags)
63 | {
64 | dest.writeString(answerId);
65 | dest.writeString(name);
66 | }
67 | }
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.spk.questionnaire"
7 | minSdkVersion 21
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0.0"
11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
12 |
13 | javaCompileOptions {
14 | annotationProcessorOptions {
15 | arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
16 | }
17 | }
18 | }
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 |
26 | compileOptions {
27 | sourceCompatibility JavaVersion.VERSION_1_8
28 | targetCompatibility JavaVersion.VERSION_1_8
29 | }
30 | }
31 |
32 | dependencies {
33 | implementation fileTree(dir: 'libs', include: ['*.jar'])
34 | testImplementation 'junit:junit:4.12'
35 | androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
36 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
37 |
38 | implementation 'androidx.appcompat:appcompat:1.0.0'
39 | implementation 'androidx.legacy:legacy-support-v4:1.0.0'
40 | implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
41 |
42 | implementation 'com.google.code.gson:gson:2.8.5'
43 | implementation 'androidx.room:room-runtime:2.0.0-rc01'
44 | annotationProcessor 'androidx.room:room-compiler:2.0.0-rc01'
45 |
46 | implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
47 | implementation 'io.reactivex.rxjava2:rxjava:2.1.14'
48 |
49 | implementation 'com.facebook.stetho:stetho:1.5.0'
50 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/qdb/QuestionWithChoicesEntity.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.qdb;
2 |
3 | import androidx.room.ColumnInfo;
4 | import androidx.room.Entity;
5 | import androidx.room.PrimaryKey;
6 |
7 | @Entity(tableName = "answer_choices")
8 | public class QuestionWithChoicesEntity
9 | {
10 | @PrimaryKey(autoGenerate = true)
11 | private long id;
12 | @ColumnInfo(name = "question_id")
13 | private String questionId;
14 | @ColumnInfo(name = "ans_choice")
15 | private String answerChoice;
16 | @ColumnInfo(name = "ans_choice_pos")
17 | private String answerChoicePosition;
18 | @ColumnInfo(name = "ans_choice_id")
19 | private String answerChoiceId;
20 | @ColumnInfo(name = "ans_choice_state")
21 | private String answerChoiceState;
22 |
23 | public String getAnswerChoiceId()
24 | {
25 | return answerChoiceId;
26 | }
27 |
28 | public void setAnswerChoiceId(String answerChoiceId)
29 | {
30 | this.answerChoiceId = answerChoiceId;
31 | }
32 |
33 | public String getAnswerChoiceState()
34 | {
35 | return answerChoiceState;
36 | }
37 |
38 | public void setAnswerChoiceState(String answerChoiceState)
39 | {
40 | this.answerChoiceState = answerChoiceState;
41 | }
42 |
43 | public String getAnswerChoicePosition()
44 | {
45 | return answerChoicePosition;
46 | }
47 |
48 | public void setAnswerChoicePosition(String answerChoicePosition)
49 | {
50 | this.answerChoicePosition = answerChoicePosition;
51 | }
52 |
53 | public long getId()
54 | {
55 | return id;
56 | }
57 |
58 | public void setId(long id)
59 | {
60 | this.id = id;
61 | }
62 |
63 | public String getQuestionId()
64 | {
65 | return questionId;
66 | }
67 |
68 | public void setQuestionId(String questionId)
69 | {
70 | this.questionId = questionId;
71 | }
72 |
73 | public String getAnswerChoice()
74 | {
75 | return answerChoice;
76 | }
77 |
78 | public void setAnswerChoice(String answerChoice)
79 | {
80 | this.answerChoice = answerChoice;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
20 |
21 |
28 |
29 |
42 |
43 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_radio_boxes.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
24 |
25 |
34 |
35 |
44 |
45 |
51 |
52 |
53 |
54 |
55 |
56 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_check_boxes.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
24 |
25 |
34 |
35 |
44 |
45 |
51 |
52 |
53 |
54 |
55 |
56 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_question.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
24 |
25 |
29 |
30 |
42 |
43 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
66 |
67 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_answers.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
27 |
28 |
32 |
33 |
44 |
45 |
46 |
47 |
48 |
49 |
58 |
59 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import androidx.appcompat.app.AppCompatActivity;
6 | import androidx.appcompat.widget.Toolbar;
7 | import android.view.View;
8 | import android.widget.Button;
9 | import android.widget.Toast;
10 |
11 | import com.spk.questionnaire.questions.AnswersActivity;
12 | import com.spk.questionnaire.questions.QuestionActivity;
13 |
14 | import java.io.IOException;
15 | import java.io.InputStream;
16 |
17 | public class MainActivity extends AppCompatActivity
18 | {
19 | private static final int QUESTIONNAIRE_REQUEST = 2018;
20 | Button resultButton;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState)
24 | {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_main);
27 |
28 | setUpToolbar();
29 |
30 | Button questionnaireButton = findViewById(R.id.questionnaireButton);
31 | resultButton = findViewById(R.id.resultButton);
32 |
33 | questionnaireButton.setOnClickListener(v -> {
34 | resultButton.setVisibility(View.GONE);
35 |
36 | Intent questions = new Intent(MainActivity.this, QuestionActivity.class);
37 | //you have to pass as an extra the json string.
38 | questions.putExtra("json_questions", loadQuestionnaireJson("questions_example.json"));
39 | startActivityForResult(questions, QUESTIONNAIRE_REQUEST);
40 | });
41 |
42 | resultButton.setOnClickListener(v -> {
43 | Intent questions = new Intent(MainActivity.this, AnswersActivity.class);
44 | startActivity(questions);
45 | });
46 | }
47 |
48 | void setUpToolbar()
49 | {
50 | Toolbar mainPageToolbar = findViewById(R.id.mainPageToolbar);
51 | setSupportActionBar(mainPageToolbar);
52 | getSupportActionBar().setTitle("Questionnaire Demo");
53 | }
54 |
55 | @Override
56 | protected void onActivityResult(int requestCode, int resultCode, Intent data)
57 | {
58 | if (requestCode == QUESTIONNAIRE_REQUEST)
59 | {
60 | if (resultCode == RESULT_OK)
61 | {
62 | resultButton.setVisibility(View.VISIBLE);
63 | Toast.makeText(this, "Questionnaire Completed!!", Toast.LENGTH_LONG).show();
64 | }
65 | }
66 | }
67 |
68 | //json stored in the assets folder. but you can get it from wherever you like.
69 | private String loadQuestionnaireJson(String filename)
70 | {
71 | try
72 | {
73 | InputStream is = getAssets().open(filename);
74 | int size = is.available();
75 | byte[] buffer = new byte[size];
76 | is.read(buffer);
77 | is.close();
78 | return new String(buffer, "UTF-8");
79 | } catch (IOException ex)
80 | {
81 | ex.printStackTrace();
82 | return null;
83 | }
84 | }
85 | }
--------------------------------------------------------------------------------
/app/schemas/com.spk.question.questions.database.AppDatabase/1.json:
--------------------------------------------------------------------------------
1 | {
2 | "formatVersion": 1,
3 | "database": {
4 | "version": 1,
5 | "identityHash": "8e4aa8e37b13fecf5857071c6006a847",
6 | "entities": [
7 | {
8 | "tableName": "answer_choices",
9 | "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `question_id` TEXT, `ans_choice` TEXT, `ans_choice_pos` TEXT, `ans_choice_id` TEXT, `ans_choice_state` TEXT)",
10 | "fields": [
11 | {
12 | "fieldPath": "id",
13 | "columnName": "id",
14 | "affinity": "INTEGER",
15 | "notNull": true
16 | },
17 | {
18 | "fieldPath": "questionId",
19 | "columnName": "question_id",
20 | "affinity": "TEXT",
21 | "notNull": false
22 | },
23 | {
24 | "fieldPath": "answerChoice",
25 | "columnName": "ans_choice",
26 | "affinity": "TEXT",
27 | "notNull": false
28 | },
29 | {
30 | "fieldPath": "answerChoicePosition",
31 | "columnName": "ans_choice_pos",
32 | "affinity": "TEXT",
33 | "notNull": false
34 | },
35 | {
36 | "fieldPath": "answerChoiceId",
37 | "columnName": "ans_choice_id",
38 | "affinity": "TEXT",
39 | "notNull": false
40 | },
41 | {
42 | "fieldPath": "answerChoiceState",
43 | "columnName": "ans_choice_state",
44 | "affinity": "TEXT",
45 | "notNull": false
46 | }
47 | ],
48 | "primaryKey": {
49 | "columnNames": [
50 | "id"
51 | ],
52 | "autoGenerate": true
53 | },
54 | "indices": [],
55 | "foreignKeys": []
56 | },
57 | {
58 | "tableName": "questions",
59 | "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `question_id` INTEGER NOT NULL, `question` TEXT)",
60 | "fields": [
61 | {
62 | "fieldPath": "id",
63 | "columnName": "id",
64 | "affinity": "INTEGER",
65 | "notNull": true
66 | },
67 | {
68 | "fieldPath": "questionId",
69 | "columnName": "question_id",
70 | "affinity": "INTEGER",
71 | "notNull": true
72 | },
73 | {
74 | "fieldPath": "question",
75 | "columnName": "question",
76 | "affinity": "TEXT",
77 | "notNull": false
78 | }
79 | ],
80 | "primaryKey": {
81 | "columnNames": [
82 | "id"
83 | ],
84 | "autoGenerate": true
85 | },
86 | "indices": [],
87 | "foreignKeys": []
88 | }
89 | ],
90 | "setupQueries": [
91 | "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
92 | "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"8e4aa8e37b13fecf5857071c6006a847\")"
93 | ]
94 | }
95 | }
--------------------------------------------------------------------------------
/app/schemas/com.spk.questionnaire.questions.database.AppDatabase/1.json:
--------------------------------------------------------------------------------
1 | {
2 | "formatVersion": 1,
3 | "database": {
4 | "version": 1,
5 | "identityHash": "8e4aa8e37b13fecf5857071c6006a847",
6 | "entities": [
7 | {
8 | "tableName": "answer_choices",
9 | "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `question_id` TEXT, `ans_choice` TEXT, `ans_choice_pos` TEXT, `ans_choice_id` TEXT, `ans_choice_state` TEXT)",
10 | "fields": [
11 | {
12 | "fieldPath": "id",
13 | "columnName": "id",
14 | "affinity": "INTEGER",
15 | "notNull": true
16 | },
17 | {
18 | "fieldPath": "questionId",
19 | "columnName": "question_id",
20 | "affinity": "TEXT",
21 | "notNull": false
22 | },
23 | {
24 | "fieldPath": "answerChoice",
25 | "columnName": "ans_choice",
26 | "affinity": "TEXT",
27 | "notNull": false
28 | },
29 | {
30 | "fieldPath": "answerChoicePosition",
31 | "columnName": "ans_choice_pos",
32 | "affinity": "TEXT",
33 | "notNull": false
34 | },
35 | {
36 | "fieldPath": "answerChoiceId",
37 | "columnName": "ans_choice_id",
38 | "affinity": "TEXT",
39 | "notNull": false
40 | },
41 | {
42 | "fieldPath": "answerChoiceState",
43 | "columnName": "ans_choice_state",
44 | "affinity": "TEXT",
45 | "notNull": false
46 | }
47 | ],
48 | "primaryKey": {
49 | "columnNames": [
50 | "id"
51 | ],
52 | "autoGenerate": true
53 | },
54 | "indices": [],
55 | "foreignKeys": []
56 | },
57 | {
58 | "tableName": "questions",
59 | "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `question_id` INTEGER NOT NULL, `question` TEXT)",
60 | "fields": [
61 | {
62 | "fieldPath": "id",
63 | "columnName": "id",
64 | "affinity": "INTEGER",
65 | "notNull": true
66 | },
67 | {
68 | "fieldPath": "questionId",
69 | "columnName": "question_id",
70 | "affinity": "INTEGER",
71 | "notNull": true
72 | },
73 | {
74 | "fieldPath": "question",
75 | "columnName": "question",
76 | "affinity": "TEXT",
77 | "notNull": false
78 | }
79 | ],
80 | "primaryKey": {
81 | "columnNames": [
82 | "id"
83 | ],
84 | "autoGenerate": true
85 | },
86 | "indices": [],
87 | "foreignKeys": []
88 | }
89 | ],
90 | "setupQueries": [
91 | "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
92 | "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"8e4aa8e37b13fecf5857071c6006a847\")"
93 | ]
94 | }
95 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/questionmodels/QuestionsItem.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.questionmodels;
2 |
3 | import android.os.Bundle;
4 | import android.os.Parcel;
5 | import android.os.Parcelable;
6 |
7 | import com.google.gson.annotations.SerializedName;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | public class QuestionsItem implements Parcelable
13 | {
14 | public static final Creator CREATOR = new Creator()
15 | {
16 | @Override
17 | public QuestionsItem createFromParcel(Parcel in)
18 | {
19 | QuestionsItem questionItem = new QuestionsItem();
20 | questionItem.questionTypeName = in.readString();
21 | questionItem.questionName = in.readString();
22 | questionItem.id = in.readInt();
23 | Bundle b = in.readBundle(AnswerOptions.class.getClassLoader());
24 | questionItem.answerOptions = b.getParcelableArrayList("q_items");
25 | questionItem.questionTypeId = in.readInt();
26 |
27 | return questionItem;
28 | }
29 |
30 | @Override
31 | public QuestionsItem[] newArray(int size)
32 | {
33 | return new QuestionsItem[size];
34 | }
35 | };
36 | @SerializedName("question_type_name")
37 | private String questionTypeName;
38 | @SerializedName("question_name")
39 | private String questionName;
40 | @SerializedName("id")
41 | private int id;
42 | @SerializedName("question_item")
43 | private List answerOptions;
44 |
45 | @SerializedName("question_type_id")
46 | private int questionTypeId;
47 |
48 | public String getQuestionTypeName()
49 | {
50 | return questionTypeName;
51 | }
52 |
53 | public void setQuestionTypeName(String questionTypeName)
54 | {
55 | this.questionTypeName = questionTypeName;
56 | }
57 |
58 | public String getQuestionName()
59 | {
60 | return questionName;
61 | }
62 |
63 | public void setQuestionName(String questionName)
64 | {
65 | this.questionName = questionName;
66 | }
67 |
68 | public int getId()
69 | {
70 | return id;
71 | }
72 |
73 | public void setId(int id)
74 | {
75 | this.id = id;
76 | }
77 |
78 | public List getAnswerOptions()
79 | {
80 | return answerOptions;
81 | }
82 |
83 | public void setAnswerOptions(List answerOptions)
84 | {
85 | this.answerOptions = answerOptions;
86 | }
87 |
88 | public int getQuestionTypeId()
89 | {
90 | return questionTypeId;
91 | }
92 |
93 | public void setQuestionTypeId(int questionTypeId)
94 | {
95 | this.questionTypeId = questionTypeId;
96 | }
97 |
98 | @Override
99 | public int describeContents()
100 | {
101 | return 0;
102 | }
103 |
104 | @Override
105 | public void writeToParcel(Parcel dest, int flags)
106 | {
107 | dest.writeString(questionTypeName);
108 | dest.writeString(questionName);
109 | dest.writeInt(id);
110 | Bundle b = new Bundle();
111 | b.putParcelableArrayList("items", (ArrayList extends Parcelable>) answerOptions);
112 | dest.writeBundle(b);
113 | dest.writeInt(questionTypeId);
114 | }
115 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Questionnaire
2 | This is a demo application which will help to make **Questionnaire** *(Survey, Feedback, Poll, Filter, Exam, Questions)* for users in your App.
3 |
4 | This project got inspiration from this repo -> https://github.com/AndreiD/surveylib
5 |
6 | 
7 | ## Let see what you will learn from this Project.
8 |
9 | - How to use Room *(part of Android Architecture Components )*.
10 | - How to use simple Background process with RxJava *(RxAndroid)* or we say how to replace Async task with RxJava.
11 | - Parcelable with Nested Model (as List of Model inside Model) in Pojo.
12 | - Other various logic building.
13 |
14 | #### Situations: (When its helpful to use/refer this Project)
15 |
16 | - [x] This project **only** works with situations where You have Multiple/Single Choice(s) based questions.**(If more variation needed, refer [AndreiD's work](https://github.com/AndreiD/surveylib))**
17 |
18 | - [x] If you want persistence with the previous selection of choice(s), means user can go back and modify the selection and previous selections will not lose there state.**(This is addition feature in this Project)**
19 | - [x] What if you don't want or submit the result of *Questionnaire* as soon the completion. Means if there is other work(s) before sending the data to back-end, and also you don't want to hanging with result in between Activities. See the [AnswerActivity](https://github.com/ShashiPrasadKushwaha/Questionnaire/blob/master/app/src/main/java/com/spk/questionnaire/questions/AnswersActivity.java) , how to retrieve the result from database and display/send. **(This is addition feature in this Project)**
20 |
21 | ###### Want to send result in Json format see the [AnswerActivity.java](https://github.com/ShashiPrasadKushwaha/Questionnaire/blob/master/app/src/main/java/com/spk/questionnaire/questions/AnswersActivity.java)
22 | ```java
23 | /*Here,JSON got created and send to make Result View as per Project requirement.
24 | * Alternatively, in your case, you make Network-call to send the result to back-end.*/
25 | private void makeJsonDataToMakeResultView()
26 | {
27 | try
28 | {
29 | JSONArray questionAndAnswerArray = new JSONArray();
30 | int questionsSize = questionsList.size();
31 | if (questionsSize > 0)
32 | {
33 | for (int i = 0; i < questionsSize; i++)
34 | {
35 | JSONObject questionName = new JSONObject();
36 | questionName.put("question",questionsList.get(i).getQuestion());
37 | //questionName.put("question_id",String.valueOf(questionsList.get(i).getQuestionId()));
38 | String questionId = String.valueOf(questionsList.get(i).getQuestionId());
39 |
40 | JSONArray answerChoicesList = new JSONArray();
41 | int selectedChoicesSize = questionsWithAllChoicesList.size();
42 |
43 | for (int k = 0; k < selectedChoicesSize; k++)
44 | {
45 | String questionIdOfChoice = questionsWithAllChoicesList.get(k).getQuestionId();
46 | if (questionId.equals(questionIdOfChoice))
47 | {
48 | JSONObject selectedChoice = new JSONObject();
49 | selectedChoice.put("answer_choice", questionsWithAllChoicesList.get(k).getAnswerChoice());
50 | //selectedChoice.put("answer_id", questionsWithAllChoicesList.get(k).getAnswerChoiceId());
51 | answerChoicesList.put(selectedChoice);
52 | }
53 | }
54 |
55 | questionName.put("selected_answer", answerChoicesList);
56 |
57 | questionAndAnswerArray.put(questionName);
58 | }
59 | }
60 | questionsAnswerView(questionAndAnswerArray);
61 |
62 | } catch (JSONException e)
63 | {
64 | e.printStackTrace();
65 | }
66 | }
67 | ```
68 | #### If you found helpful and learnt something new from this work, please star this project.
69 |
70 | ## License
71 |
72 | ~~~~
73 | Copyright 2018 Shashi Prasad Kushwaha
74 |
75 | Licensed under the Apache License, Version 2.0 (the "License");
76 | you may not use this file except in compliance with the License.
77 | You may obtain a copy of the License at
78 |
79 | http://www.apache.org/licenses/LICENSE-2.0
80 |
81 | Unless required by applicable law or agreed to in writing, software
82 | distributed under the License is distributed on an "AS IS" BASIS,
83 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
84 | See the License for the specific language governing permissions and
85 | limitations under the License.
86 | ~~~~
--------------------------------------------------------------------------------
/app/src/main/assets/questions_example.json:
--------------------------------------------------------------------------------
1 | {
2 | "data": {
3 | "questions": [
4 | {
5 | "id": 18,
6 | "question_type_id": 1,
7 | "question_type_name": "CheckBox",
8 | "question_name": "Type of Operating System ?",
9 | "question_item": [
10 | {
11 | "answer_id": "11",
12 | "name": "Android"
13 | },
14 | {
15 | "answer_id": "12",
16 | "name": "iOS"
17 | },
18 | {
19 | "answer_id": "13",
20 | "name": "Windows"
21 | },
22 | {
23 | "answer_id": "14",
24 | "name": "Linux"
25 | }
26 | ]
27 | },
28 | {
29 | "id": 7,
30 | "question_type_id": 2,
31 | "question_type_name": "Radio",
32 | "question_name": "Type of Brand ?",
33 | "question_item": [
34 | {
35 | "answer_id": "21",
36 | "name": "Samsung"
37 | },
38 | {
39 | "answer_id": "22",
40 | "name": "Apple"
41 | },
42 | {
43 | "answer_id": "23",
44 | "name": "Motorola"
45 | },
46 | {
47 | "answer_id": "24",
48 | "name": "Google"
49 | },
50 | {
51 | "answer_id": "25",
52 | "name": "HTC"
53 | },
54 | {
55 | "answer_id": "26",
56 | "name": "LG"
57 | }
58 | ]
59 | },
60 | {
61 | "id": 6,
62 | "question_type_id": 1,
63 | "question_type_name": "CheckBox",
64 | "question_name": "Included features ?",
65 | "question_item": [
66 | {
67 | "answer_id": "31",
68 | "name": "FM Player"
69 | },
70 | {
71 | "answer_id": "32",
72 | "name": "Finger Print"
73 | },
74 | {
75 | "answer_id": "33",
76 | "name": "NFC"
77 | },
78 | {
79 | "answer_id": "34",
80 | "name": "USB"
81 | },
82 | {
83 | "answer_id": "35",
84 | "name": "Wireless Charging"
85 | }
86 | ]
87 | },
88 | {
89 | "id": 5,
90 | "question_type_id": 2,
91 | "question_type_name": "Radio",
92 | "question_name": "Manufacture year ?",
93 | "question_item": [
94 | {
95 | "answer_id": "41",
96 | "name": "2009 - 2011"
97 | },
98 | {
99 | "answer_id": "42",
100 | "name": "2011 - 2013"
101 | },
102 | {
103 | "answer_id": "43",
104 | "name": "2013 - 2015"
105 | },
106 | {
107 | "answer_id": "44",
108 | "name": "2015 - 2018"
109 | },
110 | {
111 | "answer_id": "45",
112 | "name": "2018 - above"
113 | }
114 | ]
115 | },
116 | {
117 | "id": 2,
118 | "question_type_id": 2,
119 | "question_type_name": "Radio",
120 | "question_name": "Processor Brand ?",
121 | "question_item": [
122 | {
123 | "answer_id": "51",
124 | "name": "Apple"
125 | },
126 | {
127 | "answer_id": "52",
128 | "name": "Broadcom"
129 | },
130 | {
131 | "answer_id": "53",
132 | "name": "Intel"
133 | },
134 | {
135 | "answer_id": "54",
136 | "name": "Snapdragon"
137 | },
138 | {
139 | "answer_id": "55",
140 | "name": "Mediatek"
141 | },
142 | {
143 | "answer_id": "56",
144 | "name": "AMD"
145 | }
146 | ]
147 | },
148 | {
149 | "id": 19,
150 | "question_type_id": 2,
151 | "question_type_name": "Radio",
152 | "question_name": "Payment mode ?",
153 | "question_item": [
154 | {
155 | "answer_id": "61",
156 | "name": "Cash On Delivery"
157 | },
158 | {
159 | "answer_id": "62",
160 | "name": "Android Pay"
161 | },
162 | {
163 | "answer_id": "63",
164 | "name": "Net Banking"
165 | },
166 | {
167 | "answer_id": "64",
168 | "name": "Credit/Debit"
169 | },
170 | {
171 | "answer_id": "65",
172 | "name": "Saved Cards"
173 | },
174 | {
175 | "answer_id": "66",
176 | "name": "My Wallet"
177 | }
178 | ]
179 | }
180 | ]
181 | },
182 | "status": true,
183 | "message": "The data was fetched successfully"
184 | }
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/AnswersActivity.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions;
2 |
3 | import android.content.Context;
4 | import android.graphics.Typeface;
5 | import android.os.Bundle;
6 | import android.util.TypedValue;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.LinearLayout;
10 | import android.widget.TextView;
11 |
12 | import com.spk.questionnaire.R;
13 | import com.spk.questionnaire.questions.database.AppDatabase;
14 | import com.spk.questionnaire.questions.qdb.QuestionEntity;
15 | import com.spk.questionnaire.questions.qdb.QuestionWithChoicesEntity;
16 |
17 | import org.json.JSONArray;
18 | import org.json.JSONException;
19 | import org.json.JSONObject;
20 |
21 | import java.util.ArrayList;
22 | import java.util.List;
23 |
24 | import androidx.appcompat.app.AppCompatActivity;
25 | import androidx.appcompat.widget.Toolbar;
26 | import androidx.core.content.ContextCompat;
27 | import io.reactivex.Completable;
28 | import io.reactivex.CompletableObserver;
29 | import io.reactivex.android.schedulers.AndroidSchedulers;
30 | import io.reactivex.disposables.Disposable;
31 | import io.reactivex.schedulers.Schedulers;
32 |
33 | public class AnswersActivity extends AppCompatActivity
34 | {
35 | Context context;
36 | LinearLayout resultLinearLayout;
37 | List questionsList = new ArrayList<>();
38 | List questionsWithAllChoicesList = new ArrayList<>();
39 | private AppDatabase appDatabase;
40 |
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState)
43 | {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_answers);
46 |
47 | context = this;
48 | appDatabase = AppDatabase.getAppDatabase(this);
49 |
50 | resultLinearLayout = findViewById(R.id.resultLinearLayout);
51 | toolBarInit();
52 |
53 | getResultFromDatabase();
54 | }
55 |
56 | private void toolBarInit()
57 | {
58 | Toolbar answerToolBar = findViewById(R.id.answerToolbar);
59 | answerToolBar.setNavigationIcon(R.drawable.ic_arrow_back);
60 | answerToolBar.setNavigationOnClickListener(v -> onBackPressed());
61 | }
62 |
63 | /*After, getting all result you can/must delete the saved results
64 | although we are clearing the Tables as soon we start the QuestionActivity.*/
65 | private void getResultFromDatabase()
66 | {
67 | Completable.fromAction(() -> {
68 | questionsList = appDatabase.getQuestionDao().getAllQuestions();
69 | questionsWithAllChoicesList = appDatabase.getQuestionChoicesDao().getAllQuestionsWithChoices("1");
70 | }).subscribeOn(Schedulers.io())
71 | .observeOn(AndroidSchedulers.mainThread())
72 | .subscribe(new CompletableObserver()
73 | {
74 | @Override
75 | public void onSubscribe(Disposable d)
76 | {
77 |
78 | }
79 |
80 | @Override
81 | public void onComplete()
82 | {
83 | makeJsonDataToMakeResultView();
84 | }
85 |
86 | @Override
87 | public void onError(Throwable e)
88 | {
89 |
90 | }
91 | });
92 | }
93 |
94 | /*Here, JSON got created and send to make Result View as per Project requirement.
95 | * Alternatively, in your case, you make Network-call to send the result to back-end.*/
96 | private void makeJsonDataToMakeResultView()
97 | {
98 | try
99 | {
100 | JSONArray questionAndAnswerArray = new JSONArray();
101 | int questionsSize = questionsList.size();
102 | if (questionsSize > 0)
103 | {
104 | for (int i = 0; i < questionsSize; i++)
105 | {
106 | JSONObject questionName = new JSONObject();
107 | questionName.put("question", questionsList.get(i).getQuestion());
108 | //questionName.put("question_id", String.valueOf(questionsList.get(i).getQuestionId()));
109 | String questionId = String.valueOf(questionsList.get(i).getQuestionId());
110 |
111 | JSONArray answerChoicesList = new JSONArray();
112 | int selectedChoicesSize = questionsWithAllChoicesList.size();
113 | for (int k = 0; k < selectedChoicesSize; k++)
114 | {
115 | String questionIdOfChoice = questionsWithAllChoicesList.get(k).getQuestionId();
116 | if (questionId.equals(questionIdOfChoice))
117 | {
118 | JSONObject selectedChoice = new JSONObject();
119 | selectedChoice.put("answer_choice", questionsWithAllChoicesList.get(k).getAnswerChoice());
120 | //selectedChoice.put("answer_id", questionsWithAllChoicesList.get(k).getAnswerChoiceId());
121 | answerChoicesList.put(selectedChoice);
122 | }
123 | }
124 | questionName.put("selected_answer", answerChoicesList);
125 |
126 | questionAndAnswerArray.put(questionName);
127 | }
128 | }
129 |
130 | questionsAnswerView(questionAndAnswerArray);
131 |
132 | } catch (JSONException e)
133 | {
134 | e.printStackTrace();
135 | }
136 | }
137 |
138 | private void questionsAnswerView(JSONArray questionsWithAnswerArray)
139 | {
140 | if (questionsWithAnswerArray.length() > 0)
141 | {
142 | try
143 | {
144 | for (int i = 0; i < questionsWithAnswerArray.length(); i++)
145 | {
146 | String question = questionsWithAnswerArray.getJSONObject(i).getString("question");
147 |
148 | TextView questionTextView = new TextView(context);
149 | questionTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
150 | questionTextView.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
151 | questionTextView.setPadding(40, 30, 16, 30);
152 | questionTextView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
153 | questionTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
154 | questionTextView.setTypeface(null, Typeface.BOLD);
155 | questionTextView.setText(question);
156 |
157 | resultLinearLayout.addView(questionTextView);
158 |
159 | JSONArray selectedAnswerJSONArray = questionsWithAnswerArray.getJSONObject(i).getJSONArray("selected_answer");
160 |
161 | for (int j = 0; j < selectedAnswerJSONArray.length(); j++)
162 | {
163 | String answer = selectedAnswerJSONArray.getJSONObject(j).getString("answer_choice");
164 | String formattedAnswer = "• " + answer; // alt + 7 --> •
165 |
166 | TextView answerTextView = new TextView(context);
167 | answerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
168 | answerTextView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
169 | answerTextView.setPadding(60, 30, 16, 30);
170 | answerTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
171 | answerTextView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorWhite));
172 | answerTextView.setText(formattedAnswer);
173 |
174 | View view = new View(context);
175 | view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
176 | view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
177 |
178 | resultLinearLayout.addView(answerTextView);
179 | resultLinearLayout.addView(view);
180 | }
181 | }
182 | } catch (JSONException e)
183 | {
184 | e.printStackTrace();
185 | }
186 | }
187 | }
188 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/QuestionActivity.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions;
2 |
3 | import android.os.Bundle;
4 | import android.text.Spannable;
5 | import android.text.SpannableString;
6 | import android.text.style.RelativeSizeSpan;
7 | import android.widget.TextView;
8 |
9 | import com.google.gson.Gson;
10 | import com.spk.questionnaire.R;
11 | import com.spk.questionnaire.questions.adapters.ViewPagerAdapter;
12 | import com.spk.questionnaire.questions.database.AppDatabase;
13 | import com.spk.questionnaire.questions.fragments.CheckBoxesFragment;
14 | import com.spk.questionnaire.questions.fragments.RadioBoxesFragment;
15 | import com.spk.questionnaire.questions.qdb.QuestionEntity;
16 | import com.spk.questionnaire.questions.qdb.QuestionWithChoicesEntity;
17 | import com.spk.questionnaire.questions.questionmodels.AnswerOptions;
18 | import com.spk.questionnaire.questions.questionmodels.QuestionDataModel;
19 | import com.spk.questionnaire.questions.questionmodels.QuestionsItem;
20 |
21 | import java.util.ArrayList;
22 | import java.util.List;
23 |
24 | import androidx.appcompat.app.AppCompatActivity;
25 | import androidx.appcompat.widget.Toolbar;
26 | import androidx.fragment.app.Fragment;
27 | import androidx.viewpager.widget.ViewPager;
28 | import io.reactivex.Observable;
29 | import io.reactivex.schedulers.Schedulers;
30 |
31 | public class QuestionActivity extends AppCompatActivity
32 | {
33 | final ArrayList fragmentArrayList = new ArrayList<>();
34 | List questionsItems = new ArrayList<>();
35 | private AppDatabase appDatabase;
36 | //private TextView questionToolbarTitle;
37 | private TextView questionPositionTV;
38 | private String totalQuestions = "1";
39 | private Gson gson;
40 | private ViewPager questionsViewPager;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState)
44 | {
45 | super.onCreate(savedInstanceState);
46 | setContentView(R.layout.activity_question);
47 |
48 | toolBarInit();
49 |
50 | appDatabase = AppDatabase.getAppDatabase(QuestionActivity.this);
51 | gson = new Gson();
52 |
53 | if (getIntent().getExtras() != null)
54 | {
55 | Bundle bundle = getIntent().getExtras();
56 | parsingData(bundle);
57 | }
58 | }
59 |
60 | private void toolBarInit()
61 | {
62 | Toolbar questionToolbar = findViewById(R.id.questionToolbar);
63 | questionToolbar.setNavigationIcon(R.drawable.ic_arrow_back);
64 | questionToolbar.setNavigationOnClickListener(v -> onBackPressed());
65 |
66 | //questionToolbarTitle = questionToolbar.findViewById(R.id.questionToolbarTitle);
67 | questionPositionTV = questionToolbar.findViewById(R.id.questionPositionTV);
68 |
69 | //questionToolbarTitle.setText("Questions");
70 | }
71 |
72 | /*This method decides how many Question-Screen(s) will be created and
73 | what kind of (Multiple/Single choices) each Screen will be.*/
74 | private void parsingData(Bundle bundle)
75 | {
76 | QuestionDataModel questionDataModel = new QuestionDataModel();
77 |
78 | questionDataModel = gson.fromJson(bundle.getString("json_questions"), QuestionDataModel.class);
79 |
80 | questionsItems = questionDataModel.getData().getQuestions();
81 |
82 | totalQuestions = String.valueOf(questionsItems.size());
83 | String questionPosition = "1/" + totalQuestions;
84 | setTextWithSpan(questionPosition);
85 |
86 | preparingQuestionInsertionInDb(questionsItems);
87 | preparingInsertionInDb(questionsItems);
88 |
89 | for (int i = 0; i < questionsItems.size(); i++)
90 | {
91 | QuestionsItem question = questionsItems.get(i);
92 |
93 | if (question.getQuestionTypeName().equals("CheckBox"))
94 | {
95 | CheckBoxesFragment checkBoxesFragment = new CheckBoxesFragment();
96 | Bundle checkBoxBundle = new Bundle();
97 | checkBoxBundle.putParcelable("question", question);
98 | checkBoxBundle.putInt("page_position", i);
99 | checkBoxesFragment.setArguments(checkBoxBundle);
100 | fragmentArrayList.add(checkBoxesFragment);
101 | }
102 |
103 | if (question.getQuestionTypeName().equals("Radio"))
104 | {
105 | RadioBoxesFragment radioBoxesFragment = new RadioBoxesFragment();
106 | Bundle radioButtonBundle = new Bundle();
107 | radioButtonBundle.putParcelable("question", question);
108 | radioButtonBundle.putInt("page_position", i);
109 | radioBoxesFragment.setArguments(radioButtonBundle);
110 | fragmentArrayList.add(radioBoxesFragment);
111 | }
112 | }
113 |
114 | questionsViewPager = findViewById(R.id.pager);
115 | questionsViewPager.setOffscreenPageLimit(1);
116 | ViewPagerAdapter mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentArrayList);
117 | questionsViewPager.setAdapter(mPagerAdapter);
118 | }
119 |
120 | public void nextQuestion()
121 | {
122 | int item = questionsViewPager.getCurrentItem() + 1;
123 | questionsViewPager.setCurrentItem(item);
124 |
125 | String currentQuestionPosition = String.valueOf(item + 1);
126 |
127 | String questionPosition = currentQuestionPosition + "/" + totalQuestions;
128 | setTextWithSpan(questionPosition);
129 | }
130 |
131 | public int getTotalQuestionsSize()
132 | {
133 | return questionsItems.size();
134 | }
135 |
136 | private void preparingQuestionInsertionInDb(List questionsItems)
137 | {
138 | List questionEntities = new ArrayList<>();
139 |
140 | for (int i = 0; i < questionsItems.size(); i++)
141 | {
142 | QuestionEntity questionEntity = new QuestionEntity();
143 | questionEntity.setQuestionId(questionsItems.get(i).getId());
144 | questionEntity.setQuestion(questionsItems.get(i).getQuestionName());
145 |
146 | questionEntities.add(questionEntity);
147 | }
148 | insertQuestionInDatabase(questionEntities);
149 | }
150 |
151 | private void insertQuestionInDatabase(List questionEntities)
152 | {
153 | Observable.just(questionEntities)
154 | .map(this::insertingQuestionInDb)
155 | .subscribeOn(Schedulers.io())
156 | .subscribe();
157 | }
158 |
159 | /*First, clear the table, if any previous data saved in it. Otherwise, we get repeated data.*/
160 | private String insertingQuestionInDb(List questionEntities)
161 | {
162 | appDatabase.getQuestionDao().deleteAllQuestions();
163 | appDatabase.getQuestionDao().insertAllQuestions(questionEntities);
164 | return "";
165 | }
166 |
167 | private void preparingInsertionInDb(List questionsItems)
168 | {
169 | ArrayList questionWithChoicesEntities = new ArrayList<>();
170 |
171 | for (int i = 0; i < questionsItems.size(); i++)
172 | {
173 | List answerOptions = questionsItems.get(i).getAnswerOptions();
174 |
175 | for (int j = 0; j < answerOptions.size(); j++)
176 | {
177 | QuestionWithChoicesEntity questionWithChoicesEntity = new QuestionWithChoicesEntity();
178 | questionWithChoicesEntity.setQuestionId(String.valueOf(questionsItems.get(i).getId()));
179 | questionWithChoicesEntity.setAnswerChoice(answerOptions.get(j).getName());
180 | questionWithChoicesEntity.setAnswerChoicePosition(String.valueOf(j));
181 | questionWithChoicesEntity.setAnswerChoiceId(answerOptions.get(j).getAnswerId());
182 | questionWithChoicesEntity.setAnswerChoiceState("0");
183 |
184 | questionWithChoicesEntities.add(questionWithChoicesEntity);
185 | }
186 | }
187 |
188 | insertQuestionWithChoicesInDatabase(questionWithChoicesEntities);
189 | }
190 |
191 | private void insertQuestionWithChoicesInDatabase(List questionWithChoicesEntities)
192 | {
193 | Observable.just(questionWithChoicesEntities)
194 | .map(this::insertingQuestionWithChoicesInDb)
195 | .subscribeOn(Schedulers.io())
196 | .subscribe();
197 | }
198 |
199 | /*First, clear the table, if any previous data saved in it. Otherwise, we get repeated data.*/
200 | private String insertingQuestionWithChoicesInDb(List questionWithChoicesEntities)
201 | {
202 | appDatabase.getQuestionChoicesDao().deleteAllChoicesOfQuestion();
203 | appDatabase.getQuestionChoicesDao().insertAllChoicesOfQuestion(questionWithChoicesEntities);
204 | return "";
205 | }
206 |
207 | @Override
208 | public void onBackPressed()
209 | {
210 | if (questionsViewPager.getCurrentItem() == 0)
211 | {
212 | super.onBackPressed();
213 | } else
214 | {
215 | int item = questionsViewPager.getCurrentItem() - 1;
216 | questionsViewPager.setCurrentItem(item);
217 |
218 | String currentQuestionPosition = String.valueOf(item + 1);
219 |
220 | String questionPosition = currentQuestionPosition + "/" + totalQuestions;
221 | setTextWithSpan(questionPosition);
222 | }
223 | }
224 |
225 | private void setTextWithSpan(String questionPosition)
226 | {
227 | int slashPosition = questionPosition.indexOf("/");
228 |
229 | Spannable spanText = new SpannableString(questionPosition);
230 | spanText.setSpan(new RelativeSizeSpan(0.7f), slashPosition, questionPosition.length(), 0);
231 | questionPositionTV.setText(spanText);
232 | }
233 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/fragments/RadioBoxesFragment.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.fragments;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.util.TypedValue;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.Button;
11 | import android.widget.LinearLayout;
12 | import android.widget.RadioButton;
13 | import android.widget.RadioGroup;
14 | import android.widget.TextView;
15 |
16 | import com.spk.questionnaire.R;
17 | import com.spk.questionnaire.questions.QuestionActivity;
18 | import com.spk.questionnaire.questions.database.AppDatabase;
19 | import com.spk.questionnaire.questions.questionmodels.AnswerOptions;
20 | import com.spk.questionnaire.questions.questionmodels.QuestionsItem;
21 |
22 | import java.util.ArrayList;
23 | import java.util.List;
24 |
25 | import androidx.core.content.ContextCompat;
26 | import androidx.fragment.app.Fragment;
27 | import androidx.fragment.app.FragmentActivity;
28 | import io.reactivex.Observable;
29 | import io.reactivex.Observer;
30 | import io.reactivex.android.schedulers.AndroidSchedulers;
31 | import io.reactivex.disposables.Disposable;
32 | import io.reactivex.schedulers.Schedulers;
33 |
34 | /**
35 | * This fragment provide the RadioButton/Single Options.
36 | */
37 | public class RadioBoxesFragment extends Fragment
38 | {
39 | private final ArrayList radioButtonArrayList = new ArrayList<>();
40 | private boolean screenVisible = false;
41 | private QuestionsItem radioButtonTypeQuestion;
42 | private FragmentActivity mContext;
43 | private Button nextOrFinishButton;
44 | //private Button previousButton;
45 | private TextView questionRBTypeTextView;
46 | private RadioGroup radioGroupForChoices;
47 | private boolean atLeastOneChecked = false;
48 | private AppDatabase appDatabase;
49 | private String questionId = "";
50 | private int currentPagePosition = 0;
51 | private int clickedRadioButtonPosition = 0;
52 | private String qState = "0";
53 |
54 | public RadioBoxesFragment()
55 | {
56 | // Required empty public constructor
57 | }
58 |
59 | @Override
60 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
61 | {
62 | ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_radio_boxes, container, false);
63 |
64 | appDatabase = AppDatabase.getAppDatabase(getActivity());
65 |
66 | nextOrFinishButton = rootView.findViewById(R.id.nextOrFinishButton);
67 | //previousButton = rootView.findViewById(R.id.previousButton);
68 | questionRBTypeTextView = rootView.findViewById(R.id.questionRBTypeTextView);
69 | radioGroupForChoices = rootView.findViewById(R.id.radioGroupForChoices);
70 |
71 | nextOrFinishButton.setOnClickListener(v -> {
72 | if (currentPagePosition == ((QuestionActivity) mContext).getTotalQuestionsSize())
73 | {
74 | /* Here, You go back from where you started OR If you want to go next Activity just change the Intent*/
75 | Intent returnIntent = new Intent();
76 | mContext.setResult(Activity.RESULT_OK, returnIntent);
77 | mContext.finish();
78 |
79 | } else
80 | {
81 | ((QuestionActivity) mContext).nextQuestion();
82 | }
83 | });
84 | //previousButton.setOnClickListener(view -> mContext.onBackPressed());
85 |
86 | return rootView;
87 | }
88 |
89 | /*This method get called only when the fragment get visible, and here states of Radio Button(s) retained*/
90 | @Override
91 | public void setUserVisibleHint(boolean isVisibleToUser)
92 | {
93 | super.setUserVisibleHint(isVisibleToUser);
94 |
95 | if (isVisibleToUser)
96 | {
97 | screenVisible = true;
98 | for (int i = 0; i < radioButtonArrayList.size(); i++)
99 | {
100 | RadioButton radioButton = radioButtonArrayList.get(i);
101 | String cbPosition = String.valueOf(i);
102 |
103 | String[] data = new String[]{questionId, cbPosition};
104 | Observable.just(data)
105 | .map(this::getTheStateOfRadioBox)
106 | .subscribeOn(Schedulers.io())
107 | .observeOn(AndroidSchedulers.mainThread())
108 | .subscribe(new Observer()
109 | {
110 | @Override
111 | public void onSubscribe(Disposable d)
112 | {
113 |
114 | }
115 |
116 | @Override
117 | public void onNext(String s)
118 | {
119 | qState = s;
120 | }
121 |
122 | @Override
123 | public void onError(Throwable e)
124 | {
125 |
126 | }
127 |
128 | @Override
129 | public void onComplete()
130 | {
131 | if (qState.equals("1"))
132 | {
133 | radioButton.setChecked(true);
134 | } else
135 | {
136 | radioButton.setChecked(false);
137 | }
138 | }
139 | });
140 | }
141 | }
142 | }
143 |
144 | private String getTheStateOfRadioBox(String[] data)
145 | {
146 | return appDatabase.getQuestionChoicesDao().isChecked(data[0], data[1]);
147 | }
148 |
149 | private void saveActionsOfRadioBox()
150 | {
151 | for (int i = 0; i < radioButtonArrayList.size(); i++)
152 | {
153 | if (i == clickedRadioButtonPosition)
154 | {
155 | RadioButton radioButton = radioButtonArrayList.get(i);
156 | if (radioButton.isChecked())
157 | {
158 | atLeastOneChecked = true;
159 |
160 | String cbPosition = String.valueOf(radioButtonArrayList.indexOf(radioButton));
161 |
162 | String[] data = new String[]{"1", questionId, cbPosition};
163 | insertChoiceInDatabase(data);
164 |
165 | } else
166 | {
167 | String cbPosition = String.valueOf(radioButtonArrayList.indexOf(radioButton));
168 |
169 | String[] data = new String[]{"0", questionId, cbPosition};
170 | insertChoiceInDatabase(data);
171 | }
172 | }
173 | }
174 |
175 | if (atLeastOneChecked)
176 | {
177 | nextOrFinishButton.setEnabled(true);
178 | } else
179 | {
180 | nextOrFinishButton.setEnabled(false);
181 | }
182 | }
183 |
184 | private void insertChoiceInDatabase(String[] data)
185 | {
186 | Observable.just(data)
187 | .map(this::insertingInDb)
188 | .subscribeOn(Schedulers.io())
189 | .subscribe();
190 | }
191 |
192 | private String insertingInDb(String[] data)
193 | {
194 | appDatabase.getQuestionChoicesDao().updateQuestionWithChoice(data[0], data[1], data[2]);
195 | return "";
196 | }
197 |
198 | @Override
199 | public void onActivityCreated(Bundle savedInstanceState)
200 | {
201 | super.onActivityCreated(savedInstanceState);
202 |
203 | mContext = getActivity();
204 | if (getArguments() != null)
205 | {
206 | radioButtonTypeQuestion = getArguments().getParcelable("question");
207 | questionId = String.valueOf(radioButtonTypeQuestion != null ? radioButtonTypeQuestion.getId() : 0);
208 | currentPagePosition = getArguments().getInt("page_position") + 1;
209 | }
210 |
211 | questionRBTypeTextView.setText(radioButtonTypeQuestion.getQuestionName());
212 |
213 | List choices = radioButtonTypeQuestion.getAnswerOptions();
214 | radioButtonArrayList.clear();
215 |
216 | for (AnswerOptions choice : choices)
217 | {
218 | RadioButton rb = new RadioButton(mContext);
219 | rb.setText(choice.getName());
220 | rb.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
221 | rb.setTextColor(ContextCompat.getColor(mContext, R.color.grey));
222 | rb.setPadding(10, 40, 10, 40);
223 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
224 | params.leftMargin = 25;
225 | rb.setLayoutParams(params);
226 |
227 | View view = new View(mContext);
228 | view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.divider));
229 | view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
230 |
231 | radioGroupForChoices.addView(rb);
232 | radioGroupForChoices.addView(view);
233 | radioButtonArrayList.add(rb);
234 |
235 | rb.setOnCheckedChangeListener((buttonView, isChecked) -> {
236 | if (screenVisible)
237 | {
238 | clickedRadioButtonPosition = radioButtonArrayList.indexOf(buttonView);
239 | saveActionsOfRadioBox();
240 | }
241 | });
242 | }
243 |
244 | if (atLeastOneChecked)
245 | {
246 | nextOrFinishButton.setEnabled(true);
247 | } else
248 | {
249 | nextOrFinishButton.setEnabled(false);
250 | }
251 |
252 | /* If the current question is last in the questionnaire then
253 | the "Next" button will change into "Finish" button*/
254 | if (currentPagePosition == ((QuestionActivity) mContext).getTotalQuestionsSize())
255 | {
256 | nextOrFinishButton.setText(R.string.finish);
257 | } else
258 | {
259 | nextOrFinishButton.setText(R.string.next);
260 | }
261 | }
262 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/spk/questionnaire/questions/fragments/CheckBoxesFragment.java:
--------------------------------------------------------------------------------
1 | package com.spk.questionnaire.questions.fragments;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.util.TypedValue;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.Button;
11 | import android.widget.CheckBox;
12 | import android.widget.LinearLayout;
13 | import android.widget.TextView;
14 |
15 | import com.spk.questionnaire.R;
16 | import com.spk.questionnaire.questions.QuestionActivity;
17 | import com.spk.questionnaire.questions.database.AppDatabase;
18 | import com.spk.questionnaire.questions.questionmodels.AnswerOptions;
19 | import com.spk.questionnaire.questions.questionmodels.QuestionsItem;
20 |
21 | import java.util.ArrayList;
22 | import java.util.List;
23 | import java.util.Objects;
24 |
25 | import androidx.annotation.NonNull;
26 | import androidx.core.content.ContextCompat;
27 | import androidx.fragment.app.Fragment;
28 | import androidx.fragment.app.FragmentActivity;
29 | import io.reactivex.Observable;
30 | import io.reactivex.Observer;
31 | import io.reactivex.android.schedulers.AndroidSchedulers;
32 | import io.reactivex.disposables.Disposable;
33 | import io.reactivex.schedulers.Schedulers;
34 |
35 | /**
36 | * This fragment provide the Checkbox/Multiple related Options/Choices.
37 | */
38 | public class CheckBoxesFragment extends Fragment
39 | {
40 | private final ArrayList checkBoxArrayList = new ArrayList<>();
41 | private int atLeastOneChecked = 0;
42 | private FragmentActivity mContext;
43 | private Button nextOrFinishButton;
44 | //private Button previousButton;
45 | private TextView questionCBTypeTextView;
46 | private LinearLayout checkboxesLinearLayout;
47 | private AppDatabase appDatabase;
48 | private String questionId = "";
49 | private int currentPagePosition = 0;
50 | private int clickedCheckBoxPosition = 0;
51 | private String qState = "0";
52 |
53 | public CheckBoxesFragment()
54 | {
55 | // Required empty public constructor
56 | }
57 |
58 | @Override
59 | public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
60 | {
61 | ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_check_boxes, container, false);
62 |
63 | appDatabase = AppDatabase.getAppDatabase(getActivity());
64 |
65 | nextOrFinishButton = rootView.findViewById(R.id.nextOrFinishButton);
66 | //previousButton = rootView.findViewById(R.id.previousButton);
67 | questionCBTypeTextView = rootView.findViewById(R.id.questionCBTypeTextView);
68 | checkboxesLinearLayout = rootView.findViewById(R.id.checkboxesLinearLayout);
69 |
70 | nextOrFinishButton.setOnClickListener(v -> {
71 | if (currentPagePosition == ((QuestionActivity) mContext).getTotalQuestionsSize())
72 | {
73 | /* Here, You go back from where you started OR If you want to go next Activity just change the Intent*/
74 | Intent returnIntent = new Intent();
75 | mContext.setResult(Activity.RESULT_OK, returnIntent);
76 | mContext.finish();
77 |
78 | } else
79 | {
80 | ((QuestionActivity) mContext).nextQuestion();
81 | }
82 | });
83 | //previousButton.setOnClickListener(view -> mContext.onBackPressed());
84 |
85 | return rootView;
86 | }
87 |
88 | /*This method get called only when the fragment get visible, and here states of checkbox(s) retained*/
89 | @Override
90 | public void setUserVisibleHint(boolean isVisibleToUser)
91 | {
92 | super.setUserVisibleHint(isVisibleToUser);
93 |
94 | atLeastOneChecked = 0;
95 |
96 | if (isVisibleToUser)
97 | {
98 | for (int i = 0; i < checkBoxArrayList.size(); i++)
99 | {
100 | CheckBox checkBox = checkBoxArrayList.get(i);
101 | String cbPosition = String.valueOf(i);
102 |
103 | String[] data = new String[]{questionId, cbPosition};
104 | Observable.just(data)
105 | .map(this::getTheStateOfCheckBox)
106 | .subscribeOn(Schedulers.io())
107 | .observeOn(AndroidSchedulers.mainThread())
108 | .subscribe(new Observer()
109 | {
110 | @Override
111 | public void onSubscribe(Disposable d)
112 | {
113 |
114 | }
115 |
116 | @Override
117 | public void onNext(String s)
118 | {
119 | qState = s;
120 | }
121 |
122 | @Override
123 | public void onError(Throwable e)
124 | {
125 |
126 | }
127 |
128 | @Override
129 | public void onComplete()
130 | {
131 | if (qState.equals("1"))
132 | {
133 | checkBox.setChecked(true);
134 | atLeastOneChecked = atLeastOneChecked + 1;
135 |
136 | if (!nextOrFinishButton.isEnabled())
137 | {
138 | nextOrFinishButton.setEnabled(true);
139 | }
140 | } else
141 | {
142 | checkBox.setChecked(false);
143 | }
144 | }
145 | });
146 | }
147 | }
148 | }
149 |
150 | private String getTheStateOfCheckBox(String[] data)
151 | {
152 | return appDatabase.getQuestionChoicesDao().isChecked(data[0], data[1]);
153 | }
154 |
155 | private void saveActionsOfCheckBox()
156 | {
157 | for (int i = 0; i < checkBoxArrayList.size(); i++)
158 | {
159 | if (i == clickedCheckBoxPosition)
160 | {
161 | CheckBox checkBox = checkBoxArrayList.get(i);
162 | if (checkBox.isChecked())
163 | {
164 | atLeastOneChecked = atLeastOneChecked + 1;
165 |
166 | String cbPosition = String.valueOf(checkBoxArrayList.indexOf(checkBox));
167 |
168 | String[] data = new String[]{"1", questionId, cbPosition};
169 | insertAnswerInDatabase(data);
170 |
171 | } else
172 | {
173 | atLeastOneChecked = atLeastOneChecked - 1;
174 | if (atLeastOneChecked <= 0)
175 | atLeastOneChecked = 0;
176 |
177 | String cbPosition = String.valueOf(checkBoxArrayList.indexOf(checkBox));
178 |
179 | String[] data = new String[]{"0", questionId, cbPosition};
180 | insertAnswerInDatabase(data);
181 | }
182 | }
183 | }
184 |
185 | if (atLeastOneChecked != 0)
186 | {
187 | nextOrFinishButton.setEnabled(true);
188 | } else
189 | {
190 | nextOrFinishButton.setEnabled(false);
191 | }
192 | }
193 |
194 | private void insertAnswerInDatabase(String[] data)
195 | {
196 | Observable.just(data)
197 | .map(this::insertingInDb)
198 | .subscribeOn(Schedulers.io())
199 | .subscribe();
200 | }
201 |
202 | private String insertingInDb(String[] data)
203 | {
204 | appDatabase.getQuestionChoicesDao().updateQuestionWithChoice(data[0], data[1], data[2]);
205 | return "";
206 | }
207 |
208 | @Override
209 | public void onActivityCreated(Bundle savedInstanceState)
210 | {
211 | super.onActivityCreated(savedInstanceState);
212 |
213 | mContext = getActivity();
214 | QuestionsItem checkBoxTypeQuestion = null;
215 |
216 | if (getArguments() != null)
217 | {
218 | checkBoxTypeQuestion = getArguments().getParcelable("question");
219 | questionId = String.valueOf(checkBoxTypeQuestion != null ? checkBoxTypeQuestion.getId() : 0);
220 | currentPagePosition = getArguments().getInt("page_position") + 1;
221 | }
222 |
223 | questionCBTypeTextView.setText(checkBoxTypeQuestion != null ? checkBoxTypeQuestion.getQuestionName() : "");
224 |
225 | /*Disable the button until any choice got selected*/
226 | nextOrFinishButton.setEnabled(false);
227 |
228 | List checkBoxChoices = Objects.requireNonNull(checkBoxTypeQuestion).getAnswerOptions();
229 |
230 | checkBoxArrayList.clear();
231 |
232 | for (AnswerOptions choice : checkBoxChoices)
233 | {
234 | CheckBox checkBox = new CheckBox(mContext);
235 |
236 | checkBox.setText(choice.getName());
237 | checkBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
238 | checkBox.setTextColor(ContextCompat.getColor(mContext, R.color.grey));
239 | checkBox.setPadding(10, 40, 10, 40);
240 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
241 | params.leftMargin = 25;
242 |
243 | View view = new View(mContext);
244 | view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.divider));
245 | view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
246 |
247 | checkboxesLinearLayout.addView(checkBox, params);
248 | checkboxesLinearLayout.addView(view);
249 | checkBoxArrayList.add(checkBox);
250 |
251 | checkBox.setOnClickListener(view1 -> {
252 | CheckBox buttonView = (CheckBox) view1;
253 | clickedCheckBoxPosition = checkBoxArrayList.indexOf(buttonView);
254 | saveActionsOfCheckBox();
255 | });
256 |
257 | /*As user comes back for any modification in choices, "setUserVisibleHint" fragment lifecycle method get called, and "checkBox.setChecked(true)"
258 | * statement will be executed as many times as previously user checked.
259 | * On that, this below block will get executed automatically,
260 | * where this method(saveActionsOfCheckBox()) also executed which is unnecessary.
261 | * That's why we follow "setOnClickListener" instead of "setOnCheckedChangeListener".*/
262 |
263 | /*checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
264 | {
265 | @Override
266 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
267 | {
268 | clickedCheckBoxPosition = checkBoxArrayList.indexOf(buttonView);
269 | saveActionsOfCheckBox();
270 | }
271 | });*/
272 | }
273 |
274 | /* If the current question is last in the questionnaire then
275 | the "Next" button will change into "Finish" button*/
276 | if (currentPagePosition == ((QuestionActivity) mContext).getTotalQuestionsSize())
277 | {
278 | nextOrFinishButton.setText(R.string.finish);
279 | } else
280 | {
281 | nextOrFinishButton.setText(R.string.next);
282 | }
283 | }
284 | }
--------------------------------------------------------------------------------
/LICENCE.txt:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
--------------------------------------------------------------------------------