I am using Grid View where each item contains an image and some text. The size of text is dynamic. The problem am having is when the text is too long,its getting deprecated. The problem goes if I use a high vertical spacing. But since the length of text is different each time, I cannot use a high vertical spacing for all items. For small text am having no issues.
My layout.xml looks like this
<LinearLayout
android:id="@+id/list_container_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="@dimen/widget_padding"
android:background="@color/content_layout_bg"
android:orientation="vertical" >
<GridView
android:id="@+id/grid_workflow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
android:verticalSpacing="@dimen/widget_padding" >
</GridView>
</LinearLayout>
My Adapter class looks like this:
public class WorkflowAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> nameValues;
private ViewHolder viewHolder;
private final int resourceId;
public WorkflowAdapter(Context context, int resourceId,ArrayList<String> nameValues) {
super(context,resourceId,nameValues);
this.context = context;
this.nameValues = nameValues;
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.label = (TextView) convertView.findViewById(R.id.txt_workflow_child);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
String menuItem = nameValues.get(position);
viewHolder.label.setText(menuItem);
return convertView;
}
public class ViewHolder {
TextView label;
}
}
And my child layout.xml looks like this
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner_ed"
android:orientation="vertical"
android:padding="@dimen/widget_padding" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/workflow_child_bg"
android:orientation="vertical"
android:padding="@dimen/widget_padding" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/widget_padding"
android:layout_marginRight="@dimen/widget_padding"
android:src="@drawable/icon64x64" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/txt_workflow_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/widget_padding"
android:text="TextView"
android:textColor="@color/text_color"
android:textSize="@dimen/text_size" />
Just to mention I am setting column width for Grid View dynamically from code. Any help anyone ?