dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 58

Thread: [RESOLVED] how to compare resources in drawable

  1. #1

    [RESOLVED] how to compare resources in drawable

    Hi, im testing with my first app and im stuck here

    i want change one image each time what i user tap on it and +1 a counter (i need the user choose 3 images its a game)

    please note:
    1) y try with selector but once i clic the image comebacks to the default
    2) i realize a tutorial on youtube with 3 methods of implement onclick for this and don't work for me
    3) if you have a more effective way of do this please tellme i dont care about delete everything and re-focus the problem in other way

    Code:
    public class SelectActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.select_activity);
            Button btn;
            btn = findViewById(R.id.button_resultados);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), ResultsActivity.class);
                    startActivityForResult(intent, 0);
                }
            });
        }
        public void onClick(final View imageView) {
            ImageView profile1 = (ImageView) findViewById(R.id.select1);
            ImageView profile2 = (ImageView) findViewById(R.id.select2);
            ImageView profile3 = (ImageView) findViewById(R.id.select3);
            ImageView profile4 = (ImageView) findViewById(R.id.select4);
            ImageView profile5 = (ImageView) findViewById(R.id.select5);
            ImageView profile6 = (ImageView) findViewById(R.id.select6);
    
            View.OnClickListener listener= new OnClickListener() {
                public void onClick(View v) {
                    if(imageView instanceof ImageView ){
                        if(                          )){              << // here starts the problem i want check if the button is pressed or i want exchange beetween images but i dont know how
                            ((ImageView) imageView).setImageResource(R.drawable.secondimage);
                        }else{
                            ((ImageView) imageView).setImageResource(R.drawable.firstimage);
                        }
                }
                }};
            profile1.setOnClickListener(listener);
            profile2.setOnClickListener(listener);
            profile3.setOnClickListener(listener);
            profile4.setOnClickListener(listener);
            profile5.setOnClickListener(listener);
            profile6.setOnClickListener(listener);
        }
    thanks for you time if you read this and help me!

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,850

    Re: how to compare resources in drawable

    The value passed to setImageResource (int resId) is an int. You could save the last value used and test it with the if statement.
    Norm

  3. #3

    Re: how to compare resources in drawable

    but how? if i try this

    int profile;
    profile = imageView.setImageResource(R.drawable.profile_selected);

    i got the error Cannot resolve method 'setImageResource'

    this works when i use

    profile1.setimageresource but i can't do this because on the onclicklistener i have the

    if(imageView instanceof ImageView ){

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,850

    Re: how to compare resources in drawable

    What was the full text of the error message?
    Why can't the compiler resolve that method? It found its definition before in the posted code.
    That method is defined as void. It does not return a value.

    Where and how is R.drawable.profile_selected defined?
    Did you try saving the value of R.drawable.secondimage in an int variable?
    Norm

  5. #5

    Re: how to compare resources in drawable

    yes but if do this

    int carta;
    myprofile = R.drawable.profile;
    int carta2;
    myprofile2 = R.drawable.other;

    i can't compare inside of the instance because the error of "== cannot be applied with android.view,view" this are incompatible types

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,850

    Re: how to compare resources in drawable

    The int values used by setImageResource can be saved and compared.
    The int variable that is used to save the image ref values would have 3 values:
    nothing saved
    ref to image1
    ref to image2

    Then test in the if statement:
    Code:
       if(savedImageRef == ref to image1) {
         set image to image2    //  toggle to other image
         savedImageRef = ref to image 2
       }
    Norm

  7. #7

    Re: how to compare resources in drawable

    It's possible do this in a simple way?
    if the user tap 1 time on the imageview i change to the image 2 if the user click again he return to the image 1
    the typical on off but with one image

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,850

    Re: how to compare resources in drawable

    Try it and see if it does what you want.
    Norm

  9. #9

    Re: how to compare resources in drawable

    yes sr with this code i get an error of variable not initizalized

    Code:
    public void onClick(View v) {
                    int primero = R.drawable.old;
                    int segundo = R.drawable.new;
                    if(imageView instanceof ImageView ){
                        int savedImageRef;
                        if(savedImageRef == primero) {
                            ((ImageView) imageView).setImageResource(R.drawable.segundo);
                            savedImageRef = segundo;
                        }
                }
                }};

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,850

    Re: how to compare resources in drawable

    variable not initizalized
    Assign it a value when defining it.

    Note: saveImageRef is a local variable. Its value will be lost when the method exits. To save values over method calls, the variable needs to be defined outside of any method.
    Norm

  11. #11

    Re: how to compare resources in drawable

    thanks for your patience, this is strage but with the first click i can select one image but with the second the image change, and if i click first on the image 1 and later on the image 6 the image 1 change to the pressed state but if i click 2 times the same image turns only 1 time

  12. #12
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,850

    Re: how to compare resources in drawable

    The code you posted won't change the image unless the value of savedImageRef is primero. If true, it then changes the image to sequndo. It does not change the value to anything else.
    Norm

  13. #13

    Re: how to compare resources in drawable

    yes sr, i add a code and now change again to the first image, still has errors like i can't choose others images because the pointer still remains on the first imageview what i click

    thanks for the help, happy new year

    i will try some hours more by myself if i have problems i will back

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,850

    Re: how to compare resources in drawable

    Can you explain what the code is supposed to do?
    I see 6 ImageView objects.
    What is supposed to be in them initially?
    What is supposed to happen when the user clicks on one of them?
    Norm

  15. #15

    Re: how to compare resources in drawable

    yes, imagine this

    6 images (with imageresource = image1), the user click on 1 and it change to image 2

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)