-4

I want to take multiple Inputs from the spinner in android. so In succeeding this, I want to allow Spinner to take multiple inputs just like a checkbox. can anyone tell me the way to solving this problem?

I have another problem.

I'm stuck on a problem with "run".

When I click "run" It only installs the previously build Apk to the device.

The matter is It's neither running the Build.gradle nor Building a new Apk in which the changes could reflect on clicking the "run".

Means I have to click the Build Apk button every time and after that, I have to click "run" then It installs the new Apk to the Device.

Please Help me I'm working on an Important project and I don't have much time and I have to deliver it soon. Please.

  • check below solution. It is exact what you want. default spinner cannot provide you what you want, you want to customize your functionality for getting effect of spinner with multiple selection and with spinner effect – Nik Apr 03 '18 at 10:06

2 Answers2

0

you cannot do that with simple spinner which was provided by android,you have to make custom spinner.for more information you can refer this link,in this answer user has used textview and checkbox for multiple choices input.

Normal1One
  • 219
  • 3
  • 11
  • I'm stuck on a problem with "run". When I click "run" It only installs the previously build Apk to the device. The matter is It's neither running the Build.gradle nor Building a new Apk in which the changes could reflect on clicking the "run". Means I have to click the Build Apk button every time and after that, I have to click "run" then It installs the new Apk to the Device. Please Help me I'm working on an Important project and I don't have much time and I have to deliver it soon. Please. – bharat singh Apr 04 '18 at 18:16
0

Using Customization you can achieve your goal (with exact spinner Effect and complete solution as below)

xml file for adding functionality like spinner:

           <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp"
                    android:weightSum="1">

                    <LinearLayout
                        android:id="@+id/linear_combination_of_country"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/tv_com_of_country"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:layout_marginStart="3dp"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:drawablePadding="15dp"
                            android:paddingBottom="5dp"
                            android:paddingTop="10dp"
                            android:text="country"
                            android:textColor="@color/black"
                            android:textSize="16sp" />

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="25dp"
                            android:layout_gravity="center_horizontal|center_vertical"
                            android:layout_weight="0.01"
                            android:paddingTop="5dp"
                            android:src="@drawable/down_arrow" />
                    </LinearLayout>

                    <com.example.Utils.NoDefaultSpinner
                        android:id="@+id/spinner_combination_of_country"
                        android:layout_width="0dp"
                        android:layout_height="0dp" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="0.5dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="1dp"
                        android:background="@color/black" />


                </LinearLayout>

add below in your Activity class:

linear_combination_of_city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                spinner_combination_of_city.performClick();
            }
        });


linear_combination_of_city.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                // Ensure you call it only once
                linear_combination_of_city.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                spinner_combination_of_city.setDropDownWidth(linear_combination_of_city.getWidth() - 30);
                // Here you can get the size :)
            }
        });         

spinner_combination_of_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String item = parent.getItemAtPosition(position).toString();
                tv_com_of_city.setText("" + item);
            }

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

            }
        });

combinationOfCountryList();
FilterMultiSelectAdapter myCountryAdapter = new FilterMultiSelectAdapter(getContext(), 0,
                multiSelectCountryList);
        spinner_combination_of_country.setAdapter(myCountryAdapter);


private void combinationOfCountryList(){
    ArrayList<FamilyModel> multiSelectCountryList = new ArrayList<>();

    List<String> combinationOfCountry = new ArrayList<String>();
    combinationOfCountry.add("UK");
    combinationOfCountry.add("US");
    combinationOfCountry.add("Asia");

    for (int i = 0; i < combinationOfCountry.size(); i++) {
        FamilyModel model = new FamilyModel();
        model.setTitle(combinationOfCountry.get(i));
        model.setSelected(false);
        multiSelectCountryList.add(model);
    }
}   

Your model class

public class FamilyModel {
private String title;
private boolean selected;
private boolean isChild = false;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

public boolean isChild() {
    return isChild;
}

public void setChild(boolean child) {
    isChild = child;
}

}

Adapter class:

public class FilterMultiSelectAdapter extends ArrayAdapter<FamilyModel> {

private Context mContext;
private ArrayList<FamilyModel> familyList;


public FilterMultiSelectAdapter(Context context,  int resource, List<FamilyModel> objects) {
    super(context, resource, objects);
    this.mContext = context;
    this.familyList = (ArrayList<FamilyModel>) objects;
}

@Override
public View getDropDownView(int position, View convertView,
                            ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getCustomView(final int position, View convertView,
                          ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {
        LayoutInflater layoutInflator = LayoutInflater.from(mContext);
        convertView = layoutInflator.inflate(R.layout.design_filter_multi_select_view, null);
        holder = new ViewHolder();
        holder.mTextView = (TextView) convertView
                .findViewById(R.id.family_member_name);
        holder.toggle = (ToggleButton) convertView
                .findViewById(R.id.toggle);

        holder.familyLayout = (LinearLayout) convertView.findViewById(R.id.linear_checkbolx);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.mTextView.setText(familyList.get(position).getTitle());

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    if(familyList.get(position).isChild()){
        layoutParams.setMargins(120, 0, 0, 0);
        holder.familyLayout.setLayoutParams(layoutParams);

    }else{
        layoutParams.setMargins(0, 0, 0, 0);
        holder.familyLayout.setLayoutParams(layoutParams);
    }

    holder.mTextView.setTextColor(ContextCompat.getColor(mContext,R.color.black));

    holder.toggle.setTag(position);
    holder.toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int getPosition = (Integer) buttonView.getTag();
            if (buttonView.isChecked()) {
                familyList.get(getPosition).setSelected(true);
            } else {
                familyList.get(getPosition).setSelected(false);
            }
        }
    });


    holder.familyLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.toggle.isChecked())
                holder.toggle.setChecked(false);
            else
                holder.toggle.setChecked(true);
        }
    });


    if (familyList.get(position).isSelected()) {
        holder.toggle.setChecked(true);
    } else {
        holder.toggle.setChecked(false);
    }

    return convertView;
}

private class ViewHolder {
    private TextView mTextView;
    private ToggleButton toggle;
    private LinearLayout familyLayout;
}

}

design_filter_multi_select_view.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <LinearLayout
        android:id="@+id/linear_checkbolx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:background="@drawable/action_bar_hover"
        android:padding="8dp">

        <ToggleButton
            android:id="@+id/toggle"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/filter_toggle_status"
            android:checked="false"
            android:text=""
            android:textOff=""
            android:textOn="" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/family_member_name"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:gravity="center_vertical"
                    android:text="Hello"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:textColor="@color/reminder_text_color"
                    android:textSize="16sp" />


            </LinearLayout>

        </LinearLayout>


    </LinearLayout>

</RelativeLayout> 

custom Spinner class:

public class NoDefaultSpinner extends Spinner {

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

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

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

@Override
public void setAdapter(SpinnerAdapter orig ) {
    final SpinnerAdapter adapter = newProxy(orig);

    super.setAdapter(adapter);

    try {
        final Method m = AdapterView.class.getDeclaredMethod(
                "setNextSelectedPositionInt",int.class);
        m.setAccessible(true);
        m.invoke(this,-1);

        final Method n = AdapterView.class.getDeclaredMethod(
                "setSelectedPositionInt",int.class);
        n.setAccessible(true);
        n.invoke(this,-1);
    }
    catch( Exception e ) {
        throw new RuntimeException(e);
    }
}

protected SpinnerAdapter newProxy(SpinnerAdapter obj) {
    return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(
            obj.getClass().getClassLoader(),
            new Class[]{SpinnerAdapter.class},
            new SpinnerAdapterProxy(obj));
}



/**
 * Intercepts getView() to display the prompt if position < 0
 */
protected class SpinnerAdapterProxy implements InvocationHandler {

    protected SpinnerAdapter obj;
    protected Method getView;


    protected SpinnerAdapterProxy(SpinnerAdapter obj) {
        this.obj = obj;
        try {
            this.getView = SpinnerAdapter.class.getMethod(
                    "getView",int.class,View.class,ViewGroup.class);
        }
        catch( Exception e ) {
            throw new RuntimeException(e);
        }
    }

    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
        try {
            return m.equals(getView) &&
                    (Integer)(args[0])<0 ?
                    getView((Integer)args[0],(View)args[1],(ViewGroup)args[2]) :
                    m.invoke(obj, args);
        }
        catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected View getView(int position, View convertView, ViewGroup parent)
            throws IllegalAccessException {

        if( position<0 ) {
            final TextView v =
                    (TextView) ((LayoutInflater)getContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                            android.R.layout.simple_spinner_item,parent,false);
            v.setText(getPrompt());
            return v;
        }
        return obj.getView(position,convertView,parent);
    }
}

}

Nik
  • 1,991
  • 2
  • 13
  • 30
  • Please post only relevant solution for the problem and refrain from posting irrelevant code – Anuraag Baishya Apr 03 '18 at 10:21
  • I'm stuck on a problem with "run". When I click "run" It only installs the previously build Apk to the device. The matter is It's neither running the Build.gradle nor Building a new Apk in which the changes could reflect on clicking the "run". Means I have to click the Build Apk button every time and after that, I have to click "run" then It installs the new Apk to the Device. Please Help me I'm working on an Important project and I don't have much time and I have to deliver it soon. Please. – bharat singh Apr 04 '18 at 18:16
  • to avoid rebuild EACH TIME, module project settings -> Dependencies and up or down one of library item -> Apply -> Ok -> Make regenerating R.java. You can also try with File -> Settings (Ctrl+Alt+S) -> Compiler -> Uncheck "Use external build" – Nik Apr 05 '18 at 06:06
  • for more info you can check this link https://stackoverflow.com/a/17269669/5370550 – Nik Apr 05 '18 at 06:08