Get View reference in a custom spinner adapter - android

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

Related

How to change AutoCompleteTextView's EditText's view?

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?

Using same custom adapter for AutoCompleteTextView and Spinner

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?

Android ListView with custom view - get elements

I create a list view with this tutorial - http://sunil-android.blogspot.com/2013/04/android-listview-checkbox-example.html
Now I have a questions:
1. How to create a button like "Check All"/"Uncheck All" ?
2. How to have access to some view element (like edittext) in a single row ?
your answer is in the tutorial as well...
Check the tutorial link again. There is a private class MyCustomAdapter extends ArrayAdapter<country>{} which is your answer.
When you bind your data to listview using adapter (in this case MyCustomAdapter is your adapter), for each element of your data array the #Override public View getView(int position, View convertView, ViewGroup parent) { } method will fire.
View convertView parameter in the #Override public View getView(int position, View convertView, ViewGroup parent) { } method is a view for each single row of your listview. If you have define xml template for your listview row with id for each element in a row, you can find element using...
TextView code = (TextView) convertView.findViewById(R.id.code);
CheckBox name = (CheckBox) convertView.findViewById(R.id.checkBox);
Here code and name are TextBox & CheckBox of the view from specific row.
So run through the example tutorial again and I think you probably would get your answer.

How to get the spinner id or tag from getDropDownView method in android

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.

Custom spinner dialog for Android

How do I do a custom theme android spinner dialog?
This blog has an example that probably covers your question:
http://www.mokasocial.com/2011/03/easily-create-a-default-custom-styled-spinner-android/
Edit:
These blog posts has a pretty similar answer:
http://karanbalkar.com/2013/07/tutorial-39-create-custom-spinner-in-android/
http://stephenpengilley.blogspot.no/2013/01/android-custom-spinner-tutorial.html
The key to customizing an Android Spinner is to create a custom resource for the spinner rows, and then passing that row layout into a custom adapter. In the custom adapter, one can override the following two functions:
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
// Custom view generation
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
// Custom view generation
}
To determine how the Android Spinner will look to the user.

Categories

Resources