Adapter:
public class GenderAdapter extends BaseAdapter {
private Context context;
private String[] genderList;
public GenderAdapter(Context context) {
this.context = context;
this.genderList = new String[3];
this.genderList[0] = context.getString(R.string.gender); // Hint text
this.genderList[1] = context.getString(R.string.male);
this.genderList[2] = context.getString(R.string.female);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.drop_down_row_light_gray, null);
}
TextView text = (TextView) convertView.findViewById(R.id.text);
text.setText(getItem(position));
Resources resources = context.getResources();
text.setTextColor(resources.getColor(position == 0 ? R.color.gray : R.color.dark_gray)); // If it is the hint text (position == 0) set the textcolor to be a lighter color
return convertView;
}
@Override
public int getCount() {
return genderList.length;
}
@Override
public String getItem(int position) {
return genderList[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.closed_row_light_gray, null);
}
TextView text = (TextView) convertView.findViewById(R.id.text);
text.setText(getItem(position));
Resources resources = context.getResources();
text.setTextColor(resources.getColor(position == 0 ? R.color.gray : R.color.dark_gray)); // If it is the hint text (position == 0) set the textcolor to be a lighter color
return convertView;
}
}
drop_down_row_light_gray:<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
style="@style/Base.TextAppearance.AppCompat.Medium"
android:background="@color/light_gray"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="48dp"
android:paddingLeft="8dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:ellipsize="end"
android:textColor="@color/dark_gray"
android:gravity="center"/>
closed_row_light_gray:<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
style="@style/Base.TextAppearance.AppCompat.Medium"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="48dp"
android:paddingLeft="8dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:textColor="@color/dark_gray"
android:gravity="center"/>
Add "setError" to the spinner :private void setSubmitButtonOnClickListener() {
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFormValid()) {
...
} else {
if (isAgeEmpty()) {
TextView textView = (TextView) ageSpinner.getSelectedView().findViewById(R.id.text); // "text" is the layout id of the TextView that displays the text
textView.setError(getString(R.string.age_is_required));
}
...
}
}
});
}
Notes:
1) In my experience, when I used 9 patch as the spinne's background, the spinner's text dissapeared. It does not happen if I use a "normal" drawable or background color.
2) The "arrow thingy" to the right side of a spinner is actually a background (a 9 patch png). That being said, if you change the spinne background the arrow will dissapear.ng>
No hay comentarios:
Publicar un comentario