Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to fix and unselect or reset the pointer on the imageview clicked

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to fix and unselect or reset the pointer on the imageview clicked

    Hi, im trying to learn with my first app, and i have 2 problem

    1) i have to click 2 times on the image for change it
    2) the imageview clicked its selected even if i click in other sector of the screen

    thanks


    public void onClick(final View imageView) {
            final ImageView mypic1 = (ImageView) findViewById(R.id.select1);
            ImageView mypic2 = (ImageView) findViewById(R.id.select2);
            ImageView mypic3 = (ImageView) findViewById(R.id.select3);
            ImageView mypic4 = (ImageView) findViewById(R.id.select4);
            ImageView mypic5 = (ImageView) findViewById(R.id.select5);
            ImageView mypic6 = (ImageView) findViewById(R.id.select6);
     
            View.OnClickListener listener= new OnClickListener() {
                public void onClick(View v) {
                    int profileold = R.drawable.image;
                    int profilenew = R.drawable.image_selected;
                    if(imageView instanceof ImageView ){
                        if(savedImageRef == seleccion){
                            ((ImageView) imageView).setImageResource(R.drawable.image);
                            savedImageRef = profileold;
                        }else{
                            ((ImageView) imageView).setImageResource(R.drawable.image_selected);
                            savedImageRef = profilenew;
                        }
                }
                }};
            mypic1.setOnClickListener(listener);
            mypic2.setOnClickListener(listener);
            mypic3.setOnClickListener(listener);
            mypic4.setOnClickListener(listener);
            mypic5.setOnClickListener(listener);
            mypic6.setOnClickListener(listener);
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: How to fix and unselect or reset the pointer on the imageview clicked

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to fix and unselect or reset the pointer on the imageview clicked

    yes because i can't get a solution there

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    217
    My Mood
    Cool
    Thanks
    0
    Thanked 24 Times in 24 Posts

    Default Re: How to fix and unselect or reset the pointer on the imageview clicked

    Using onClick as a method name twice is confusing. Once as a separate method and once inside the anonymous class. Also, please provide the complete source (I'll provide my own images) so that I can see what is going on.

    Regards,
    Jim

  5. #5
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to fix and unselect or reset the pointer on the imageview clicked

    jim this is the last code

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
     
    public class SelectActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.select_images);
            Button btn;
            btn = findViewById(R.id.button_resultados);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), ResultadosActivity.class);
                    startActivityForResult(intent, 0);
                }
            });
        }
        int savedImageRef = R.drawable.image1;
     
        public void onClick(final View imageView) {
            ImageView mypic1 = (ImageView) findViewById(R.id.select1);
            ImageView mypic2 = (ImageView) findViewById(R.id.select2);
            ImageView mypic3 = (ImageView) findViewById(R.id.select3);
            ImageView mypic4 = (ImageView) findViewById(R.id.select4);
            ImageView mypic5 = (ImageView) findViewById(R.id.select5);
            ImageView mypic6 = (ImageView) findViewById(R.id.select6);
     
            View.OnClickListener listener= new OnClickListener() {
                public void onClick(View v) {
                    int profileold = R.drawable.image1;
                    int profilenew = R.drawable.image2;
                    if(imageView instanceof ImageView ){
                        if(savedImageRef == seleccion){
                            ((ImageView) imageView).setImageResource(R.drawable.profileold);
                            savedImageRef = profileold;
                        }else{
                            ((ImageView) imageView).setImageResource(R.drawable.profilenew);
                            savedImageRef = profilenew;
                        }
                }
                }};
            mypic1.setOnClickListener(listener);
            mypic2.setOnClickListener(listener);
            mypic3.setOnClickListener(listener);
            mypic4.setOnClickListener(listener);
            mypic5.setOnClickListener(listener);
            mypic6.setOnClickListener(listener);
        }
     
        }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: How to fix and unselect or reset the pointer on the imageview clicked

    Also posted at: https://stackoverflow.com/questions/...d-change-image
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How can i fix my null pointer exception error?
    By jomdaime in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2014, 06:00 PM
  2. [SOLVED] How to tell a JPanel, that one of the JPanels it contains has been clicked?
    By Ludo in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 20th, 2014, 03:24 PM
  3. Loading an image in ImageView through code javafx
    By viper_pranish in forum JavaFX
    Replies: 0
    Last Post: January 19th, 2014, 04:02 AM
  4. Null Pointer Exception. How to fix this problem?
    By carbLoading2012 in forum Exceptions
    Replies: 2
    Last Post: February 4th, 2013, 02:06 AM
  5. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM