Enable Disable button in android programmatically

How to turn on-off android simple button functionality and make button non-clickable.

Buttons are used to hold some line of code that can be executed with different type of button click functions. But some times developer needs to disable the whole button with some kind of condition and convert the button to non executable using setEnabled(boolean value) method. This method gives us the facility to enable or disable button with boolean true-false value. So in this tutorial we are simply creating a toggle button and setting up setEnabled() method on toggle button on off. If toggle button is off then button click functionality will be disable via this method and after on toggle button button works again normally. So here is the complete step by step tutorial for Enable Disable button in android programmatically.

android-project-download-code-button

How to Enable Disable button in android programmatically.

Code for MainActivity.java file.

 package com.enabledisablebutton_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
 
 ToggleButton togglebutton;
 Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 togglebutton = (ToggleButton)findViewById(R.id.toggleButton1);
 button = (Button)findViewById(R.id.button1);
 
 button.setEnabled(false);
 
 togglebutton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 if(togglebutton.isChecked())
 {
 //Enabling button on toggle button on.
 button.setEnabled(true);
 
 }
 else {
 //Disabling button on toggle button off.
 button.setEnabled(false);
 }
 }
 });
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 Toast.makeText(MainActivity.this, " Button is Enable ", Toast.LENGTH_LONG).show();
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.enabledisablebutton_android_examples.com.MainActivity" >

 <ToggleButton
 android:id="@+id/toggleButton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="ToggleButton" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/toggleButton1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="38dp"
 android:text="Sample Button" />
 
</RelativeLayout>

Screenshot:

button-on

Enable Disable button in android programmatically

Click here to download Enable Disable button in android programmatically project with source code.

One Comment

  1. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.

Leave a Reply

Your email address will not be published. Required fields are marked *