• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I had this issue with a button with a custom background, and ended up using the selected state for this. That state is available for all views.

To use this you have to define a custom button background as a state list:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="false" android:state_focused="false" 
     android:state_pressed="false"><bitmap ... /></item>
  <item android:state_selected="true"><bitmap ... /></item>
  <item android:state_focused="true"><bitmap ... /></item>
  <item android:state_pressed="true"><bitmap ... /></item>
</selector>

Then to use that background, let's say it is in /res/drawable/button_bg.xml in your layout file, you use:

...
<Button android:background="@drawable/button_bg" ... />
...

In your code you can switch to the (de-)selected state in your onClick listener:

myButton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    v.setSelected(true);
    // normal click action here
  }
});

The activated state matches the intended meaning better, but is only available from Android 3.x and higher.

  • 46
Reply Report
    • The isActivated() methode is only available from API level 11 and higher. I do not recommend using this for comparability reasons with old android OS version. API level 11 is equals to android 3.x what no one is using on their phones.
      • 2
    • @phlipp is right, I noticed this in testing (after typing this answer) and switched to selected, but forgot to update my answer.

Use the following code. It's useful.

mycodes_Button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mycodes_Button.setPressed(true);
        return true;
    }
});
  • 41
Reply Report
      • 1
    • There's a little problem with this. I think that when the activity is paused the buttons get unpressed. It's a bummer for me because I display a progress dialog and also navigate to other activities and expect the buttons to keep pressed in these situations.
    • This basically works, but you should mention, that you lose the normal button functionality. Because you return true here, the onClick stuff is never executed. So actually you have to trigger the stuff, you want the button to do from within this onTouch method. Btw: You should use v.setPressed(true).

Would a ToggleButton suit your needs?

Judging from your comments it seems that you aren't aware of the Touch Mode.In this mode, which is the default for most of the things, there is no focus (which is what you are trying to achieve) and no selected items.

You could try to exit the touch mode programmatically, but I wouldn't recommend it. After a small period of getting used to it, the touch mode will give a much better experience to the users.

  • 5
Reply Report
    • After i click the button, it doesn't stay as pressed (i mean with green color), it returns to the original state, gray one. I need normal button and not a ToggleButton.. thanks for your answers!
    • Well,i think it's the same thing...i mean that's what i want..i want when one button pressed then keep as pressed or focused