sanjay
sanjay

Reputation: 2620

Borders for gridview items in android

i retrieved image+text from DB and displayed it in a gridview.i.e. Each item of gridview consists of one image+one text.It works fine.But i just wanna know how to set border of each gridview items separately in android.How could i do this?

My code is..

public class HomePage extends Activity  {
    private ArrayList<SingleElementDetails> allElementDetails=new ArrayList<SingleElementDetails>();
    DBAdapter db=new DBAdapter(this);
    String category, description;
    String data;
    String data1;
    GridView gridview;
    Button  menu;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.homepage);

        menu=(Button)findViewById(R.id.menus);

      menu.setOnClickListener(new OnClickListener(){
          public void onClick(View v)
          {
              gridview=(GridView)findViewById(R.id.gridview);
              TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
               int mGalleryItemBackground = a.getResourceId(
                        R.styleable.Gallery1_android_galleryItemBackground, 0);
                a.recycle();


              allElementDetails.clear();
              db.open();
                long id;
                //id=db1.insertTitle1(category, description,r_photo);
                Cursor cursor = db.getAllTitles1();
                while (cursor.moveToNext())
                {
                    SingleElementDetails single=new SingleElementDetails();
                    single.setDishName(cursor.getString(1));
                    single.setCateogry(cursor.getString(2));
                    single.setDescription(cursor.getString(3));
                    single.setImage(cursor.getBlob(4));
                    allElementDetails.add(single);

                }
                db.close();
             CustomListAdapter adapter=new CustomListAdapter(HomePage.this,allElementDetails);
             gridview.setAdapter(adapter);
             gridview.setBackgroundResource(mGalleryItemBackground);



          }
      });
        }

}

I used CustomListAdapter here to store retrieved data from DB. My CustomListAdapter code is...

public class CustomListAdapter extends BaseAdapter {
    private  ArrayList<SingleElementDetails> allElementDetails;

    private LayoutInflater mInflater;

    public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) {
        allElementDetails = results;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return allElementDetails.size();        
    }

    public Object getItem(int position) {
        return allElementDetails.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        convertView = mInflater.inflate(R.layout.listview1, null);
        ImageView imageview = (ImageView) convertView.findViewById(R.id.image);

        TextView textview1= (TextView) convertView.findViewById(R.id.dishname_entry);
        TextView textview2 = (TextView) convertView.findViewById(R.id.category_entry);
        TextView textview3=(TextView)convertView.findViewById(R.id.description_entry);
        textview1.setText(allElementDetails.get(position).getDishName());
        textview2.setText(allElementDetails.get(position).getCategory());

        if(allElementDetails.get(position).getDescription().length()>8)
            textview3.setText(allElementDetails.get(position).getDescription().substring(0,8)+"...");
        else
            textview3.setText(allElementDetails.get(position).getDescription());    

        byte[] byteimage=allElementDetails.get(position).getImage();
        ByteArrayInputStream imageStream = new ByteArrayInputStream(byteimage);
        BitmapFactory.Options op=new BitmapFactory.Options();
        op.inSampleSize=12;
        Bitmap theImage= BitmapFactory.decodeStream(imageStream,null,op);
        imageview.setImageBitmap(theImage);
        return convertView;
    }    

}

Upvotes: 1

Views: 2028

Answers (1)

misterB
misterB

Reputation: 13

Maybe there's an easier way but Possibly create an xml as a drawable in drawables with the layers defined inside like so:

Each drawable is represented by an element inside a single element.

file location:

res/drawable/filename.xml

The filename is used as the resource ID.

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <bitmap
        android:src="@drawable/rectangle" />
</item>
<item>
    <bitmap
        android:src="@drawable/griditem" />
</item>
<item>
    <nine-patch> other etc...
</item>
    <item> other drawable etc.</item>
</layer-list>

could use it as a drawable or a bitmap like so:

Bitmap bitmap=BitmapFactory.decodeResource(getApplicationContext.getResources(),R.drawable.filename);

Some documentation here

Upvotes: 1

Related Questions