I am trying to add a border programmatically to certain row elements in an ArrayAdapter that is displayed within an AlertDialog. Basically I want to take this :
And make it like this (so placing white borders on the left and right side of the views for certain rows, black border above VARIABLE):
Here is the code. This is just debu code, so don't worry too much about efficiency since it will only be run locally and only for debugging:
public class RuleDebugItemAdapter extends ArrayAdapter<RuleDebugItem> {
Context mContext;
int mLayoutResourceId;
ArrayList<RuleDebugItem> mData;
public RuleDebugItemAdapter(Context context, int resource, ArrayList<RuleDebugItem> data) {
super(context, resource, data);
mContext = context;
mLayoutResourceId = resource;
mData = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView tv = (TextView) v.findViewById(android.R.id.text1);
RuleDebugItem item = mData.get(position);
tv.setSingleLine(false);
if (item.type.equalsIgnoreCase(Field.VARIABLE)) {
tv.setText(item.ruleDebugText);
tv.setTextSize(18);
tv.setTypeface(null, Typeface.BOLD);
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.white));
tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.black));
}
else if (item.type.equalsIgnoreCase(Field.FORMRULE)) {
tv.setText(item.ruleDebugText);
tv.setTextSize(18);
tv.setTypeface(null, Typeface.BOLD);
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.white));
tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.black));
}
else if (item.type.equalsIgnoreCase(Field.CONDITION_BLOCK)) {
tv.setText(item.ruleDebugText);
tv.setTextSize(18);
tv.setTypeface(null, Typeface.BOLD);
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_orange_light));
tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white));
}
else if (item.type.equalsIgnoreCase(Field.FUNCTION)) {
tv.setTextSize(16);
if (item.success) {
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_green_light));
tv.setText(Field.SPACE + item.ruleDebugText);
}
else {
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_blue_bright));
tv.setText("Init " + item.ruleDebugText);
}
tv.setTypeface(null, Typeface.BOLD);
tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white));
}
else if (item.type.equalsIgnoreCase(Field.ACTION)) {
tv.setTextSize(16);
if (item.success) {
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_green_light));
tv.setText(Field.SPACE + item.ruleDebugText);
}
else {
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_blue_bright));
tv.setText("Pre-" + item.ruleDebugText);
}
tv.setTypeface(null, Typeface.BOLD);
tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white));
}
else if (item.type.equals(Field.CONDITION)) {
tv.setText(item.ruleDebugText);
tv.setTextSize(16);
tv.setTypeface(null, Typeface.NORMAL);
if (item.success)
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_green_light));
else
v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_red_light));
tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white));
}
return v;
}
}
All in an effort to make is a bit more readable. Yeah, I know the colors are ugly and I could do a tree instead.. :)