Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:15:03+00:00 2026-06-07T15:15:03+00:00

I have an application set up to where there are eighteen buttons. The basic

  • 0

I have an application set up to where there are eighteen buttons. The basic idea behind this is that if three of those buttons are clicked, a string variable is assigned to each button. The values are then added together and compared to a string. If the values of the strings match, I would like an alert dialog to appear. I tried storing each button variable in shared preferences, but the alert dialog shows only after you restart that activity. I would like this to happen instantly. If anyone can shed any light on this, I would be forever grateful. Also, if a more efficient way of doing this is possible, that would be helpful.

I am not sure hot to handle variables outside of a block such as a button. If I knew how to, I would just pass the three variables outside of the button block and call for it within the same file, compare and then set the alert. Again, the basic logic is: if three specific buttons are selected and all three are selected, then and only then should the alert dialog appear. (If it helps, any other button selected resets all the other buttons). Code:

public class Stage1Level1Activity extends Activity {



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stage1level1);



    ImageButton a1 = (ImageButton) findViewById(R.id.a1);
    ImageButton a3 = (ImageButton) findViewById(R.id.a3);
    ImageButton a4 = (ImageButton) findViewById(R.id.a4);
    ImageButton b1 = (ImageButton) findViewById(R.id.b1);
    final ImageButton b2 = (ImageButton) findViewById(R.id.b2);
    ImageButton b3 = (ImageButton) findViewById(R.id.b3);
    ImageButton b4 = (ImageButton) findViewById(R.id.b4);
    ImageButton c1 = (ImageButton) findViewById(R.id.c1);
    final ImageButton c2 = (ImageButton) findViewById(R.id.c2);
    ImageButton c3 = (ImageButton) findViewById(R.id.c3);
    ImageButton c4 = (ImageButton) findViewById(R.id.c4);
    ImageButton d1 = (ImageButton) findViewById(R.id.d1);
    final ImageButton d2 = (ImageButton) findViewById(R.id.d2);
    ImageButton d3 = (ImageButton) findViewById(R.id.d3);
    ImageButton d4 = (ImageButton) findViewById(R.id.d4);
    ImageButton e1 = (ImageButton) findViewById(R.id.e1);
    ImageButton e3 = (ImageButton) findViewById(R.id.e3);
    ImageButton e4 = (ImageButton) findViewById(R.id.e4);



    a1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });


    a3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    a4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });

    b2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_filled);
            SharedPreferences sharedPreferences1 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences1.edit();
            editor.putString("part1", "a");
            editor.commit();
        }
        });
    b3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    b4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    c1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    c2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            c2.setBackgroundResource(R.drawable.green_filled);
            SharedPreferences sharedPreferences2 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences2.edit();
            editor.putString("part2", "b");
            editor.commit();
        }
        });
    c3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    c4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    d1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    d2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            d2.setBackgroundResource(R.drawable.green_filled);
            SharedPreferences sharedPreferences3 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences3.edit();
            editor.putString("part3", "c");
            editor.commit();
        }
        });
    d3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    d4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    e1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });

    e3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });
    e4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);


        }
        });

    SharedPreferences sharedPreferences1 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    String part1 = sharedPreferences1.getString("part1", "");
    SharedPreferences sharedPreferences2 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    String part2 = sharedPreferences2.getString("part2", "");
    SharedPreferences sharedPreferences3 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    String part3 = sharedPreferences3.getString("part3", "");

    String added = part1 + part2 + part3;
    String compared = "abc";

    if (added.equalsIgnoreCase(compared) ){

        AlertDialog alertDialog = new AlertDialog.Builder(Stage1Level1Activity.this).create();
        alertDialog.setTitle("Reset...");
        alertDialog.setMessage("R u sure?");
        alertDialog.setButton2("OK", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {



            } });

        alertDialog.show();
    }





}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:15:06+00:00Added an answer on June 7, 2026 at 3:15 pm

    If you create your variables in the “Class” that contained all those handlers they could all access the variables directly.

    So try this:

    public class Stage1Level1Activity extends Activity {
        private String myVariable;
    

    You should then be able to access myVariable in ANY of these methods you have defined

    (Editorial follows, not part of answer)
    The problem is that this code really should be 10-15 lines, seriously (and not densely-packed hard to understand lines either!). I can’t quite do it for you because I don’t have the GUI tool, but for instance at a very VERY minimum just to get you started (so you can see what I mean):

    b3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);
        }
        });
    b4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);
        }
    

    is equivalent to:

    OnClickListener ocLictener=new View.OnClickListener() {
        public void onClick(View view) {
            b2.setBackgroundResource(R.drawable.green_empty);
            c2.setBackgroundResource(R.drawable.green_empty);
            d2.setBackgroundResource(R.drawable.green_empty);
        }
        });
    b3.setOnClickListener(ocListener);
    b4.setOnClickListener(ocListener);
    

    (Note that I just picked 2 that followed that exact same pattern, there are many more.

    I’m not trying to be critical (Well I guess I am but I don’t mean it in a mean way, just instructive), I’m just thinking that if you continue coding along these lines you are going to cause yourself a lot of heartache. I would post this entire clip on the “Refactoring” SO site and see what happens 🙂

    By the way part of the problem is the GUI builders, they encourage this kind of code, but they don’t require it…

    EDIT: Actually I just can’t leave this alone, let me try a little refactor:

    public class Stage1Level1Activity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stage1level1);

    // Here we create a bunch of image buttons.  Instead of having he same line repeated
    // let's put them into an array.  I forget the type of Android's resources but let's
    // assume they are Strings:
    
    // Need to list all the ids somehow, this is probably the easiest.
    private String[] ids=new String[]{R.id.a1, R.id.a2, r.id.a3, ..., r.id.e4}  
    private String[] triggerIds=new String[]{R.id.b2, R.id.c2, r.id.d2}    
    private ImageButton btns=new ImageButton[ids.length];
    private ImageButton triggerBtns=new ImageButton[ids.length];
    
    for(int i=0;i<ids.length;i++)
        btns[i]= (ImageButton) findViewById(ids[i]);
    for(int i=0;i<triggerIds.length;i++)
        triggerBtns[i]=(ImageButton) findViewById(ids[i]);
    
    // Now you have two types of buttons, the first type are all the same
    for(ImageButton ib:btns) {
        ib.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                for(ImageButton tb:triggerBtns) {
                    tb.setBackgroundResource(R.drawable.green_empty);
        }
        });
    }
    
    int arrayPtr=0;  // Used to fill out some values in the following loop
    
    for(ImageButton tb:triggerBtns) {
        tb.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                tb.setBackgroundResource(R.drawable.green_filled);
                SharedPreferences sharedPreferences1 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences1.edit();
                // Right here is the only place that these three differ.  The pattern is:
                // editor.putString("part1", "a");
                // editor.putString("part2", "b");
                // editor.putString("part3", "c");
                String[] values=new String[]{"part1", "a", "part2","b","part3","c"};
                editor.putString(values[arrayPtr++], values[arrayPtr++]);
                editor.commit();
            }
        });
    // I'll leave it here, that replaces at least 4/5 of the original code and in 20
    // or so lines.  A lot more could be done, but the array trick is really handy
    // for code like this.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which is using a set of three combo boxes. I
I have an application that uses a ResourceDictionary to set the styles, which it
I have an application where every single page uses a set of data that
I have an application that uses multiple WebViews. Nowhere do I set the priority
I have tried this but it doesn't work: tell application Preview set myfile to
Suppose we have an application (on a server) that has a set of resources:
I have a simple application that is set up to use Docrine2 DBAL, the
I have an application set up so that I can adjust its skin at
I have an application set up using jaas module for login. There are ajax
I have an application set up that uses facebook credits set up with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.