1

I am trying to change ImageView background when clicked(like Duolingo)

one

Here is my code from fragment :

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.frag_repeat, container, false);
    final int[] a1 = {0};
    final int[] a2 = {0};
    final int[] a3 = {0};
    final int[] a4 = {0};


    TypedArray itemsIcon = getResources().obtainTypedArray(R.array.nav_drawer_icons);



    ImageView wer1 = (ImageView) rootView.findViewById(R.id.imageView);

    ImageView wer2 = (ImageView) rootView.findViewById(R.id.imageView2);

    ImageView wer3 = (ImageView) rootView.findViewById(R.id.imageView23);

    ImageView wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
    TextView textView1 = (TextView) rootView.findViewById(R.id.textView711);


    textView1.setText(ss[i]);
    wer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a1[0] = 1;
            a2[0] = 0;
            a3[0] = 0;
            a4[0] = 0;
        }
    });
    wer2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a2[0] = 1;
            a1[0] = 0;
            a3[0] = 0;
            a4[0] = 0;
        }
    });
    wer3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a3[0] = 1;
            a2[0] = 0;
            a1[0] = 0;
            a4[0] = 0;
        }
    });
    wer4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a4[0] = 1;
            a2[0] = 0;
            a3[0] = 0;
            a1[0] = 0;
        }
    });
    if(a1[0] > 0){
        wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer1.setBackgroundColor(Color.parseColor("#42A5F5"));
        wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
    } else  if(a2[0] > 0){

        wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer2.setBackgroundColor(Color.parseColor("#42A5F5"));
        wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }else  if(a3[0] > 0){
        wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer3.setBackgroundColor(Color.parseColor("#42A5F5"));
        wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
    } else if(a4[0] > 0){
        wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer4.setBackgroundColor(Color.parseColor("#42A5F5"));
        wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }
    if(i1 == 1){
        wer1.setImageResource(itemsIcon.getResourceId(i, -1));


    } else if(i1 == 2){
        wer2.setImageResource(itemsIcon.getResourceId(i, -1));

    } else if(i1 == 3){
        wer3.setImageResource(itemsIcon.getResourceId(i, -1));

    } else if(i1 == 4){
        wer4.setImageResource(itemsIcon.getResourceId(i, -1));

    }

    return rootView;
}

But ImageView background doesn't change! I have CardViews inside RelativeLayout and inside RelativeLayout I have ImageView.

camelCaseCoder
  • 1,447
  • 19
  • 32

3 Answers3

0

Please share xml file. Maybe you don't set background for ImageView in xml

BaDo
  • 540
  • 8
  • 19
0

Each of your onClick listeners is just toggling values in an array. I suspect what you really want is to invoke the setBackground color code inside each handler.

From your code, I think what you are trying to do is this:

  1. Whichever button was clicked - change it's background color from white to #42A5F5.

  2. For the other three buttons not clicked, change the background color back to white.

I don't know what the name of your class is (I'll refer to it as "YourClass"). But make your 4 ImageViews member variables.

class YourClass extends Fragment // I'm guessing this is the declaration, it doesn't matter - use whatever you have now
{
    ImageView _wer1;
    ImageView _wer2;
    ImageView _wer3;
    ImageView _wer4;

Then inside onCreateView, assign each one of these member variables exactly where you assign the local variables:

    _wer1 = (ImageView) rootView.findViewById(R.id.imageView);
    _wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
    _wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
    _wer4 = (ImageView) rootView.findViewById(R.id.imageView43);

Now set each of your click listeners to invoke a "ToggleBackgroundColors". Notice that there's a different index value passed to this function in each callback handler.

_wer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            YourClass.this.ToggleBackgroundColors(0); 
        }
    });

_wer2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            YourClass.this.ToggleBackgroundColors(1);
        }
    });

_wer3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            YourClass.this.ToggleBackgroundColors(2);
        }
    });

_wer4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            YourClass.this.ToggleBackgroundColors(3);
        }
    });

Then implement ToggleBackgroundColors:

void ToggleBackgroundColors(int which)
{
    int highlight = Color.parseColor("#42A5F5");

    ImageView [] views = {_wer1, _wer2, _wer3, _wer4};
    for (int x = 0; x < views.length; x++)
    {
        int background = (which == x) ? highlight : Color.WHITE;
        views[x].setBackgroundColor(background);
    }
}
selbie
  • 100,020
  • 15
  • 103
  • 173
0

In your onClickListener, you can also set colorFilter on your imageView when you onClick it.

Consider using the onTouchListener instead though as you would like the imageView to change color when you touch the button and not when you click on it.

ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
Simon
  • 19,658
  • 27
  • 149
  • 217