Question

Actually , I am trying to implement This .I have a little problem ,When i click Image Circle 1 Button ,on that time Passcode Field change background color of 1st field . But when i click Image Circle 1 Button there are no change in 2nd Passcode field.I think getId() Properly not working .May I know what is the correct way to achieve my objective?Maybe this question too basic, but i did't find any suitable solution.Please Help me out.Please check this statement View.OnClickListener imgButtonHandlerone = new View.OnClickListener()

Here is my code :

public class UserPasscode extends Activity {

ImageButton Imagepassone ;
ImageButton Imagepasstwo ;
ImageButton Imagepassthree ;
ImageView image;
ImageView image2;
ImageView image3;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_passcode);

    ActionBar ab = getActionBar(); 
    ab.setDisplayUseLogoEnabled(false);
    ab.setDisplayShowHomeEnabled(true);
    ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    ab.setCustomView(R.layout.actionbar);
    ab.setDisplayHomeAsUpEnabled(true);
    ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#33CCFF"));     
    ab.setBackgroundDrawable(colorDrawable);

    Imagepassone = (ImageButton) findViewById(R.id.ButtonPassOne);
    Imagepasstwo = (ImageButton) findViewById(R.id.ButtonPassTwo);
    Imagepassthree = (ImageButton) findViewById(R.id.ButtonPassThree);

    Imagepassone.setOnClickListener(imgButtonHandlerone); 
    Imagepasstwo.setOnClickListener(imgButtonHandlertwo); 
    Imagepassthree.setOnClickListener(imgButtonHandlerthree); 

    image = (ImageView) findViewById(R.id.imagefirstpasscode);
    image2 = (ImageView) findViewById(R.id.imagesecondpasscode);
    image3=(ImageView)findViewById(R.id.imagethirdpasscode);

}


View.OnClickListener imgButtonHandlerone = new View.OnClickListener() {

    public void onClick(View v) {     

          if(v.isPressed()){

              int d ; 
              d = R.drawable.passselect;

                if(image.getId()!=d)
                    {
                    image.setImageResource(d);
                     }          
                 if(image.getId()==d && (image2.getId()!=d))
                  {
                 image2.setImageResource(d);
                  }
             }
                }

};


View.OnClickListener imgButtonHandlertwo = new View.OnClickListener() {

    public void onClick(View v) {


                }
};

View.OnClickListener imgButtonHandlerthree = new View.OnClickListener() {

    public void onClick(View v) {


                }
};

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            Intent intent = new Intent(this, UserDobActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
            default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed() {
   moveTaskToBack(true); 
   UserPasscode.this.finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.user_passcode, menu);
    return true;
}}
Was it helpful?

Solution

d is not an id. I'd suggest you to do like this:

// same listener for everyone
Imagepassone.setOnClickListener(imgButtonHandler); 
Imagepasstwo.setOnClickListener(imgButtonHandler); 
Imagepassthree.setOnClickListener(imgButtonHandler);  

//...
// switch id for each view inside the same listener
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
    public void onClick(View v) { 
        // check the view clicked by id    
        if(v.getId() == R.id.Imagepassone) {

            // do something like change background
            if(image.getBackground() != getResources().getDrawable(R.drawable.passselect)) {
                image.setBackgroundResource(R.drawable.passselect);
            } else if((image.getBackground() == getResources().getDrawable(R.drawable.passselect)) &&
                (image2.getBackground() != getResources().getDrawable(R.drawable.passselect)) {
                image.setBackgroundResource(R.drawable.passselect);
            }

        } else if(v.getId() == R.id.Imagepasstwo) {
            // do something
        } else if(v.getId() == R.id.Imagepassthree) {  
            // do something
        }
    }
};  

But I think this is not a proper way.. As I can see and understand, your image (image, image2, image3) are the top rounded image, right? If yes, you can try, by every click to change the step of these icons above and change their backgrounds. It will be better I think to do this with a local int and change the step. Something like this should work:

// local variable just below ImageView image3; line
private int nbStep = 0;

// ...
Imagepassone.setOnClickListener(imgButtonHandler); 
Imagepasstwo.setOnClickListener(imgButtonHandler); 
Imagepassthree.setOnClickListener(imgButtonHandler); 

// listener
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
     public void onClick(View v) { 
         // check id like above
         if(v.getId() == R.id.Imagepassone) {
             // augment the local int
             nbStep++;
             // call a general method to check the background with int
             changeStepBackground(nbStep);
         }
     }
});

public void changeStepBackground(int i) { 
    // four step switch
    switch(i) {
        case 1:
            image.setBackgroundResource(R.drawable.passselect);
        break;
        case 2:
            image2.setBackgroundResource(R.drawable.passselect);
        break;
        case 3:
            image3.setBackgroundResource(R.drawable.passselect);
        break;
        case 4:
            image4.setBackgroundResource(R.drawable.passselect);
        break;
    }
}

I hope I understand clearly and this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top