Buttons Android Disabled looks Active - android

I'm an Android beginner and I am currently trying to make my first calculator app.
My biggest inspiration is the Windows 10 built-in calc. I want some buttons to be disabled during some actions to get rid of bugs but I don't like the look of disabled-buttons.
I'd rather have them enabled but make them so they don't perform any action. I tried to use .setClickable(false) but then there is no sound of click as well as no animation of clicking (shadow).
Can they be fully clickable but have no action?

Use a variable to decide that. Put it somewhere in your class:
private boolean clicked = false;
Then(at the on click listener of the button):
if (clicked) {
//your action happens here(it wouldn't happen, until you change clicked to true)
}

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/disabled_color" android:state_enabled="false" />
<item android:drawable="#color/enabled_color" android:state_enabled="true"/>
</selector>
You can use the above xml (let's say we name it button_selector.xml)
Apply it as background for your Button (in your activity layout)
In your code, based on your actions, use
setEnabled(false);
and that's it. You can change it back programmatically when you want the button to be active again. Also, you can add
setFocusable(false);
setFocusableInTouchMode(false);
setClickable(false);

Try this:
Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(false);
What it does is disable the button and make it not clickable.

Related

Change View Background color for clicked recyclerview items

I am trying to change the background color of my recyclerview row when it is clicked. When one row is clicked, its background color is changed. If another row is clicked, then the previous row changes back to its old state and the newly clicked one changes color.
I've tried achieving this using selectors, but when i release the touch, the color just goes back to normal and none of my rows are highlighted.
Would anyone have an idea how to approach this? Thanks in advance
Here is my selector code - Desired goal (Selected row STAYS charcoal until another row is selected)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#color/charcoal_dark" />
<item android:state_enabled="true"
android:state_focused="false" android:drawable="#color/charcoal_dark" />
<item android:drawable="#color/black"/>
</selector>
There are several ways this is a question I posted about multiselection. But your rows shall have a boolean property, for me this is the most simple way to do it of all I have found. In my question #cool alien showed a different aproach. Here is another one, the only difference from your question is that you are looking for single-select.
For Single select in your OnBindViewHolder, you should have a variable tracking previous selections and adjust, when there is a click. I have at the end of my question my code.
Have you tried handling onClick event of the element and changing the background color property?
Here is an example code from post How to Change color of Button in Android when Clicked?
boolean check = false;
Button backward_img;
Button backward_img1;
backward_img = (Button) findViewById(R.id.bars_footer_backward);
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1);
backward_img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
check = true;
backward_img.setBackgroundColor(Color.BLUE);
}
});
if (check == true) {
backward_img1.setBackgroundColor(Color.RED);
backward_img.setBackgroundColor(Color.BLUE);
}

Image "inside" button while keeping standard animations

I would like to define a button in Android, through XML, which, below the standard Button graphics (change color when clicked, slightly rounded edges,...), shows an image of my choice. I would like the final product to be somewhat like this:
I have tried change the src and background of an ImageButton, but it does not provide the intended effect. Could you please point me some way of achieving this?
Maybe that's not exactly what you mean by standard, but when you set a background, you end up having to recreate the behavior when the button is clicked. The best way to do it in Android is by using a selector as your button's background. Create a XML drawable with the selector in it.
Example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_bg_pressed" />
<item android:drawable="#drawable/button_bg_default" />
</selector>
That way, when your button is clicked, it will change the background to other image that does what you want.
EDIT:
There are other modifiers such as focused, if you need.
If you want Lollipop's ripple effects, I think you can create a Layout and put your button inside it. Change the Layout background and set the button background to transparent. If that doesn't work, try adding android:background="?android:attr/selectableItemBackground" to your button.
As Paulo said, you can achieve the click effect with a selector.
See this answer (https://stackoverflow.com/a/30192562) it gives the code for a very nice and customizable ripple effect.

i need the ON/OFF button for enable/disable by clicking the button ,this is possible by using OnClickListner or any method?

how to do the button with ON/Off by clicking it must varies and along with that it shows Green for ON else RED. this is possible without graphics or must need graphics?
any one tried like this plz help me?
ToggleButton does the same. And here is tutorial which will tell how to use this.
You can use ToggleButton
If you with to use Button instead, you can make field boolean m_isOn; in your class, and in OnClickListener check this field and set button color (e.g. with setColorFilter()) and text accordingly.
Edit
Small example, if you really wish to avoid ToggleButton and using drawables:
#Override
public void onClick(View v) {
m_isOn ^= true;
((Button)v).getBackground().setColorFilter(m_isOn ? 0xFF00FF00 : 0xFFFF0000, PorterDuff.Mode.MULTIPLY);
((Button)v).setText(m_isOn ? "ON" : "OFF");
}
use ImageButton instead of button, and selector as image.
this XML file you can put into drawable directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_prev_active" />
<item android:drawable="#drawable/button_prev_no_active" />
In layout do this:
<ImageButton android:id="#+id/btnPrev"
android:scaleType="fitXY"
android:padding="0px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="prevClickHandler"
android:src="#drawable/button_prev_selector"/>

Android: Visual version of Button.performClick

I know that I can trigger the OnClickListener.onClick of a Button manually in code by calling performClick, but that doesn't seem to make it visually appear as it's been clicked. I'm looking for a way to manually make a button appear as if it's been clicked. Do I need to manually change the background drawable and invalidate (and then change it back again on a Handler.postDelayed call), or is there a more framework-y way of doing this?
EDIT
I know how to make the button have different drawables to appear pressed when the user initiates the press. The question is this:
Is there a simple way to make a button appear pressed programmatically when not physically pressed by the user?
SOLUTION
I just subclassed Button and made the button aware of it's normal background as a StateListDrawable and the Drawable that is used as the pressed state. I expose a method that manually sets the background to the "pressed" drawable, and I use Handler.postAtTime to have it return to it's normal background so it can be used as a regular button again when I'm done.
Although this question is very old, I figured I'll still answer it. You don't need to subclass the View. First call performClick(), visual cue won't last long, but then you can set the button's pressed state via view.setPressed(true); and then reset it a couple of milliseconds later like this.
handler.postDelayed(new Runnable() {
#Override
public void run() {
view.setPressed(false);
}
}, 100);
Ya, you have to create 2 drawables. One for pressed state and other for normal state.
Then you will have to create an xml like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/focused_drawable" />
<item
android:state_pressed="false"
android:drawable="#drawable/unfocused_drawable" />
</selector>
Place this xml inside your drawable folder. You can also add focused state as
android:state_focused="true"
Then inside your layout which is used by your activity, give inside your button tag:
android:background="#drawable/your xml file name"

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

This question already has answers here:
Closed 10 years ago.
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?
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.
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;
}
});
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.

Categories

Resources