I already know how to add String\'s to a gridView in android however I want to add textviews to format the text( Gravity, color, etc). This is my current code:
try it with something like this
gridview.xml
item.xml
Custom adapter class for textView
public class TextViewAdapter extends BaseAdapter {
private Context context;
private final String[] textViewValues;
public TextViewAdapter(Context context, String[] textViewValues) {
this.context = context;
this.textViewValues = textViewValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.item, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(textViewValues[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return textViewValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
and then , finally set adapter in your main class
gridView.setAdapter(new TextViewAdapter(this, YOUR_ARRAY_WITH_TEXT_FOR_TEXTVIEWS));
also note that you can pass any other values like color of text and as another argument in constructor and then set them in adapter like ...
textView.setColor(textViewColors[position]);
for attributes that all textview have in common you can change the item.xml only :) i hope it helped you ....