1

I am an android newbie and have used the code here: https://stackoverflow.com/a/17577310/6412402 to create a custom Multispinner (it works great!) My graphic designer friend hates me/the current layout and wants an iOS-like scroll where options the user has passed fade from view (similar to how a scrollview has a fadingedge length) How do I give this custom spinner a fading edge length similar to this: Fading Scroll

except the Fading scroll image is just for text and i want items in my custom spinner to have this property. Honestly it doesn't have to look like the picture we just need some kind of fading property. Please help! Here is the code in the java class Multispinner:

package packagename;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.util.List;

public class MultiSpinner extends Spinner {

private CharSequence[] entries;
private boolean[] selected;
private MultiSpinnerListener listener;

public MultiSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiSpinner);
    entries = a.getTextArray(R.styleable.MultiSpinner_android_entries);
    if (entries != null) {
        selected = new boolean[entries.length]; // false-filled by default
    }
    a.recycle();
}
private OnMultiChoiceClickListener mOnMultiChoiceClickListener = new OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        selected[which] = isChecked;
    }
};
private DialogInterface.OnClickListener mOnClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // build new spinner text & delimiter management
        StringBuffer spinnerBuffer = new StringBuffer();
        for (int i = 0; i < entries.length; i++) {
            if (selected[i]) {
                spinnerBuffer.append(entries[i]);
                spinnerBuffer.append(", ");
            }
        }

        // Remove trailing comma
        if (spinnerBuffer.length() > 2) {
            spinnerBuffer.setLength(spinnerBuffer.length() - 2);
        }

        // display new text
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_item,
                new String[] { spinnerBuffer.toString() });
        setAdapter(adapter);

        if (listener != null) {
            listener.onItemsSelected(selected);
        }

        // hide dialog
        dialog.dismiss();
    }
};

@Override
public boolean performClick() {
    new AlertDialog.Builder(getContext())
            .setMultiChoiceItems(entries, selected, mOnMultiChoiceClickListener)
            .setPositiveButton(android.R.string.ok, mOnClickListener)
            .show();
    return true;
}

public void setMultiSpinnerListener(MultiSpinnerListener listener) {
    this.listener = listener;
}

public interface MultiSpinnerListener {
    public void onItemsSelected(boolean[] selected);
}
}

and my xml code

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdgeLength="400dp"
android:requiresFadingEdge="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="edu.ucsb.cs.cs185.project.cookey.ScrollingActivity"
tools:showIn="@layout/activity_scrolling">

<packagename.MultiSpinner
    android:id="@+id/multispinner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:entries="@array/multispinner_entries"
    android:fadingEdgeLength="300dp"
    android:requiresFadingEdge="vertical"/>
</android.support.v4.widget.NestedScrollView>
Community
  • 1
  • 1
Nate
  • 11
  • 2

0 Answers0