How to Disable an Android Button

How to disable an Android button?

Did you try this?

myButton.setEnabled(false); 

Update: Thanks to Gwen. Almost forgot that android:clickable can be set in your XML layout to determine whether a button can be clickable or not.

How to disable a button in Kotlin?

Just to disable a button .

button.isEnabled = false
button.isClickable = false

and If you want to disable a button , and want to grey out its color & background ,you can do something like this -

fun markButtonDisable(button: Button) {
button?.isEnabled = false
button?.setTextColor(ContextCompat.getColor(textView.context, R.color.white))
button?.setBackgroundColor(ContextCompat.getColor(textView.context, R.color.greyish))
}

Disabling a Button with an icon

Create a new grayed icon and add both inside a selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/icon_ok" android:state_enabled="true" />
<item android:drawable="@drawable/icon_ok_disabled" android:state_enabled="false" />

</selector>

Use inside button like: android:drawableStart="@drawable/selector"



For TextColor,

Create another selector inside res/color/mycustomtextcolor.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#666" android:state_enabled="false" />
<item android:color="#000" android:state_enabled="true"/>
</selector>

Inside your widget call using: android:textColor="@color/mycustomtextcolor"

Or

inside your style add another item using: <item name="android:textColor">@color/mycustomtextcolor</item>

How to disable button click?

case R.id.next:
Log.i("Tag","tag");
if(imageCounter < imageList.length)
{
imageCounter++;
imagePath = imageList[imageCounter];
if (imageCounter==(imageList.length)-1)
{
ImageButton next=(ImageButton)findViewBYId(R.id.next);
next.setEnabled(false);
}
}
break;
case R.id.back:
if(imageCounter > 0)
{
imageCounter--;
imagePath = imageList[imageCounter];
if (imageCounter==0)
{
ImageButton back=(ImageButton)findViewBYId(r.id.back);
back.setEnabled(false);
}
}
break;

How to disable button once it is clicked and enable it again once another button is clicked in android

Try this one

// Create a global button variable
Button flagButton = null;

Button.OnClickListener btnOnClickListner = new Button.OnClickListener() {
@Override
public void onClick(View v) {

if(flagButton == null)
flagButton = (Button)v;
else
flagButton.setEnabled(true);

v.setEnabled(false);
flagButton = (Button)v;

if (v == btn1)
// my code
else if (v == btn2)
// my code
else if (v == btn3)
// my code
else if (v == btn4)
// my code
}
}

How to disable dot button in android Kotlin

so you want to disable the button that has the name dotButton after the user clicks it for the first time :

to disable a button, use:

dotButton.isClickable=false

if you want the button to become clickable again(like when the user deletes the dot that he already pressed) you can use:

dotButton.isClickable=true

Edit:
modify your activity code like that :

when (button.id) {
buttonOne.id -> isClicked += "1"
buttonTwo.id -> isClicked += "2"
buttonThree.id -> isClicked += "3"
dotButton.id-> {
isClicked+="."
dotButton.isClickable=false//**add this line here**

}
}

fun clearFunction(view: View){
viewResult.setText("")
dotButton.isClickable=true//**add this line here**
}


Related Topics



Leave a reply



Submit