Adding background color and image to spinner in android xamarin

I am trying to add a gray background color to a spinner and its itemrow, I want to change the text color to blue and want to put the image to the right of the counter. I am currently getting white in the note and black in the tab device, I am very new to android, please help me.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/moviesSpinner"
        android:prompt="@string/movie_prompt" />
    <ImageView
        android:src="@android:drawable/Icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imageView1" />
</LinearLayout>

itemrow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#ffffff"
    android:padding="3dip">
    <TextView
        android:padding="3dip"
        android:layout_marginTop="2dip"
        android:textColor="#C11B17"
        android:textStyle="bold"
        android:id="@+id/company"
        android:layout_marginLeft="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

mainActivity.cs

var moviesSpinner1 = FindViewById<Spinner>(Resource.Id.moviesSpinner);
            moviesSpinner1.Adapter = new MoviesAdapter(this, MoviesRepository.Movies);
+4
source share
1 answer
public class SpinnerAdapter extends ArrayAdapter<SpinnerItem> {
    private Context mContext;
    private ArrayList<SpinnerItem> listState;

    public SpinnerAdapter(Context context, int resource,
            ArrayList<SpinnerItem> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.listState = objects;
    }

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

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

    @SuppressLint("InflateParams")
    public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDown) {
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater layoutInflator = LayoutInflater.from(mContext);
            convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
                    null);
            holder = new ViewHolder();
            holder.mTextView = (TextView) convertView.findViewById(R.id.text);
            holder.mCheckedImage = (ImageView) convertView
                    .findViewById(R.id.checkbox);
            holder.mBgLayout= (RelativeLayout) convertView
                    .findViewById(R.id.bg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

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

        if (isDropDown) {
        if (listState.get(position).isSelected()) {
             holder.mCheckedImage.setVisibility(View.VISIBLE);
             holder.mBgLayout.setBackgroundColor(android.Color.BLUE); // not sure if the syntax is correct. you need to check this line
        } else {
             holder.mCheckedImage.setVisibility(View.INVISIBLE);
             holder.mBgLayout.setBackgroundColor(android.Color.YELLOW); // not sure if the syntax is correct. you need to check this line
        }
}

        return convertView;
    }

    private class ViewHolder {
        private TextView mTextView;
        private ImageView mCheckedImage;
        private RelativeLayout mBgLayout;
    }

    public void setSpinnerAdapter(ArrayList<SpinnerItem> spinnerItems) {

        this.listState = spinnerItems;
        notifyDataSetChanged();
    }
}


private void setBottelCountData() {
        final String[] select_qualification = { "", "1", "2",
                "3" };

        bottelCountList = new ArrayList<>();

        for (int i = 0; i < select_qualification.length; i++) {
            SpinnerItem spinnerItem = new SpinnerItem();
            spinnerItem.setTitle(select_qualification[i]);
            if (i == 2) {

                spinnerItem.setSelected(false);
            } else {
                spinnerItem.setSelected(false);
            }
            bottelCountList.add(spinnerItem);
        }

        bottelCountAdapter = new SpinnerAdapter(getActivity(), 0,
                bottelCountList);
        bottelCountAdapter.setDropDownViewResource(R.layout.spinner_item_layout);
        bottel_Count_Spinner.setAdapter(bottelCountAdapter);
        bottel_Count_Spinner.setSelection(3);
    }



bottel_Count_Spinner
                .setOnItemSelectedListener(new OnItemSelectedListener() {

                    public void onItemSelected(AdapterView<?> parent,
                            View view, int position, long id) {
                        if (position == 0) {
                            for (int count = 0; count < bottelCountList.size(); count++) {
                                bottelCountList.get(count).setSelected(false);
                            }
                            noOfBottel = 0;
                            return;
                        }
                        bottelCountList.get(position).setSelected(true);
                        for (int count = 0; count < bottelCountList.size(); count++) {
                            if (position != count) {
                                bottelCountList.get(count).setSelected(false);
                            }
                        }
                        bottel_Count_Spinner.setPrompt("Hello");
                        bottelCountAdapter.setSpinnerAdapter(bottelCountList);
                        noOfBottel = Integer.parseInt(bottelCountList.get(
                                position).getTitle());
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });

use this xml

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

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/checkbox"
        android:layout_centerVertical="true"
        android:padding="10dp"/>

    <ImageView
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:src="@android:drawable/checkbox_on_background"
        android:contentDescription="@string/app_name"
        android:layout_marginLeft="10dp"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Hope this helps you. if you find any error, report

0
source

All Articles