Skip to content

Commit

Permalink
Merge pull request #5 from nipuns/master
Browse files Browse the repository at this point in the history
Spinner
  • Loading branch information
vijayrawatsan committed May 31, 2015
2 parents e704fa3 + 9ce2859 commit 1d8c248
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 2 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ value - will be the path of chosen image on external storage
}
]
}

```

key - must be unique in that particular step.

type - must be check_box for CheckBox.
Expand All @@ -137,6 +139,25 @@ text(in options) - text fot the CheckBox.

value(in options) - true/false.

#### Spinner
```json
{
"key":"name",
"type":"spinner",
"hint":"Name Thy House"
"values":["Stark", "Targeriyan", "Lannister"]
}
```

key - must be unique in that particular step.

type - must be spinner

hint - hint for Spinner.

values - Array of Strings.

value - will be the value present in the spinner after completion of wizard

#### RadioButton (can be used for single/multiple RadioButtons)

Expand Down Expand Up @@ -441,6 +462,7 @@ Contributions welcome via Github pull requests.

- [material](https://github.com/rey5137/material)
- [MaterialEditText](https://github.com/rengwuxian/MaterialEditText)
- [MaterialSpinner](https://github.com/ganfra/MaterialSpinner)

Thanks!

Expand Down
6 changes: 5 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ android {
}
}
repositories {
maven {url "https://clojars.org/repo/"}
maven { url "https://clojars.org/repo/" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.github.rey5137:material:1.1.0'
compile 'com.rengwuxian.materialedittext:library:2.0.3'
compile('com.github.ganfra:material-spinner:1.1.0') {
exclude group: 'com.nineoldandroids', module: 'library'
exclude group: 'com.android.support', module: 'appcompat-v7'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class JsonFormConstants {
public static final String LABEL = "label";
public static final String CHOOSE_IMAGE = "choose_image";
public static final String OPTIONS_FIELD_NAME = "options";
public static final String SPINNER = "spinner";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -274,4 +275,14 @@ public static JsonFormFragment getFormFragment(String stepName) {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
presenter.onCheckedChanged(buttonView, isChecked);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
presenter.onItemSelected(parent, view, position, id);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.vijay.jsonwizard.widgets.ImagePickerFactory;
import com.vijay.jsonwizard.widgets.LabelFactory;
import com.vijay.jsonwizard.widgets.RadioButtonFactory;
import com.vijay.jsonwizard.widgets.SpinnerFactory;

/**
* Created by vijay on 5/19/15.
Expand All @@ -41,6 +42,7 @@ private void registerWidgets() {
map.put(JsonFormConstants.CHECK_BOX, new CheckBoxFactory());
map.put(JsonFormConstants.RADIO_BUTTON, new RadioButtonFactory());
map.put(JsonFormConstants.CHOOSE_IMAGE, new ImagePickerFactory());
map.put(JsonFormConstants.SPINNER, new SpinnerFactory());
}

public List<View> fetchFormElements(String stepName, Context context, JSONObject parentJson, CommonListener listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import android.view.View;
import android.widget.CompoundButton;
import android.widget.Spinner;

/**
* Created by vijay on 5/17/15.
*/
public interface CommonListener extends View.OnClickListener, CompoundButton.OnCheckedChangeListener {
public interface CommonListener extends View.OnClickListener, CompoundButton.OnCheckedChangeListener, Spinner.OnItemSelectedListener {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -149,4 +150,12 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
}
}
}

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String parentKey = (String) parent.getTag(R.id.key);
if (position > 0) {
String value = (String) parent.getItemAtPosition(position + 1);
getView().writeValue(mStepName, parentKey, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.vijay.jsonwizard.widgets;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.rey.material.util.ViewUtil;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.customviews.GenericTextWatcher;
import com.vijay.jsonwizard.interfaces.CommonListener;
import com.vijay.jsonwizard.interfaces.FormWidgetFactory;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import fr.ganfra.materialspinner.MaterialSpinner;

/**
* Created by nipun on 30/05/15.
*/
public class SpinnerFactory implements FormWidgetFactory {

@Override
public List<View> getViewsFromJson(String stepName, Context context, JSONObject jsonObject, CommonListener listener) throws Exception {
List<View> views = new ArrayList<>(1);
MaterialSpinner spinner = (MaterialSpinner) LayoutInflater.from(context).inflate(R.layout.item_spinner, null);

String hint = jsonObject.optString("hint");
if (!TextUtils.isEmpty(hint)) {
spinner.setHint(jsonObject.getString("hint"));
spinner.setFloatingLabelText(jsonObject.getString("hint"));
}

spinner.setId(ViewUtil.generateViewId());

spinner.setTag(R.id.key, jsonObject.getString("key"));
spinner.setTag(R.id.type, jsonObject.getString("type"));

JSONArray valuesJson = jsonObject.optJSONArray("values");
String[] values = null;
if (valuesJson != null && valuesJson.length() > 0) {
values = new String[valuesJson.length()];
for (int i = 0; i < valuesJson.length(); i++) {
values[i] = valuesJson.optString(i);
}
}

if (values != null) {
spinner.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, values));
}

spinner.setOnItemSelectedListener(listener);
views.add(spinner);
return views;
}
}
12 changes: 12 additions & 0 deletions library/src/main/res/layout/item_spinner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<fr.ganfra.materialspinner.MaterialSpinner
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:ms_multiline="false"
app:ms_typeface="font/Roboto-Regular.ttf"
app:ms_arrowColor="?colorPrimaryDark"
app:ms_arrowSize="16dp"
app:ms_alignLabels="true"
app:ms_floatingLabelColor="@color/secondary_text"
/>
6 changes: 6 additions & 0 deletions sample/src/main/assets/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
"key":"chooseImage",
"type":"choose_image",
"uploadButtonText":"Choose"
},
{
"key":"house",
"type":"spinner",
"hint": "Name Thy House",
"values":["Stark", "Targeriyan", "Lannister"]
}
],
"title":"Step 1 of 3",
Expand Down

0 comments on commit 1d8c248

Please sign in to comment.