Question: For my lab I need to create a custom adapter that extends the baseAdapter and is used for a spinner. I created a custom adapter




For my lab I need to create a custom adapter that extends the baseAdapter and is used for a spinner. I created a custom adapter ColorAdapter, which is the second picture, but I am struggling with using it with a spinner. I can't seem to set up the dropDownView. Is there something Im missing? I am new to android studio and dont quite understand how a custom adapter works.
Instructions: Create an application that uses a custom adapter. Your application will allow a user to select a color from a Spinner and once selected, set the background of the activity's layout to the selected color. 1. Create an application with an activity called ColorActivity (your Main activity) 2. Create a custom adapter called ColorAdapter that extends BaseAdapter that will present to the user a set of color options. The views generated by the custom adapter should be simple TextViews, where the text value is the name of the color. The background of the text view should behave as follows: i. When the spinner is in drop-down mode (i.e. the user clicked the drop-down button and the various selection options are being displayed), each textviews' background color should be the same as its displayed text value. ii. When an item is selected, the view's background color should be set to white. 3. When the user selects a color from the Adapter View, the activity's layout background should be set to the selected color. One way to change the background color of the layout, which you will recall is also a view object, is to assign it an ID as you would any regular view object. Then from within your code you can access the layout by passing its ID to findViewById(R.id.layout_id), and from there change it's background color. import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget. BaseAdapter; import android.widget. TextView; public class ColorAdapter extends BaseAdapter { private Context context; private String[] colors; LayoutInflater inflater; public ColorAdapter(Context context, String[] colors) { this.context=context; this.colors=colors; inflater=(LayoutInflater. from( context)); @Override public int getCount() { return colors. Length; @Override public Object getItem(int position) { return colors (position); @Override public long getItemId(int position) { return 0; @Override public View getView(int position, View view, ViewGroup parent) { view =inflater. inflate(R.layout.support_simple_spinner_dropdown_item, root: null); TextView textView = new TextView( context); textView.setText(colors (position]); return textView; import android.os. Bundle; import android.view.View; import android.widget. AdapterView; import android.widget. Spinner; import android.widget. Toast; import android.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String() C={"red, blue, yellow"}; Spinner spinner = findViewById(R.id. spinner); Color Adapter adapteri = new ColorAdapter( context: this,C); adapteri.setDropDownViewResource (android.R.layout. simple_dropdown_item_iline); Cannot resolve method 'setDropDown View Resource(int)' spinner.setAdapter(adapteri); spinner.setOnItemSelectedListener(this); PickAColor2 red, blue yellow Instructions: Create an application that uses a custom adapter. Your application will allow a user to select a color from a Spinner and once selected, set the background of the activity's layout to the selected color. 1. Create an application with an activity called ColorActivity (your Main activity) 2. Create a custom adapter called ColorAdapter that extends BaseAdapter that will present to the user a set of color options. The views generated by the custom adapter should be simple TextViews, where the text value is the name of the color. The background of the text view should behave as follows: i. When the spinner is in drop-down mode (i.e. the user clicked the drop-down button and the various selection options are being displayed), each textviews' background color should be the same as its displayed text value. ii. When an item is selected, the view's background color should be set to white. 3. When the user selects a color from the Adapter View, the activity's layout background should be set to the selected color. One way to change the background color of the layout, which you will recall is also a view object, is to assign it an ID as you would any regular view object. Then from within your code you can access the layout by passing its ID to findViewById(R.id.layout_id), and from there change it's background color. import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget. BaseAdapter; import android.widget. TextView; public class ColorAdapter extends BaseAdapter { private Context context; private String[] colors; LayoutInflater inflater; public ColorAdapter(Context context, String[] colors) { this.context=context; this.colors=colors; inflater=(LayoutInflater. from( context)); @Override public int getCount() { return colors. Length; @Override public Object getItem(int position) { return colors (position); @Override public long getItemId(int position) { return 0; @Override public View getView(int position, View view, ViewGroup parent) { view =inflater. inflate(R.layout.support_simple_spinner_dropdown_item, root: null); TextView textView = new TextView( context); textView.setText(colors (position]); return textView; import android.os. Bundle; import android.view.View; import android.widget. AdapterView; import android.widget. Spinner; import android.widget. Toast; import android.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String() C={"red, blue, yellow"}; Spinner spinner = findViewById(R.id. spinner); Color Adapter adapteri = new ColorAdapter( context: this,C); adapteri.setDropDownViewResource (android.R.layout. simple_dropdown_item_iline); Cannot resolve method 'setDropDown View Resource(int)' spinner.setAdapter(adapteri); spinner.setOnItemSelectedListener(this); PickAColor2 red, blue yellow
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
