Skip to content

Commit

Permalink
Make Spinner widget open from start-of-list when nothing is selected (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
iammvaibhav committed Mar 1, 2018
1 parent ff27b5a commit 0343d95
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
@@ -0,0 +1,60 @@
package org.odk.collect.android.views;

import android.content.Context;
import android.support.v7.widget.AppCompatSpinner;
import android.util.AttributeSet;

/**
* Created by vaibhav on 1/3/18.
*/

public class CustomSpinner extends AppCompatSpinner {

private boolean toggleFlag = true;

public CustomSpinner(Context context, AttributeSet attrs,
int defStyle, int mode) {
super(context, attrs, defStyle, mode);
}

public CustomSpinner(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}

public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CustomSpinner(Context context, int mode) {
super(context, mode);
}

public CustomSpinner(Context context) {
super(context);
}

@Override
public int getSelectedItemPosition() {
// this toggle is required because this method will get called in other
// places too, the most important being called for the
// OnItemSelectedListener
if (!toggleFlag && super.getSelectedItemPosition() == getAdapter().getCount() - 1) {
return 0; // get us to the first element
}
return super.getSelectedItemPosition();
}

@Override
public boolean performClick() {
// this method shows the list of elements from which to select one.
// we have to make the getSelectedItemPosition to return 0 so you can
// fool the Spinner and let it think that the selected item is the first
// element
toggleFlag = false;
boolean result = super.performClick();
toggleFlag = true;
return result;
}

}
Expand Up @@ -26,7 +26,6 @@
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import org.javarosa.core.model.SelectChoice;
Expand All @@ -38,6 +37,7 @@
import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.external.ExternalDataUtil;
import org.odk.collect.android.views.CustomSpinner;
import org.odk.collect.android.widgets.interfaces.MultiChoiceWidget;

import java.util.List;
Expand All @@ -52,7 +52,7 @@
@SuppressLint("ViewConstructor")
public class SpinnerWidget extends QuestionWidget implements MultiChoiceWidget {
List<SelectChoice> items;
Spinner spinner;
CustomSpinner spinner;
String[] choices;

public SpinnerWidget(Context context, FormEntryPrompt prompt) {
Expand Down
2 changes: 1 addition & 1 deletion collect_app/src/main/res/layout/spinner_layout.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatSpinner xmlns:android="http://schemas.android.com/apk/res/android"
<org.odk.collect.android.views.CustomSpinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="match_parent"
Expand Down

0 comments on commit 0343d95

Please sign in to comment.