Search code examples
androidnullandroid-edittextinput-type-file

I can't change InputType of EditText after I set it to TYPE_NULL


I set the InputType of an EditText to TYPE_NULL with:

editText.setInputType(InputType.TYPE_NULL);

I can set it to TYPE_NULL, it works! But if I want to set InputType to something else, like TYPE_CLASS_TEXT, it doesn't work!

How can I change it dynamically in code? Like to TYPE_NULL, then to TYPE_CLASS_TEXT and to TYPE_NULL again?


Solution

  • // Try this one
    
    **activity_main1.xml**
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >
    
        <EditText
            android:id="@+id/txtValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/btnClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enable Input" />
    
    </LinearLayout>
    

    MainActivity1

    public class MainActivity1 extends Activity {
    
        private Button btnClick;
        private TextView txtValue;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main1);
    
            txtValue = (TextView)findViewById(R.id.txtValue);
            btnClick = (Button)findViewById(R.id.btnClick);
    
            txtValue.setInputType(InputType.TYPE_NULL);
            btnClick.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    if(txtValue.getInputType()==InputType.TYPE_NULL){
                        txtValue.setInputType(InputType.TYPE_CLASS_TEXT);
                        txtValue.invalidate();
                        btnClick.setText("Disable Input");
                    }else{
                        txtValue.setInputType(InputType.TYPE_NULL);
                        txtValue.invalidate();
                        btnClick.setText("Enable Input");
                    }
                }
            });
    
        }
    
    }