How to use butterknife in my case? - android

In my code, how should i change if i want use butterknife?
Layout list_item is listview, so I can generate butterknife injection, but I don't know how to fix other code, should I use ViewHolder?
public class GangAdapter extends ArrayAdapter<Gang> {
public GangAdapter(Context context, ArrayList<Gang> gangs) {
super(context, 0, gangs);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Gang currentFeature = getItem(position);
ImageView spotImageView = convertView.findViewById(R.id.Image);
spotImageView.setImageResource(currentFeature.getImageResourceId());
TextView featureTextView = convertView.findViewById(R.id.where);
featureTextView.setText(currentFeature.getFeature());
TextView detailTextView = convertView.findViewById(R.id.about);
detailTextView.setText(currentFeature.getExplanation());
return convertView;
}
Here is my list_item.xml.
I think there is no problem. isn't it?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:id="#+id/where"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left" />

You can setup the annotations and the pass the inflated view to while binding as
public class GangAdapter extends ArrayAdapter<Gang> {
#BindView(R.id.Image) ImageView spotImageView;
#BindView(R.id.where) TextView featureTextView ;
#BindView(R.id.about) TextView detailTextView ;
//^^^^^^^^^^^ do the setup
public GangAdapter(Context context, ArrayList<Gang> gangs) {
super(context, 0, gangs);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
ButterKnife.bind(this, convertView);
// ^^^^^^^^^^^
// pass the view with which you want to bind the views
}
Gang currentFeature = getItem(position);
spotImageView.setImageResource(currentFeature.getImageResourceId());
featureTextView.setText(currentFeature.getFeature());
detailTextView.setText(currentFeature.getExplanation());
return convertView;
}
}

Related

Spinner setOnItemSelectedListener not working with custom adapter

I have made a custom spinner to populate a list of image and text that comes from mysql DB
when I tried to select an item from the spinner it doesn't work, even when I debug and put a break point inside the listener it doesn't go in.
My Adapter:
public class SpinnerShopAdapter extends ArrayAdapter<supermarketspinnerGS> {
private Context mcontext;
ArrayList<supermarketspinnerGS> SavedListContent;
public SpinnerShopAdapter(Context context, ArrayList<supermarketspinnerGS> SavedListContent) {
super(context, 0, SavedListContent);
this.mcontext = context;
this.SavedListContent = SavedListContent;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.spinner_supermarket_row, parent, false
);
}
ImageView imvSupermaket = convertView.findViewById(R.id.spinner_supermarket_imageView);
TextView tvSupamrketname = convertView.findViewById(R.id.spinner_supermarket_name);
TextView tvIdShop = convertView.findViewById(R.id.spinner_supermarket_idshop);
supermarketspinnerGS currentItem = getItem(position);
if (currentItem != null) {
Glide.with(context).load(currentItem.getImage()).into(imvSupermaket);
tvSupamrketname.setText(currentItem.getName());
tvIdShop.setText(currentItem.getIdshop());
}
return convertView;
}
}
onCreate code:
spinnerStore = (Spinner) findViewById(R.id.saveditemlist_spinner_storeshead);
adapterHead = new SpinnerShopAdapter(ActivitySavedListItem.this, getListHeadArray);
spinnerStore.setAdapter(adapterHead);
spinnerStore.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
supermarketspinnerGS clickedItem = (supermarketspinnerGS) parent.getItemAtPosition(position);
String clickedshopName = clickedItem.getIdshop();
selectedItemText=getListHeadArray.get(spinnerStore.getSelectedItemPosition()).getIdshop();
Toast.makeText(ActivitySavedListItem.this, clickedshopName + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I really don't know what is wrong and looking forward for your help. If you need more details please feel free to ask me I'll be glad to update my post
EDIT :
XML SPINNER
<LinearLayout
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:background="#drawable/spinner_market_shape"
app:layout_constraintStart_toEndOf="#+id/textView13"
app:layout_constraintTop_toBottomOf="#+id/textView10">
<Spinner
android:id="#+id/saveditemlist_spinner_storeshead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp" />
</LinearLayout>
spinner_supermarket_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/spinner_supermarket_imageView"
android:layout_width="75dp"
android:layout_height="25dp"
android:layout_marginStart="4dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:clickable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/spinner_supermarket_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="invisible"
/>
<TextView
android:id="#+id/spinner_supermarket_idshop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="invisible"
/>
</RelativeLayout >
try to to add clickable=false to all your elements on your xml spinner row
Iam not sure but
I think you must set your spinner item click listener in your spinner custom adapter in initView function
like this:
private View initView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.spinner_supermarket_row, parent, false
);
}
ImageView imvSupermaket = convertView.findViewById(R.id.spinner_supermarket_imageView);
TextView tvSupamrketname = convertView.findViewById(R.id.spinner_supermarket_name);
TextView tvIdShop = convertView.findViewById(R.id.spinner_supermarket_idshop);
supermarketspinnerGS currentItem = getItem(position);
if (currentItem != null) {
Glide.with(context).load(currentItem.getImage()).into(imvSupermaket);
tvSupamrketname.setText(currentItem.getName());
tvIdShop.setText(currentItem.getIdshop());
}
parent.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(position==CUSTOM_POSITION){ /*do something*/}
}
});
return convertView;
}
I hope it works

Why ListView onClick does not work?

Trying to implement a onlick functionality for the android listview but something seems to not working. I tried to find some solutions over the internet and I found that there were recommendation to change the focusable property of imageview and textview elements. Tried it and it did not work. Appreciate if you guys can recommend any other alternatives.
Layout item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="#+id/rootItemId"
android:clickable="true"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable='false'
android:background="#drawable/frame">
<ImageView
android:id="#+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="#string/hello_world"
android:padding="5dp"
android:focusable='false'
android:src="#drawable/ic_launcher"
android:scaleType="fitXY"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:focusable='false'
android:layout_toRightOf="#id/ivIcon">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable='false'
android:textAppearance="#android:style/TextAppearance.Medium"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
list fragment
public class EventsListFragment extends ListFragment{
ArrayList<Event> eventsChildren;
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.e("RedditListingsClick",position + " " + id);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
eventsChildren = new ArrayList<Event>();
EventsChildAdapter adapter = new EventsChildAdapter(inflater.getContext(), eventsChildren);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
list adapter
public class EventsChildAdapter extends ArrayAdapter<Event> {
Context context;
RelativeLayout root;
private static class ViewHolder {
TextView title;
ImageView thumbnail;
}
public EventsChildAdapter(Context context, ArrayList<Event> eventsChildren) {
super(context, R.layout.item_listing, eventsChildren);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Event child = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_listing, null);
//root=(RelativeLayout)convertView.findViewById(R.id.rootItemId);
viewHolder.title = (TextView) convertView.findViewById(R.id.tvTitle);
//viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.ivIcon);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
System.out.println(child.getTitle());
viewHolder.title.setText(child.getTitle());
Picasso.with(this.context).load(child.getThumbnailImageURL()).resize(200, 100).into(viewHolder.thumbnail);
// Return the completed view to render on screen
return convertView;
}
}
In addition to the above, I have already tried adding android:descendantFocusability="blocksDescendants" to relativelayout but still did not work. Finally I tried to make the root relativelayout clickable and removed focus from the other elements and this did not work either.
You can try this:
public class EventsListFragment extends ListFragment implement OnItemClickListener {
...
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// the child callback here.
}
}
and this in onActivityCreated
setListAdapter(Your adapter);
getListView().setOnItemClickListener(this);

How customize spinner in Android

I need to customize spinner in Android.
I have my simple Adapter:
public class MonthsSpinnerAdapter extends BaseAdapter {
private Activity context;
private String [] itemList;
public MonthsSpinnerAdapter(Activity context,String [] monthArray) {
this.context=context;
this.itemList=monthArray;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return itemList[position];
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_item, parent,
false);
RobotoTextView make = (RobotoTextView) row.findViewById(R.id.spinner_title);
make.setText(itemList[position]);
Log.d("SpinnerA", "getView");
return row;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_dropdown_item, parent,
false);
RobotoTextView month = (RobotoTextView) row.findViewById(R.id.spinner_month);
month.setText(itemList[position]);
RobotoTextView year = (RobotoTextView) row.findViewById(R.id.spinner_year);
year.setText(context.getString(R.string.year));
Log.d("SpinnerA", "getDropDownView");
return row;
}
}
But when I added it to my spinner and run the up, I don't see anything in spinner.
MonthsSpinnerAdapter adapter = new MonthsSpinnerAdapter(GeneralActivity.this
, getResources().getStringArray(R.array.month_list));
spinner.setAdapter(adapter);
I think it will be work nice, but I dont know where I have mistake. This is example what I see, when run app:
And at this picture, what I need:
And this my xml files if you need:
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:typeface="roboto_medium"
android:padding="5dp"
android:textColor="#fff"
android:id="#+id/spinner_title"
android:text="month"
android:background="#color/primary_color"
xmlns:android="http://schemas.android.com/apk/res/android" />
And DropDown layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_medium"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:id="#+id/spinner_month"
android:text="month"
android:textColor="#545454"
android:background="#fafafa" />
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_regular"
android:layout_marginTop="16dp"
android:layout_marginRight="12dp"
android:layout_alignParentRight="true"
android:text="year"
android:id="#+id/spinner_year"
/>
</RelativeLayout>
Help me please with my problem
Try to return itemList.length in getCount function of your adapter.
#Override
public int getCount() {
return itemList.length;
}

Overriding dropdown list style for Spinner in Dialog mode

I need to override List of dropdown spinner items when Spinner id in dialog mode (android:spinnerMode="dialog"). I need this to define my own list divider.
I have found dropDownListViewStyle item in application Theme, which contains divider item. And it works, but only for android:spinnerMode="dropdown".
Can I have the same effect for "dialog" mode?
Try this:
<Spinner
android:id="#+id/spinnerAddToList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dialog"/>
Dropdown layout spinner_item_line_drop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18sp" />
<LinearLayout
android:id="#+id/separator"
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>
Main View layout: spinner_item_line_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18sp" />
</LinearLayout>
SpinnerAdapter:
public class SpinnerLineAdapter extends BaseAdapter {
private List<MyListEntity> values;
public SpinnerLineAdapter(List<MyListEntity> values)
{
this.values = values;
}
public void setValue(List<MyListEntity> list)
{
this.values = list;
}
#Override
public int getCount() {
return values.size();
}
#Override
public MyListEntity getItem(int position) {
return values.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
ViewHolder viewHolder;
Context context = parent.getContext();
if (convertView == null) {
itemView = LayoutInflater.from(context).inflate(R.layout.spinner_item_line_main, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = itemView.findViewById(R.id.text_view);
itemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) itemView.getTag();
}
viewHolder.textView.setText(values.get(position).getName());
return itemView;
}
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View itemView = convertView;
ViewHolder viewHolder;
Context context = parent.getContext();
if (convertView == null) {
itemView = LayoutInflater.from(context).inflate(R.layout.spinner_item_line_drop, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = itemView.findViewById(R.id.text_view);
itemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) itemView.getTag();
}
viewHolder.textView.setText(values.get(position).getName());
return itemView;
}
private static class ViewHolder {
TextView textView;
}
}
Activity:
SpinnerLineAdapter spinnerAddToListAdapter = new SpinnerLineAdapter(list);
spinnerAddToList.setAdapter(spinnerAddToListAdapter);
spinnerAddToList.setSelection(0, false);
spinnerAddToList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
MyListEntity list = spinnerAddToListAdapter.getItem(position);
viewModel.addList(list.id);
viewModel.update(myEntity);
}
#Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
Button click to show dialog:
mButtonAdd.setOnClickListener(view -> {
spinnerAddToList.performClick();
});

Android Spinner created dynamically change selected item background color

I'm creating a Spinner dynamically like the code below:
private void createCoordinationSpinner() {
TextView tvwCoordination = new TextView(this);
this.spinnerCoordination = new Spinner(this);
//Some code to setup textview...
LinearLayout.LayoutParams paramsSpinnerCoordination = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
paramsSpinnerCoordination.setMargins(10, 0, 10, 0);
this.spinnerCoordination.setLayoutParams(paramsSpinnerCoordination);
this.spinnerCoordination.setBackgroundResource(R.drawable.rounded_border);
this.spinnerCoordination.setPrompt("Coordination");
this.spinnerCoordination.setOnItemSelectedListener(spinnerItemClickListener);
//Adding both views to an existing LinearLayout, that is possible to have another views, instead of spinner
linearSchoolCoordination.addView(tvwCoordination, 0);
linearSchoolCoordination.addView(spinnerCoordination, 1);
linearSchoolCoordination.setGravity(Gravity.BOTTOM);
}
//Thats the line that sets the adapter:
adapterCoordination = new ItemSpinnerAdapter(context, R.layout.coordinationspinnerlayout, arrayCoordination);
rounded_border.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/gray_dialog" />
<padding android:left="8dp"
android:top="8dp"
android:right="8dp"
android:bottom="8dp" />
<corners android:radius="20dp" />
</shape>
coordinationspinnerlayout.xml:
<LinearLayout android:orientation="vertical"
android:background="#color/transparent2"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/transparent2"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/transparent2"
android:gravity="center_vertical" >
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageSpinnerItem"
android:src="#drawable/itemlistviewicon_nochildren"
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="5dp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/tvwSpinnerCoordination"
android:layout_marginLeft="10dp"
android:textSize="16dip"
android:textColor="#color/black"
android:text="TextView"
android:layout_weight="1"/>
<ImageView android:layout_height="20dp"
android:layout_width="20dp"
android:id="#+id/imageSpinnerDownArrow"
android:src="#drawable/spinnerdownarrow"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<View android:background="#color/silver"
android:layout_height="1dp"
android:layout_width="fill_parent"
android:id="#+id/linearSpinnerSeparator"/>
</LinearLayout>
</LinearLayout>
And finally, the adapter, ItemSpinnerAdapter.java:
public class ItemSpinnerAdapter extends ArrayAdapter<CoordinationData>
{
private int tvwID;
private List<CoordinationData> coordinationList;
public ItemSpinnerAdapter(Context context, int textViewResourceId, List<CoordinationData> coordinationList)
{
super(context, textViewResourceId, coordinationList);
this.tvwID = textViewResourceId;
this.coordinationList = coordinationList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
View view = convertView;
if (view == null)
{
LayoutInflater layInf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layInf.inflate(tvwID, null);
holder = new ViewHolder();
holder.tvwDescription = (TextView) view.findViewById(R.id.tvwSpinnerCoordination);
holder.description = coordinationList.get(position).getCoordinationName();
view.findViewById(R.id.linearSpinnerSeparator).setVisibility(View.GONE);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
holder.description = coordinationList.get(position).getCoordinationName();
}
if (holder.tvwDescription != null)
holder.tvwDescription.setText(holder.description);
return view;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
View view = convertView;
if (view == null)
{
LayoutInflater layInf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layInf.inflate(tvwID, null);
holder = new ViewHolder();
holder.tvwDescription = (TextView) view.findViewById(R.id.tvwSpinnerCoordination);
holder.tvwDescription.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
holder.description = coordinationList.get(position).getCoordinationName();
view.findViewById(R.id.imageSpinnerDownArrow).setVisibility(View.GONE);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
holder.description = coordinationList.get(position).getCoordinationName();
}
if (holder.tvwDescription != null)
holder.tvwDescription.setText(holder.description);
return view;
}
public class ViewHolder
{
TextView tvwDescription;
String description;
}
}
adding it to a LinearLayout that already exists in my activity's XML layout file.
Along with my CustomSpinnerAdapter, it works pretty well.
But I still need to change some properties and I'm stucked...
Since my app is using blue texts and backgrounds, I'd like to set the selection of a spinner item highlight color. Its default is orange.
Since I'm developing for API 10, I'm using limited Spinner methods.
Any suggestions?
Thanks in advance!
EDIT: is that possible to change prompt header background color?
EDIT2: adding further code.
EDIT:
You're using a spinner, so you need to implement [getDropDownView][1].
If you want to change the selected color of your View you need to create a Color List Drawable like so
In Colors/list_color.xml
<selector>
<item android:state_selected="true">SomeColor</item>
</selector>
in your view
<YourView
android:background="#color/list_color" />
See this resource for more info
take the custum adapter
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
_position = position;
LinearLayout linear_ayout = (LinearLayout)parent.getChildAt(0);
linear_ayout.setBackgroundColor(Color.TRANSPARENT);
TextView tv = (TextView) linear_ayout.getChildAt(0);
tv.setTextColor(Color.WHITE);
gname_spinner=mySpinner.getSelectedItem().toString();
if (mySpinner.getSelectedItem().toString().equalsIgnoreCase("All Groups"))
filItemAdapter("A", "");
else
filItemAdapter("G", mySpinner.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Logger.d("******", "not selected");
filItemAdapter("A", "");
}
});
MyCustomAdapter custom_adapter = new MyCustomAdapter(
getApplicationContext(), R.layout.custum_spinner_group,
group_list);
mySpinner.setAdapter(custom_adapter);
public class MyCustomAdapter extends ArrayAdapter<String> {
List<String> group_list;
public MyCustomAdapter(Context context, int textViewResourceId,
List<String> group_list) {
super(context, textViewResourceId, group_list);
this.group_list = group_list;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.custum_spinner_group, parent, false);
TextView text_group = (TextView) row.findViewById(R.id.text_group_name);
LinearLayout layout = (LinearLayout) row
.findViewById(R.id.layout_group_name);
text_group.setText(group_list.get(position));
if (position == _position) {
layout.setBackgroundColor(getResources().getColor(R.color.spinner_background));
text_group.setTextColor(getResources().getColor(R.color.white));
} else {
layout.setBackgroundColor(Color.TRANSPARENT);
text_group.setTextColor(getResources().getColor(R.color.spinner_background));
}
Log.d("selected group", ":"+text_group.getText());
return row;
}
}
After long time searching for a solution,
I found out that I can't set style or change its selection color dynamically 'cause of the API version that I'm developing.

Categories

Resources