Search code examples
androidviewflippertouch-event

Android - How would I access a variable within the onTouch method for use in ViewFlipper


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!


Solution

  • 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.