Not its dropdown view, but the TextView view where our choice will appear, that looks like an EditText.
I'm using custom adapter, and I've tried to set it in adapter constructor, and then throw it back to parent using super, but the EditText view is still using the style in xml.
The only view-related I override was getView:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view;
//I do something with TextView here, but won't affect the problem
return view;
}
Then I use the adapter like this
adapterAutoComplete = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterAutoComplete.setDropDownViewResource(
R.layout.custom_dropdown_layout);
autoComplete.setAdapter(adapterAutoComplete);
Resulted in just how I've said, its EditText view used a style I used in xml and totally abandoned R.layout.custom_layout_item.
What did I do wrong?
Related
I have this:
ArrayAdapter<String> spinnerAdapter=new ArrayAdapter<String>(context,R.layout.spinneritem,arrayListWithData);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Inside the R.layout.spinneritem I have only a TextView. I need to access that TextView to set a custom typeFace to it.
Is there any way I can get a reference to the TextView, so I can set the typeface programmatically to it?
Thank you.
You need to implement a custom ArrayAdapter and change the typeface inside the
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
or
#Override
public View getView(int position, View convertView, ViewGroup parent)
method, based on your needs.
If you need to simply change the font of your TextView, you can follow the Android guide on How to change font in XML in the developer site
So I use one adapter for my Spinner and AutoCompleteTextView, which extends ArrayAdapter<Object>. For View-related method, I only override getView.
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view;
//I do something with TextView here, but won't affect the problem
return view;
}
Now, both declared like this:
adapterSpinner = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterService.setDropDownViewResource(
R.layout.custom_dropdown_layout);
spinner.setAdapter(adapterSpinner);
adapterAutoComplete = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterAutoComplete.setDropDownViewResource(
R.layout.custom_dropdown_layout);
autoComplete.setAdapter(adapterAutoComplete);
The result confusing me:
AutoCompleteTextView
Applies custom_layout_item to its dropdown View instead, and instead applies a style I gave in its element in xml to its TextView/EditText layout. It ignores custom_dropdown_layout I've set.
Spinner
Applies correctly all of them, its TextView view using custom_layout_item, and its dropdown view using custom_dropdown_layout. Though it ignores the style I gave to it in xml.
Functional, both works fine. But from UI, quite a mess... Why did it happen?
I am using the following code to setup my spinner :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.country_code_spinner_item,
countrySpinnerOptions){
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextColor(Color.parseColor("#808080"));
return v;
}
};
This assigns the default color of the text to "#808080"
Now i have also implemented setOnTouchListener method for this spinner. What i want is although initially the color text of the spinner is 808080. But after the spinner is touched and a new text is chosen from a list. The color now permanently changes to BLACK.
How do i implement this ?
Thanks
after a long click on a list view item (a list view row contains of only one TextView), a dynamically created EditText is supposed to appear in the corresponding list view row. How do I manage to do do this?
Thanks
You'll have to extend an ArrayAdapter (if you haven't done yet), and do that in the getView() method.
The second of this method's parameters is a View (usually called convertView, but not necessarily). Speaking very vaguely, this View represents actually the current row's layout, so you'll need to add the EditText into this method.
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LinearLayout rootLinearLayout = (LinearLayout) convertView.findViewById(R.id.root_ll);
EditText et = new EditText(this);
// Set additional parameters to et
...
rootLinearLayout.addView(et);
...
return convertView;
}
I have several spinners that I have created a custom ArrayAdapter for so I can change the drop down menu look. I want to manipulate the view depending on what spinner the dropdown belongs to. I thought I would be able to do something like parent.getTag() but it is returning null.
The custom array adapter looks like:
class BackgroundColorAdapter extends ArrayAdapter<String> {
BackgroundColorAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
if(parent.getTag().equals("background"){
//Do custom stuff here
}
return(row);
}
}
and I'm setting the tag:
settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());
I think I'm confused how the view hierarchy works but it seems logical that the parent of the spinner drop down would be the spinner. Anyone know how I can find out what spinner the drop down belongs to in getDropDownView?
edit: made the settingsSpinner a single spinner instead of an array of spinners to make it less confusing
Eventually got this to work, here is the code for example that changes the text font for each item in the drop down.
class TextSizeAdapter extends ArrayAdapter<String> {
TextSizeAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textSizes);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
TextView text = (TextView)row.findViewById(R.id.item_text);
text.setTextSize(TypedValue.COMPLEX_UNIT_PX,appState.FONTSIZES[position]);
RadioButton radio = (RadioButton)row.findViewById(R.id.item_radio);
if(settingsSpinners[2].getSelectedItemPosition() == position){
radio.setChecked(true);
}else{
radio.setChecked(false);
}
return(row);
}
}
I'm unfamiliar with getDropDownView(), and don't know why you use it. Documentation for getDropDownView() states the following about the parent:
parent the parent that this view will eventually be attached to
This doesn't sound like the 'parent' you are looking for...
Since the 'parent' in the getView() call is indeed a Spinner, you could use that to store an instance variable of the parent like below:
public Spinner mParent = null;
public View getView (int position, View convertView, ViewGroup parent)
{
this.mParent = parent;
return super.getView(position, convertView, parent);
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // Your code here -> but use 'mParent'
}
I haven't tried, but maybe it's a workaround to get what you need. Please let me know if you found the solution.