Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I keep a button as pressed after clicking on it? [duplicate]

Possible Duplicate:
Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?

I have a button, and I want that when I press it, it stays as pressed (with the green color on Froyo).

Any help?

mycodes_Button = (Button) findViewById(R.id.mycodes); ... if (saved_Button.isPressed()) {     saved_Button.setFocusable(true); } 

Something like this?

like image 360
Nikitas Avatar asked Jan 20 '11 12:01

Nikitas


People also ask

How do I stop my keys from double clicking?

Present Code click(function (e) { // Prevent button from double click var isPageValid = Page_ClientValidate(); if (isPageValid) { if (isOperationInProgress == noIndicator) { isOperationInProgress = yesIndicator; } else { e.

How do you make a button invisible after clicking?

you can do like this way. yourbutton. setVisibility(Button. GONE);

What is the working of duplicate button?

The duplicate button is created using the selected button type defined by Select as a basis (this handles the row selection for us). The button's action ( buttons. buttons. action ) then simply places the selected row(s) into an editing state.

How do you make button click only once in Javascript?

There are a number of ways to allow only one-click in Javascript: Disable the button after clicking on it. Use a boolean flag to indicate “clicked”, don't process again if this flag is true. Remove the on-click attribute from the button after clicking once.


1 Answers

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.

like image 177
beetstra Avatar answered Sep 22 '22 07:09

beetstra