Thursday, March 1, 2012

Make button enable and disable in android

This program is for make a button enable and disable.




Step 2) Put below code in your main java file
 
package cal.ender;

import java.util.Calendar;

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

public class CalenderActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        final Button btn1=(Button)findViewById(R.id.button1);
        Button btn2=(Button)findViewById(R.id.button2);
        Button btn3=(Button)findViewById(R.id.button3);
       
       
        btn2.setOnClickListener(new OnClickListener() {
                                               
                                                public void onClick(View arg0) {
                                                                // TODO Auto-generated method stub
                                                                btn1.setEnabled(false);
                                                               
                                                               
                                                }
                                });
               
        btn3.setOnClickListener(new OnClickListener() {
                                               
                                                public void onClick(View arg0) {
                                                                // TODO Auto-generated method stub
                                                                btn1.setEnabled(true);
                                                               
                                                }
                                });
           
       
    }
}


Step 2) Code for main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
/>
<Button android:text="Change me" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Disable" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Enable" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

 </LinearLayout>

4 comments:

  1. Good evening, I just want to know if you know how to enable or disable an image in android. Its not like a button, just an image that when you tap it will looks like it's tapped, maybe its color turns different. Thank you

    ReplyDelete
  2. hii.. i have develop ticket booking application...... but single problem... button can't disable/enable..... how to disable/enable button click on runtime.....

    ReplyDelete
  3. you have to set the disable and enable with true and false , in you code, where you want to enable or disable that.

    ReplyDelete
  4. Monica SarfudheenMay 22, 2015 at 1:00 AM

    hello.... we can click/touch on the button "charge me" to enable it.. and when it is clicked once more, then it is disabled.
    Can you please show how it can be done?
    Thankyou

    ReplyDelete