metalink
metalink

Reputation: 584

How customize spinner in Android

I need to customize spinner in Android.
I have my simple Adapter:

public class MonthsSpinnerAdapter extends BaseAdapter {

private Activity context;
private String [] itemList;

public MonthsSpinnerAdapter(Activity context,String [] monthArray) {
    this.context=context;
    this.itemList=monthArray;
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return itemList[position];
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_item, parent,
            false);
    RobotoTextView make = (RobotoTextView) row.findViewById(R.id.spinner_title);
    make.setText(itemList[position]);
    Log.d("SpinnerA", "getView");
    return row;
}


public View getDropDownView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_dropdown_item, parent,
            false);
    RobotoTextView month = (RobotoTextView) row.findViewById(R.id.spinner_month);
    month.setText(itemList[position]);
    RobotoTextView year = (RobotoTextView) row.findViewById(R.id.spinner_year);
    year.setText(context.getString(R.string.year));
    Log.d("SpinnerA", "getDropDownView");
    return row;
}

}

But when I added it to my spinner and run the up, I don't see anything in spinner.

MonthsSpinnerAdapter adapter = new MonthsSpinnerAdapter(GeneralActivity.this
                                , getResources().getStringArray(R.array.month_list));
spinner.setAdapter(adapter);

I think it will be work nice, but I dont know where I have mistake. This is example what I see, when run app: enter image description here
And at this picture, what I need:enter image description here

And this my xml files if you need:

<com.devspark.robototextview.widget.RobotoTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:typeface="roboto_medium"
    android:padding="5dp"
    android:textColor="#fff"
    android:id="@+id/spinner_title"
    android:text="month"
    android:background="@color/primary_color"
    xmlns:android="http://schemas.android.com/apk/res/android" />

And DropDown layout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

<com.devspark.robototextview.widget.RobotoTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:typeface="roboto_medium"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="12dp"
    android:id="@+id/spinner_month"
    android:text="month"
    android:textColor="#545454"
    android:background="#fafafa" />

    <com.devspark.robototextview.widget.RobotoTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:typeface="roboto_regular"
        android:layout_marginTop="16dp"
        android:layout_marginRight="12dp"
        android:layout_alignParentRight="true"
        android:text="year"
        android:id="@+id/spinner_year"
        />

</RelativeLayout>

Help me please with my problem

Upvotes: 0

Views: 162

Answers (1)

Sunny
Sunny

Reputation: 137

Try to return itemList.length in getCount function of your adapter.

@Override
public int getCount() {
    return itemList.length;
}

Upvotes: 1

Related Questions