1

I want to add a text below every image, that's my actual code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);



    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    //imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
    imageView.setLayoutParams(new GridView.LayoutParams(
            grid.getWidth()/2,  grid.getHeight()/3));

    return imageView;
}
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Dom9301
  • 41
  • 5
  • 2
    Have a look at this tut http://www.vogella.com/tutorials/AndroidListView/article.html chapter 3, you can define your listItem as xml and inflate it – A.S. May 13 '14 at 12:23

2 Answers2

1

try this, create layout like this

<?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:gravity="center"
android:orientation="vertical" >
       <ImageView
        android:id="@+id/child_Img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/photos_grid_bg"
        android:contentDescription="@string/app_name" />
     <TextView
        android:id="@+id/title_Txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:singleLine="true"
        android:text=""
        android:textColor="@color/white"
        android:textSize="12sp" />
</LinearLayout>

Inflate your layout like this

  @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder mHolder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.photos_grid_item, null);
        mHolder = new ViewHolder();

        mHolder.Img = (ImageView) convertView
                .findViewById(R.id.Img);
        mHolder.title_Txt = (TextView) convertView
                .findViewById(R.id.title_Txt);
        convertView.setTag(mHolder);

    } else {
        mHolder = (ViewHolder) convertView.getTag();
    }

    try {
        mHolder.Img.setImageResource(mThumbIds[position]);
                    mHolder.title_Txt.setText("Set your text here");
    } catch (Exception e) {
        // TODO: handle exception
    }
    return convertView;
}

private class ViewHolder {
    private ImageView Img;
    private TextView title_Txt;
}
Murali Ganesan
  • 2,925
  • 4
  • 20
  • 31
1
LinearLayout layout = new LinearLayout(mContext);

        layout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        mageView imageView = new ImageView(mContext);



        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        imageView.setLayoutParams(new LinearLayout.LayoutParams(
                grid.getWidth()/2,  grid.getHeight()/3));
        layout.addView(imageView);

        TextView tv = new TextView(mContext);
        tv.setLayoutParams(new LinearLayout.LayoutParams(
                grid.getWidth()/2,  grid.getHeight()/3));
        tv.setText("hello");
        layout.addView(tv);
        return layout;

try this, you can adjust the size of view later according to you design

Asheesh
  • 565
  • 6
  • 21