1

In Holo theme, as you click on a button it gets blue and shines for a moment. now I want the button remains at this appearance, and at the next click comes back to normal appearance. how to do that?

UPDATE: my code:

public class HomeActivity extends SherlockActivity {
org.holoeverywhere.widget.Button bt;
boolean isPressed = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    bt = (org.holoeverywhere.widget.Button) findViewById(android.R.id.button1);
    bt.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (!isPressed) {
                    isPressed = true;
                } else {
                    isPressed = false;
                }
                bt.setPressed(isPressed);
            }

            return true;
        }
    });
}
}
Soheil
  • 1,676
  • 7
  • 34
  • 65

2 Answers2

1

Method 1 : You can set a selector drawable for this button

1.Create a file `buttonbg.xml` in `drawable/`

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >

        <item
            android:state_focused="true"
            android:drawable="@drawable/focused_button" />
        <item
            android:state_pressed="true"
            android:drawable="@drawable/pressed_button" />
        <item
            android:drawable="@drawable/normal_button" />
    </selector>

2. Create necessary images for indicating states of button

3. Set this buttonbg.xml as background for this button.

4. set this button setPressed(true) inside button click. 

For more info : How to modify the default button state in Android without affecting the pressed and selected states?

Method 2 :

boolean isPressed = false;
((Button)findViewById(R.id.button)).setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                if(!isPressed){
                    isPressed = true;
                }else{
                    isPressed = false;
                }  
                        ((Button)findViewById(R.id.button)).setPressed(isPressed);                      
            }

                return true;
            }
        });
Community
  • 1
  • 1
Asha Soman
  • 1,846
  • 1
  • 18
  • 28
  • unfortunately neither worked,`@drawable/focused_button` and `@drawable/pressed_button` and `@drawable/normal_button` can not be found...and the second method causes force close :( – Soheil Sep 04 '13 at 08:38
  • Ask your graphic designer to create images for pressed button,normal button etc.. and use their names instead of the names given in the answer.This is only an example which directs you to your goal.What is the exception obtained in second method? – Asha Soman Sep 04 '13 at 08:45
  • I want to attach a TouchListener to my button by `setOnTouchListener` method in `onCreate()`, as I do this my app gets force closed...why? – Soheil Sep 04 '13 at 08:50
  • Have you added the ActionBarSherlock library? – Asha Soman Sep 04 '13 at 09:11
  • +1 yes, actually the theory of your first method is correct and finally solved my problem, however as it is described by @Nizam I didn't need customized images... – Soheil Sep 04 '13 at 13:56
1

First of all, You can get default images used by SDK(Button, check box, whatever it is) from this location ->

~\android-sdk\platforms\android-18\data\res\drawable-hdpi

Here I'm looking for API-18. Use Whatever version you like that support Holo theme.

From there, You can find an image something like btn_default_pressed_holo_dark.9.png .

Copy-paste it to your project drawable-hdpi folder. This was the first step. Now,

public class MainActivity extends Activity {
    Button b;
    Drawable back;
    int flag=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button) findViewById(R.id.button1);

        back=b.getBackground();

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {   
                if(flag==0){
                    b.setBackgroundResource(R.drawable.btn_default_pressed_holo_dark);
                    flag=1;
                    }
                else{
                    flag=0;
                    b.setBackground(back);
                }
            }
        });
    }


}

Check it :)

Nizam
  • 5,698
  • 9
  • 45
  • 57
  • +1 bravo! worked perfectly :) however I had to change `b.setBackground(back);` to `b.setBackgroundDrawable(back);` so that it work(however it is deprecated!)...thanks – Soheil Sep 04 '13 at 12:47
  • You are welcome :) Yes,`.setBackgroundDrawable()` is deprecated as from API Level 16. – Nizam Sep 04 '13 at 13:49