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 7660443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:27:58+00:00 2026-05-31T13:27:58+00:00

I have a variable within a loop that i am changing whenever i touch

  • 0

I have a variable within a loop that i am changing whenever i touch on a button. i need this variable at the very end of the activity for setting which view for my viewflipper to display. This whole thing started because i kept getting the ‘non-final variable inside an inner class defined in a different method’ error. so i was looking on here and did as some people suggested – make a local final variable then reassign within the onTouch method. See code below:

for(int categoryIndex = 0; categoryIndex <menuCategories.size(); categoryIndex++){

        final CustomButton menuButton = new CustomButton(this);
        menuButton.setId(ViewIdentification.getId());
        menuButton.setText(menuCategories.get(categoryIndex).category_name);
        menuButton.setTextColor(Color.argb(255, 77, 30, 16));
        menuButton.setTextSize(35);
        menuButton.setGravity(Gravity.CENTER);
        menuButton.setTypeface(tf);
        menuButton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        menuButton.setPadding(10, 10, 10, 10);

        sideMenu.addView(menuButton);

        final int currentCategoryIndex = categoryIndex;
        menuButton.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                myCurrentCategory = currentCategoryIndex;
                Log.d("Touched Side Menu button ", menuButton.getId() + ": " + menuButton.getText().toString());
                Log.d("My Current Category is ", Integer.valueOf(myCurrentCategory).toString());


                return false;
            }
        });
........
.......
.......building more stuff within this loop 

In this loop i am building up an array of LinearViews that contain a number of RelativeViews(which of course have any number of views inside that). Since i have all these LinearViews i created a viewflipper to add all of these linear views then depending on what myCurrentCategory is will be what i set my flipper to. See Code Below:

    ViewFlipper flipper = new ViewFlipper(this);
    RelativeLayout.LayoutParams viewFlipperLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,  LayoutParams.WRAP_CONTENT);
    viewFlipperLayoutParams.addRule(RelativeLayout.RIGHT_OF, sideMenu.getId());
    viewFlipperLayoutParams.setMargins(0, 0, 100, 0);
    //subMenuLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, screenLayout.getId());
    flipper.setLayoutParams(viewFlipperLayoutParams);

    for(int flipperIndex = 0; flipperIndex < subMenuLinearLayouts.size(); flipperIndex++){
        flipper.addView(subMenuLinearLayouts.get(flipperIndex));
    }

    //set which part of the menu we want to see
    flipper.setDisplayedChild(myCurrentCategory);

One subMenuLinearLayout is a LinearLayout with relativelayouts with those relativelayouts having other views(as i explained before but wanted to be clear).

Just to note i have defined myCurrentCategory at the top of the program so it is globally defined. When i set it to known states 0,1,2,3,4,5, the ViewFlipper correctly shows me the correct LinearLayout…so i know this works. However inside the onTouch method where i am assigning a new value to myCurrentCategory based on the categoryIndex the ViewFlipper does not flip to that view. My log statements that i have show that the myCurrentCategory variable in question is even getting the right values. I have just tested it. So it boils down to why is the ViewFlipper not ‘seeing’ the change in the variable? thanks!

  • 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-05-31T13:27:59+00:00Added an answer on May 31, 2026 at 1:27 pm

    You should get the correct output in your Log message in onTouch. But how should your ViewFlipper know that you just changed the content of a variable (onTouch happens at some random point in the future)?

    You need to call some method in onTouch (e.g. flipper.setDisplayedChild) to see something.

    So it boils down to why is the ViewFlipper not ‘seeing’ the change in the variable? thanks!

    Java / ViewFlipper does not monitor variables for you just because you used that variable at some point. In fact the code does not even know what variable you used. It just gets a new value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a class which represents a person, a variable within that class
I have variable @count of datatype int.I am setting values to this @count. I
I have a variable that is built in loop. Something like: $str = ;
I have a variable that contains a 4 byte, network-order IPv4 address (this was
related (sort of) to this question. I have written a script that will loop
So...I have this scenario where I have a Foreach loop that loops through a
This is driving me insane now. I have a loop that creates buttons. Each
I have a query and a loop written that lists all the rows from
I'm observing some strange behavior with a local variable within a nested for loop
I have a simple foreach loop that goes through the productID's I have stored

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.