Buttons with state

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Buttons with state

Postby eento » Sun Aug 12, 2012 12:50 am

Good evening,

I ask myself the following head breaks:

I would like to create a Favorites button that has the two following states:

1st state: "Add to Favorites"

2nd state (on OnClick event): "Remove from favorites"

But I'd also be able to return to the 2nd state: "Add to Favorites" by a 2nd OnClick event ect..

Does anyone have a solution for it with a simple OnClickListener it seems impossible.

i tried something basic like this:




Code: Select all
private Boolean check;
   
    favoris_button = (Button) findViewById(R.id.promotion_favoris);
              
               if (check == true)
               {
                  favoris_button.setBackgroundColor(R.drawable.btn_orange9);
                    favoris_button.setText("Supprimer des favoris");
               }
               else
               {
                  favoris_button.setBackgroundColor(R.drawable.btn_red9);
                    favoris_button.setText("Ajouter aux favoris");
               }
              
               favoris_button.setOnClickListener(new View.OnClickListener()
               {
                   public void onClick(View v)
                   {
                        check = true;
                        
                   }
               });



But the problem is that i want to immediatly change the button view on my current activity.. it could only run if OnCreate() method of the activity was recalled..
eento
Freshman
Freshman
 
Posts: 2
Joined: Sun Aug 12, 2012 12:47 am

Top

Re: Buttons with state

Postby eento » Sun Aug 12, 2012 1:47 am

Problem resolved using checkbox :

Code: Select all
favoris_button = (CheckBox) findViewById(R.id.star);
          
           favoris_button.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
               if (((CheckBox) v).isChecked()) {
                  favoris_button.setText("Supprimer des categories");
               }
               else
                  favoris_button.setText("Ajouter aux favoris");
            }
         });
eento
Freshman
Freshman
 
Posts: 2
Joined: Sun Aug 12, 2012 12:47 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests