Search code examples
androidbuttonviewflipper

Set button pressed from ViewFlipper (Android)


I'm making an Android app and have a strange problem. I need a ViewFlipper for 5 tabs. I made a layout for a page with 5 buttons. I used OnClickListener and flipping tabs works ok, but I need to setPressed(true) for a button, and I can't do that. There is a repaint problem or something. I looked for solution on the net but no one else has this problem? What did I do wrong?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    factory = getLayoutInflater();
        skin = factory.inflate(R.layout.skin_tab_raspored, null);

    vremelayout = (LinearLayout) skin.findViewById(R.id.vreme);
    infolayout = (LinearLayout) skin.findViewById(R.id.info);

        buttons[0] = (Button) skin.findViewById(R.id.pon);
        buttons[1] = (Button) skin.findViewById(R.id.uto);
        buttons[2] = (Button) skin.findViewById(R.id.sre);
        buttons[3] = (Button) skin.findViewById(R.id.cet);
        buttons[4] = (Button) skin.findViewById(R.id.pet);

        buttons[0].setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                flipper_info.setDisplayedChild(0);
                buttons[currentTab].setPressed(false);
                currentTab = 0;
                buttons[0].setPressed(true);
            }
        });
        buttons[1].setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                flipper_info.setDisplayedChild(1);
                buttons[currentTab].setPressed(false);
                currentTab = 1;
                buttons[1].setPressed(true);
            }
        });
        buttons[2].setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                flipper_info.setDisplayedChild(2);
                buttons[currentTab].setPressed(false);
                currentTab = 2;
                buttons[2].setPressed(true);
            }
        });
        buttons[3].setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                flipper_info.setDisplayedChild(3);
                buttons[currentTab].setPressed(false);
                currentTab = 3;
                buttons[3].setPressed(true);
            }
        });
        buttons[4].setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                flipper_info.setDisplayedChild(4);
                buttons[currentTab].setPressed(false);
                currentTab = 4;
                buttons[4].setPressed(true);
            }
        });

        buttons[0].setPressed(true);

        add_time(); //adding content to vremelayout

        flipper_info = new ViewFlipper(this);

        flipper_info.addView(this.addContentToInfo(Dan.pon));//adding content to infolayout
        flipper_info.addView(this.addContentToInfo(Dan.uto));
        flipper_info.addView(this.addContentToInfo(Dan.sre));
        flipper_info.addView(this.addContentToInfo(Dan.cet));
        flipper_info.addView(this.addContentToInfo(Dan.pet));

        infolayout.addView(flipper_info);

        setContentView(skin);
}

Solution

  • use setEnabled() insted of setPressed inside every clickListener

                flipper_info.setDisplayedChild(0);
                buttons[currentTab].setEnabled(true);
                currentTab = 0;
                buttons[0].setEnabled(false);
    

    OR

    For Background color change use toggleButton , it will allow you to set selector for on/off state ... toggle states inside clickListener .