1) How we lock/unlock a EditText in android ?
or
How we prevent EditText to take input in android ?
Answer :- Here is the sample project to show how we can prevent "EditText" to take input in android.
main.xml
<?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">
<EditText android:id="@+id/textbox" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/btnLockUnlock" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
Main.java
package com.androd.satya;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity{
private EditText editText;
private boolean value = false;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.textbox);
Button btnLockUnlock = (Button) findViewById(R.id.btnLockUnlock);
btnLockUnlock.setText("Lock/Unlock");
btnLockUnlock.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (value){
value = false;
} else{
value = true;
}
lockUnlock(value);
}
});
}
private void lockUnlock(boolean value){
if (value){
Toast.makeText(this, "Lock the EditText", Toast.LENGTH_LONG).show();
editText.setFilters(new InputFilter[]{
new InputFilter(){
@Override
public CharSequence filter(CharSequence source,int start,int end,Spanned dest,int dstart,int dend) {
return source.length() < 1 ? dest.subSequence(dstart,dend):"";
}
}
});
} else{
Toast.makeText(this, "UnLock the EditText", Toast.LENGTH_LONG).show();
editText.setFilters(new InputFilter[]{
new InputFilter(){
//@Override
public CharSequence filter(CharSequence source,int start,int end,Spanned dest,int dstart,int dend){
return null;
}
}
});
}
}
}