0

I am creating a custom spinner in my application. The items are displaying perfectly, but the issue is, the list items are not getting scrolled.

Please refer the image below. In the image below, I am using plenty of countries but not being able to scroll for other country options.

Custom dropdown image link

This is my complete spinner code below:

main_activity.xml

        <android.support.percent.PercentRelativeLayout
            android:id="@+id/layoutSpinner"
            android:layout_centerHorizontal="true"
            android:background="@drawable/bg_spinner_countrycode"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            app:layout_widthPercent="25%">

            <Spinner
                android:id="@+id/spCountryCode"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </android.support.percent.PercentRelativeLayout>

MainActivity.java:

Spinner spCountryCode = (Spinner) findViewById(R.id.spCountryCode);

CountryCodeAdapter countryCodeAdapter = new CountryCodeAdapter(LoginScreenActivity.this, modelList);
spCountryCode.setAdapter(countryCodeAdapter);

CountryCodeAdapter.java:

public class CountryCodeAdapter extends ArrayAdapter<CountryCodeModel> {

    Activity activity;
    List<CountryCodeModel> itemList;
    LayoutInflater inflater;


    public CountryCodeAdapter(Activity activity, List<CountryCodeModel> itemList) {
        super(activity, R.layout.country_code_spinner_layout, itemList);
        this.activity = activity;
        this.itemList = itemList;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = inflater.inflate(R.layout.country_code_spinner_layout, parent, false);

        ImageView imgFlag = (ImageView) itemView.findViewById(R.id.imgFlag);
        TextView lblCountryName = (TextView) itemView.findViewById(R.id.lblCountryName);
        TextView lblCountryCode = (TextView) itemView.findViewById(R.id.lblCountryCode);

        Picasso.with(activity).load(itemList.get(position).getStrCountryFlagUrl()).into(imgFlag);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgFlag.getLayoutParams();
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        imgFlag.setLayoutParams(lp);

        //lblCountryName.setText(itemList.get(position).getStrCountryName());
        lblCountryName.setVisibility(View.GONE);
        //lblCountryCode.setText("(" + itemList.get(position).getStrCountryCode() + ")");
        lblCountryCode.setVisibility(View.GONE);


        return itemView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View itemView = inflater.inflate(R.layout.country_code_spinner_layout, parent, false);

        ImageView imgFlag = (ImageView) itemView.findViewById(R.id.imgFlag);
        TextView lblCountryName = (TextView) itemView.findViewById(R.id.lblCountryName);
        TextView lblCountryCode = (TextView) itemView.findViewById(R.id.lblCountryCode);

        Picasso.with(activity).load(itemList.get(position).getStrCountryFlagUrl()).into(imgFlag);
        lblCountryName.setText(itemList.get(position).getStrCountryName());
        lblCountryCode.setText(itemList.get(position).getStrCountryCode());

        return itemView;
    }
}

Also, I have also tried, countryCodeAdapter.setDropDownViewResource(R.layout.country_code_spinner_layout);, just before spCountryCode.setAdapter(countryCodeAdapter);, but still no success.

Also, if you want to see my custom spinner layout, here is it.

country_code_spinner_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/countryCodeSpinnerDropDownHeight">

    <ImageView
        android:id="@+id/imgFlag"
        android:layout_width="@dimen/countryCodeSpinnerImageSize"
        android:layout_height="@dimen/countryCodeSpinnerImageSize"
        android:layout_marginLeft="@dimen/countryCodeSpinnerMarginLeft"
        android:layout_centerVertical="true"/>

    <com.base.silpre.Font_TextViewOpenSans_SemiBold
        android:id="@+id/lblCountryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imgFlag"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/countryCodeSpinnerMarginLeft"
        android:textSize="@dimen/countryCodeSpinnerTextsSize"
        android:textColor="@android:color/black"/>

    <com.base.silpre.Font_TextViewOpenSans_Italic
        android:id="@+id/lblCountryCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/lblCountryName"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/countryCodeSpinnerMarginLeft"
        android:textSize="@dimen/countryCodeSpinnerTextsSize"
        android:textColor="@android:color/black"/>

</RelativeLayout>

Please revert back if anyone has a solution for scrolling the custom spinner.

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
Sarthak Sharma
  • 137
  • 3
  • 10

1 Answers1

0

Try wrap_content for height

   `< Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>`