How to setup onFocusChangeListener() on a dynamically created set of editTexts?

 

Questions


i have this code in which i inflate a linearLayout containing 3 editTexts everytime an edittext of the previous lineaLayout loses focus. I want to have the onFocusChangeListener on the most recent created editTexts only. Instead, onFocusChangeListener is called only when the the first linearLayout loses focus and not the rest of others.

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sale_purchase_vouch);
    no=0;
    for(int i=0;i<30;i++)
        flag[i]=true;

    save=(Button)findViewById(R.id.Save);
    save.setText("Confirm Purchase");
    LayoutInflater l=getLayoutInflater();
    container=(LinearLayout)findViewById(R.id.container);
    row[no]=(LinearLayout)l.inflate(R.layout.row, container);
    items[no]=(AutoCompleteTextView)row[no].findViewById(R.id.item);
    quants[no]=(EditText)row[no].findViewById(R.id.quant);
    rates[no]=(EditText)row[no].findViewById(R.id.rate);

    quants[no].setOnFocusChangeListener(this);
    flag[no]=false;


}


@Override
public void onFocusChange(View arg0, boolean arg1) {
    // TODO Auto-generated method stub
    if(flag[no+1]==true){

        if(arg1==false){
            no++;
    LayoutInflater g=getLayoutInflater();

    row[no]=(LinearLayout)g.inflate(R.layout.row, container);
    items[no]=(AutoCompleteTextView)row[no].findViewById(R.id.item);
    quants[no]=(EditText)row[no].findViewById(R.id.quant);
    rates[no]=(EditText)row[no].findViewById(R.id.rate);
    Log.d("detection", "Row is "+ no+ arg0.getId());
        }
    }
}

I created an array of boolean to know whether the last editText had created a new linearLayout(listened to the onFocusChangeListener). help!!!

 

 

————————————————-

Answer

You need to extend the EditText class so you create your own type of View where might want to have an inner class which will implement View.OnFocusChangeListener and public method so you can setup listener with an instance of this inner class using the instance of newly created type.

android,android-linearlayout,dynamic,runtime

Facebook Comments

Post a comment