0

I want to know how to put tick mark on image which we click in grid layout.I have image of tickmark pls tell me how to put tick mark over image?

here is my code

    public class HeadshotAllPhoto extends Activity implements OnItemClickListener 
{
    ImageView bg,tickmark;
    int ht,wt;
    GridView imagegrid;
    static boolean flag = false;
    String path,filemanagerstring;
    String[] ImageNameArr;
    String filePath = null;
    String ImageName ;
    Bitmap bitmap;
    DataHelperHeadshot dbHeadshot;
    String uplaodedHeadshot;
    Cursor cursor;
    String sub_list ;
    String[] pathArr;
    Bitmap[] bmp;
    ArrayList<String> items = new ArrayList<String>();
    ImageView i;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.headshotallphoto);
        dbHeadshot = new DataHelperHeadshot(this);

        bg = (ImageView)findViewById(R.id.selectHeadshotView);      

        List<String> names = this.dbHeadshot.fetchAllHeadshot();
        StringBuilder sb = new StringBuilder();
        for (String name : names)
        { 
           name.trim();
           sb.append(",");
           sb.append(name);
        } 
        bmp = new Bitmap[SelectionHeadshot.icount+1];
        for(int p=0;p<names.size();p++)           
        {       
            if(names.get(p) !=null)
            {               
                bitmap = decodeFile(names.get(p));    
                if(bitmap!=null)
                {
                    items.add(names.get(p));                  
                    bmp[p] = bitmap;
                }
            }           
        }       
        imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
        imagegrid.setAdapter(new ImageAdapter(getApplicationContext(),bmp));
        imagegrid.setOnItemClickListener(this);      
    }

    public class ImageAdapter extends BaseAdapter
    {
        private Context mContext;       
        Bitmap[] mImageArray;        

        public ImageAdapter(Context c, Bitmap[] imgArray) 
        {
              mContext = c;
              mImageArray = imgArray;              
        }

        public int getCount() 
        {
              return mImageArray.length;
        }

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

        public View getView(int position,View convertView,ViewGroup parent)
        {
            System.gc();           
            if (convertView == null) 
            {               
                i = new ImageView(mContext);
                i.setLayoutParams(new GridView.LayoutParams(92,92));
                i.setScaleType(ImageView.ScaleType.CENTER_CROP);               
                i.setImageBitmap(mImageArray[position]);                
            }
            else 
                 i = (ImageView) convertView;           
            return i;
        }

    }

    public Bitmap decodeFile(String filePath) 
    {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 100;
        final int H = 50;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < H)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;        
        bitmap = BitmapFactory.decodeFile(filePath, o2);        
        return bitmap;
    }

    public void onItemClick(AdapterView<?> a, View v, final int position, long id) 
    {   
        imagegrid.getChildAt(position).setBackgroundResource(R.drawable.imgredselectmark);  
    }
}

Thanks

Saurabh
  • 457
  • 2
  • 8
  • 26
Monali
  • 1,966
  • 12
  • 39
  • 59
  • Do you mean like a checkbox tick mark, or a tick mark overlay like a todo application? – codinguser May 17 '11 at 09:02
  • i have the image of tickmark ..yes it is like checkbox – Monali May 17 '11 at 09:10
  • You should use custom adapter for gridview ..... – Kakey May 17 '11 at 09:13
  • I used the imageadaptor for that. pls see my edited post, I dont know how to put tickmark image on selected image or u can see my code on http://stackoverflow.com/questions/6026928/showing-images-in-gridview – Monali May 17 '11 at 09:16
  • I want to set it as a background and also if i click on another image then the tick mark should display on that image – Monali May 17 '11 at 09:47
  • i dont want to disappear the image, – Monali May 17 '11 at 10:07
  • @Monali - If I understood it correctly, you do not know how to overlay one image over the other (also, you need to define onClick() listener for your ImageView object to initiate that action). Please check: http://stackoverflow.com/questions/4611012/how-to-composecombine-two-photo-image-in-android/4614265#4614265 – Zelimir May 17 '11 at 10:09

1 Answers1

0

Try this code :

        private int Splash_Time_Limit = 3000;

        gridview.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            gridview.getChildAt(arg2).setBackgroundResource(R.drawable.wrong);

            //  new Handler().postDelayed(new Runnable() {
            //  @Override
            //  public void run() {
            //       gridview.getChildAt(arg2).setBackgroundResource(R.drawable.tick1);
            //  }
            //  }, Splash_Time_Limit);
        }
    });

If you click the Image in Grid View the images tick image will be set as Background image.

If you want to disappear that Tick image after some times use Handler code that is commented.

Venky
  • 11,049
  • 5
  • 49
  • 66
  • it does not give any error but the background image is not visible although i dont know it is changing or not – Monali May 17 '11 at 10:41
  • ImageView img_icon = new ImageView(this); img_icon.setImageResource(R.drawable.imgredselectmark); img_icon.setAdjustViewBounds(true); img_icon.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); FrameLayout frame = (FrameLayout)findViewById(R.id.frame); frame.addView(img_icon); – Monali May 17 '11 at 11:19
  • @m4n07 Which one i implemented? – Venky Jun 01 '11 at 11:01
  • yes, Any working code sample for having a checkbox over the images in grid view. (like the ones in Gallery Application) – m4n07 Jun 01 '11 at 12:06