Search code examples
androidandroid-fragmentsandroid-viewpager

Changes to button in fragment not shown on screen


I have a ViewPager that loads a quiz-question from database. In my fragment i implemented a left- and right arrow to try to inform the user that the view is slideable. I also wanted to implement onClickListeners to them so that they also can be clicked to change to the next or previous fragment.

To indicate when the user is on the first/last question i used setAlpha on the background to gray it out. I can see in the debugger that the method is called, but alpha-change is not shown on the display of the device. Now if i scroll two pages forward and back again to the first, i can see that the button is gray.

Relevant code from my fragment:

public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    if(mQuestion.getId() == 10) ) {
        mRightButton.getBackground().setAlpha(40);
    } else {
        mRightButton.getBackground().setAlpha(255);
        mRightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((QuizActivity)getActivity()).nextFragment();
            }
        });
    }

    if(mQuestion.getId() == 1) {
        mLeftButton.getBackground().setAlpha(40); // is called but not changing the alpha on the button until i slide forward and back...
    } else {
        mLeftButton.getBackground().setAlpha(255);
        mLeftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((QuizActivity)getActivity()).previousFragment();
            }
        });
    }
}

The onClickListeners work as intended.


Solution

  • Just discovered that if i use setVisibility(View.HIDDEN) instead of changing the Alpha it works fine.