Set focus to the EditText upon clicking a button

15,631

Solution 1

This code for OnFocusChangeListener has to be modified little bit, for anonymous classes you need to have final reference of any object which you wish to modify, since you want to modify EditText

final int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
            R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
            R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};

Now for OnClickListener do like this

for (int i=0;i<btnEditId.length;i++) 
{
    final int counter=i;
    int button=btnEditId[i];
    findViewById(button).setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {

            // HERE v refers to button itself on which you clicked, 
            // you need to update get edit text so
            // based on ith position accessing the same edit as the button correspond too
            EditText edt=etColorId[counter];
            edt.setEnabled(true);
            edt.requestFocus();
            // and you are DONE!!!
        }
    });
}

UPDATE:

 new OnFocusChangeListener() 
 {

            @Override
            public void onFocusChange(View v, boolean hasFocus) 
            {

                if(!hasFocus) 
                { // lost focus
                       v.setEnabled(false);
                }
                else
                {
                    //you are already enabling on button click
                }
            }
  });

Solution 2

Inside

onClick()
{
    edittext.setFocusableInTouchMode(true);
    edittext.requestFocus();
}

Solution 3

onFocusChange called every time when lost focus and gain focus.

@Override
public void onFocusChange(View v, boolean hasFocus) {
    // TODO Auto-generated method stub
    v.setEnabled(false);
}

So, first you need to change code

@Override
public void onFocusChange(View v, boolean hasFocus) {
    // TODO Auto-generated method stub
    if(hasFocus) {// gain focus
       // Code block
    }
    if(!hasFocus) { // lost focus
        // Code block
    }
}

You EditText is disabled every time, when press Button

Share:
15,631
Carlo Dacuyan
Author by

Carlo Dacuyan

Updated on June 05, 2022

Comments

  • Carlo Dacuyan
    Carlo Dacuyan almost 2 years
    int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
                R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
                R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};
        
        for (int editText : etColorId) {
            findViewById(editText).setOnFocusChangeListener(new OnFocusChangeListener() {
                
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    v.setEnabled(false);
                }
            });
        }
        
        int[] btnEditId = new int[] {R.id.btnEdit1, R.id.btnEdit2, R.id.btnEdit3, R.id.btnEdit4, R.id.btnEdit5,
                R.id.btnEdit6, R.id.btnEdit7, R.id.btnEdit8, R.id.btnEdit9, R.id.btnEdit10, R.id.btnEdit11,
                R.id.btnEdit12, R.id.btnEdit13, R.id.btnEdit14, R.id.btnEdit15};
        
        for (int button : btnEditId) {
            findViewById(button).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    v.setEnabled(true);
                    v.requestFocus();
                }
            });
        }
    

    How can I gain focus to EditText upon clicking a button? I have a bunch of EditTexts and Buttons. Each button corresponds to a specific EditText. What I want is, for example: if I clicked the button1, the EditText1 will be focused. And then when I click other button, the EditText corresponds to that button will be focused and then the other EditTexts will be disabled.

  • Carlo Dacuyan
    Carlo Dacuyan almost 10 years
    Thanks for the answer Akhil Jain. Your code does really help. The button now works the way it is. All I need to finish now is how the onFocusChange works. PS: Ty for the update, will analyze it.
  • Carlo Dacuyan
    Carlo Dacuyan almost 10 years
    Thanks for this @Palak. Will take a loot at it!
  • Akhil Jain
    Akhil Jain almost 10 years
    @CarloDacuyan Glad it helped