Enable disable edittext input android programmatically

Make EditText not Editable so automatic keyboard keypad popup not open using setEnabled(false).

EditText used to get input from user but sometimes app developer dose not want to get input from user as some special project requirement like without checking terms & conditions to use this app you can not register so app builder can disable edittext to not get input from app user. So here is the complete step by step tutorial for Enable disable edittext input in android programmatically.

android-project-download-code-button

How to Enable disable edittext input in android programmatically.

Code for MainActivity.java file.

 package com.edittextdisable_android_examples.com;

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


public class MainActivity extends Activity {

 Button EnableProgrammatically , DisableProgrammatically;
 EditText Ed1;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 EnableProgrammatically = (Button)findViewById(R.id.button1);
 DisableProgrammatically = (Button)findViewById(R.id.button2);
 Ed1 = (EditText)findViewById(R.id.editText1);
 
 EnableProgrammatically.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Ed1.setEnabled(true);
 Toast.makeText(MainActivity.this, " EditText Enable Programmatically ", Toast.LENGTH_LONG).show();
 }
 });
 
 DisableProgrammatically.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Ed1.setEnabled(false);
 Toast.makeText(MainActivity.this, " EditText Disable Programmatically ", 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.edittextdisable_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="33dp"
 android:text="Click Here to Enable EditText" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:text="Click Here to Disable EditText" />

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="177dp"
 android:ems="10"
 android:hint="This is EditText" />

</RelativeLayout>

Screenshots:

enable edittext

Enable disable edittext input in android programmatically

Click here to download Enable disable edittext input in android programmatically project.

Leave a Reply

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