user836200
user836200

Reputation: 655

Adding labels/text to Android Gridview

Is there a way to add a text label to an android gridview? I'm using the standard android example, Hello GridView.

For instance, if I wanted to add an image title below each of the images, is there a way to do this? If so, could someone explain how this would work? Or, are there alternative ways to implement this? Thanks!

Upvotes: 2

Views: 13081

Answers (3)

MKJParekh
MKJParekh

Reputation: 34311

You can create Custom GridView as like in ListView we define Custom Layout to show the ListRow you also can create Custom Layout for the GridView Item See this Example For GridView.

Using the simple GridView You can just show one ImageView as you are doing right now, Now If you want to add another View (text) with each Grid Item, You will have to create one XML file to containing a TextView and ImageView.

Then use that XML file in the Adapter and in the GetView method you can set Image and Text to your view.

Here is another example for the same And here is another Good Example .

Upvotes: 7

Harsh Dev Chandel
Harsh Dev Chandel

Reputation: 763

Call this adapter from your activity

      GridView view =(GridView) findViewbyId(is of grid view here);
      view.setAdapter(new BottomMenuGridAdaptor(this.context));

This is the code for adapter

     public class BottomMenuGridAdaptor extends BaseAdapter {


private Context context;
public BottomMenuGridAdaptor(Context context) {
    super();
    this.context = context;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return 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.tab_button, null);
    }
    LinearLayout ll=(LinearLayout) convertView.findViewById(R.id.linear_topbar);

    if(position==PostionHandler.getPosition(context))
        ll.setBackgroundResource(R.drawable.tab_blue_bg);
    else
        ll.setBackgroundResource(R.drawable.tab_red_bg);

    ImageView imageView=(ImageView)convertView.findViewById(R.id.icon);
    imageView.setImageResource(mThumbIds[position]);
    TextView txt= (TextView) convertView.findViewById(R.id.title);
    txt.setText(mThumbtext[position]);
    return convertView;
}

private Integer[] mThumbIds = {
        R.drawable.temp_thisweek,
        R.drawable.temp_store,
        R.drawable.temp_mylibrary, 
        R.drawable.temp_lateststore,
        R.drawable.temp_more
};

private String[] mThumbtext = {
        "This Week",
        "C&EN Store",
        "My Library", 
        "Latest News",
        "More"
};

Upvotes: 0

user493244
user493244

Reputation: 919

for Exaample take a LinearLayout and add a imageview , textview and inflate that layout and assingn to convertview .

Upvotes: 1

Related Questions