Monday, October 24, 2011

EditText In Android

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;
}

});
}
}
}

10 comments:

  1. CAN YOU SEND ME SOME MORE TUTORIALS WITH OUTPUT!
    (TELEPHONY,WIFI,BLUETOOTH,LAYOUT)PLS
    THANKS

    ReplyDelete
  2. Nice Blog ! Do you need instant help to fix your QuickBooks issues? If yes, we are here to help you. Dial our QuickBooks Toll Free Phone Number +1-800-986-4607 now.our team have are 24*7 availability & very Experienced.
    View on Map: https://tinyurl.com/yyah7zez.

    ReplyDelete
  3. Whenever you face any technical glitches, you can call us at our QuickBooks Support Phone Number 1(800)986-4591 We will deliver you the finest solutions. Do not go here and there as we are here to assist you in every possible manner. Connect with us now!. For More Visit: http://www.santrasolutions.com/quickbooks-support-phone-number/
    https://tinyurl.com/y569xkcy

    ReplyDelete
  4. Nice Blog ! Let our support team solve your issues. No matter how complex the problem is, we are always here to support you. Right from the installation process to resolving any complex error. Dial our QuickBooks Customer Service Number 1-800-986-4607.
    View on Map: https://tinyurl.com/vq37tnv

    ReplyDelete
  5. Excellent and very informative blog click here
    Quickbooks Proadvisor Support Phone Number and for more detail dial on 844-908-0801

    ReplyDelete
  6. Nice Blog ! Do you Need instant help fixing problems with your QuickBooks? Now dial our QuickBooks Payroll Support Phone Number 855 -9O7-O4O6! We have technical experts who can instantly fix your problems.

    ReplyDelete
  7. If you need help in solving issues you are facing in any of the above versions, you can connect with us at our QuickBooks Support Phone Number New York 855-907-0406.
    View On Map: QuickBooks Support Phone Number.

    ReplyDelete
  8. Great content and very helpful!
    worried about QuickBooks support and any kind of help not to worry more.just make call to our 24*7 Expert support to fix it instant.
    Dial QuickBooks Helpline number +1-844-908-0801.

    ReplyDelete