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?
Related
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?
I have a listView that uses the
SimpleAdapter
Each element of this list contains 2 textView
I wanna color(change the backgroud color) an item of this list, how can I do?
To change the background color of the whole view (not just the textview) and using a very simplistic approach you would have this call:
private void setupAdapter() {
SimpleAdapter adapter = new SimpleAdapter(this, data, resource, from, to) {
public View getView(int position, View convertView, ViewGroup parent) {
View superView = super.getView(position, convertView, parent);
superView.setBackgroundColor(R.color.black); // or whatever color
return superView;
};
};
// use the adapter as: myListView.setAdapter(adapter);
}
this could be done using android selector (xml). see the example here
One way to create custom SimpleAdapter class which overrides getView method and there you make an background change depending - I guess - on some condition.
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.
How can I set the height of the spinner? I mean the dropdown items's height.
For I found in SDK12 my spinner will show a long long item list to show the dropdown items.
But it shows to long , how can i set the height?
You can call setDropDownViewResource method and pass corresponding layout, on ArrayAdapter.
In this example im setting simple_spinner_dropdown_item resource which is higher than regular and has a radiobutton. I think you can pass your custom layout to.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
And in case you are making custom layout, you'll have to declare TextView as root element in order to adapter fill it with text.
Maybe it could help..
ArrayAdapter yourSpinnerAdapter = new ArrayAdapter(this,
R.layout.spinner_item, yourItem) {
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
convertView = super.getDropDownView(position, convertView,
parent);
convertView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams p = convertView.getLayoutParams();
p.height = 100; // set the height
convertView.setLayoutParams(p);
return convertView;
}
};
I'm trying to change the font of a TextView in my ArrayAdapter. The font chantelli_antiqua.ttf is in the assets folder.
Here is my Java code:
listItemAdapter = new ArrayAdapter<MenuItem>(this, R.layout.listitem, menuItems);
Typeface font = Typeface.createFromAsset(getAssets(), "chantelli_antiqua.ttf");
TextView v = (TextView)listItemAdapter.getView(0, null, null);
v.setTypeface(font);
xml for the listitem layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="30sp"
/>
I'm quite sure the problem lies with the Adapter.getView(int, View, ViewGroup) method. I just don't really understand what to pass as variables and tried null. But this doesn't do what I would like it to.
How to change the font of the TextView in the Adapter to the custom font?
Update
According to Pixie's suggestion I created a MenuItemAdapter which extends ArrayAdapter<MenuItem>:
public class MenuItemAdapter extends ArrayAdapter<MenuItem>
{
private Typeface font;
public MenuItemAdapter(Context context, int textViewResourceId, List<MenuItem> objects)
{
super(context, textViewResourceId, objects);
font = Typeface.createFromAsset(context.getAssets(), "chantelli_antiqua.ttf");
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
((TextView)view).setTypeface(font);
return super.getView(position, view, viewGroup);
}
}
And changed my java code to:
listItemAdapter = new MenuItemAdapter(this, R.layout.listitem, menuItems);
But now my app crashes after the onCreate of the ListActivity, but before hitting the breakpoint in getView(...), I haven't been able to figure out yet why. Any suggestion?
Update2
Changed the code for getView(...) to:
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
View v = super.getView(position, view, viewGroup);
((TextView)v).setTypeface(font);
return v;
}
and this works. :)
You shouldn't call the getView() method of your adapter. The ListView does this for you. You have to extend the ArrayAdapter class and override the getView() method instead. In this method you have to inflate a new view or re-use convertView and set the typeface for this view.
I think the problem is in return super.getView(position, view, viewGroup); at the end of getView() method.
I think it should be like this
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
TextView tv = ((TextView)view).setTypeface(font);
tv.setText(<String> getItem());
return tv;
}
please note this code is example I didn't try it now but I made custom arrayAdapter before and it was something like that.
Here is a tutorial describing how to create custom arrayAdapter.