Change and hold image background color in horizontal scrolling

In my application, I used ImageView

internally HorizontalScrollView

. ImageView

images are obtained from URI (json parsing) and images are loaded into ImageView

using a generic image loader.
Here my problem is how to change the background ImageView

for the corresponding image on the click of the element.
  thank you in advance

what should i try:

 String RecentBooks="http://";

 ArrayList<List<String>> arrrecentbooks= new ArrayList<List<String>> ();

      

// HorizontalScrollview is completely created programmatically

HorizontalScrollView scollviewh = new HorizontalScrollView(this);

        View v1=null;

        LinearLayout linear_pmain = new LinearLayout(this);

        linear_pmain.setOrientation(LinearLayout.HORIZONTAL);
        //  linear_pmain.setBackgroundDrawable(getResources().getDrawable(R.drawable.topshelf));bottomthumbnail

      

// arrrecentbook contains images processed by fron uri

        int length=arrrecentbooks.size();

 for(int j=0;j<length;j++)
        {
            LayoutInflater vii = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            v1=vii.inflate(R.layout.thumbnailrowlayout, null);

            List<String> listitem=arrrecentbooks.get(j);

            String  rimageurl=listitem.get(13); 

            String  rbookprice=listitem.get(10);

 img=(ImageView) v1.findViewById(R.id.imgg);
            TextView txt=(TextView) v1.findViewById(R.id.txtg);
            txt.setText("Rs."+rbookprice+"/-");
            txt.setTypeface(tf,Typeface.NORMAL);    


        img.setTag(new String[]{listitem.get(0),listitem.get(1),listitem.get(2),listitem.get(3),listitem.get(4),listitem.get(5),listitem.get(6),listitem.get(7),listitem.get(8),listitem.get(9),listitem.get(10),listitem.get(11),listitem.get(12),listitem.get(13),listitem.get(14),listitem.get(15),listitem.get(16),listitem.get(17),listitem.get(18),listitem.get(19),listitem.get(20),"recentbooks"});

      

// image loaded into image view

            {

                imageLoader=new ImageLoader(this);

                imageLoader.DisplayImage(rimageurl, this, img);
            }
            img.setOnClickListener(myIItemClickListener);

            linear_pmain.addView(v1);
        }

        scollviewh.addView(linear_pmain);

        lrecentbooks.addView(scollviewh);

      

// imgeview on itemclick listener

   public OnClickListener myIItemClickListener = new OnClickListener() {

    @Override

    public void onClick(View v) {


        try{

         RelativeLayout borderlyt=(RelativeLayout)findViewById(R.id.Booklyut);

            borderlyt.setBackgroundResource(R.drawable.bgorange);

            String[] Qtag = (String[]) v.getTag();


            Log.d("myItemClickListener", Qtag + "");

            //v.setBackgroundResource(R.drawable.bgorange);

            Intent itemintent = new   Intent(cxt,NoolDashboardDetailPage.class);


            Bundle b = new Bundle();

            b.putStringArray("iarray", Qtag);

            b.putInt("mflag", 0);

            itemintent.putExtra("android.intent.extra.INTENT", b);

            startActivityForResult(itemintent,2);

        }
        catch (Exception e) {

            // TODO: handle exception
        }
    }
};

      

+3


source to share


1 answer


I think you would like to highlight images when touched?

Try the following below img.setOnClickListener(myIItemClickListener);

::

img.setOnTouchListener(new View.OnTouchListener() {                 
    public boolean onTouch(View v, MotionEvent e) {   
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            ((ImageView) v).setColorFilter(0x44E79B0E, PorterDuff.Mode.SRC_ATOP);
        }
        if ((e.getAction() == MotionEvent.ACTION_UP) || (e.getAction() == MotionEvent.ACTION_CANCEL)) {
            ((ImageView) v).setColorFilter(null);
        }
        return false;
    }
});

      



Change "0x44E79B0E" to the desired transparent color.

Let me know if this works for you.

+2


source







All Articles