How can I detect focused EditText in android?

AndroidAndroid Edittext

Android Problem Overview


I have a number of EditText elements in my page along with two buttons. I want the user to touch on any one EditText field and click any button to insert a certain value into that very EditText field they touched. Giving input using the keypad is not allowed. Please help me to do this.

Android Solutions


Solution 1 - Android

You can use View.OnFocusChangeListener to detect if any view (edittext) gained or lost focus.

for (EditText view : editList){
   view.setOnFocusChangeListener(focusListener);
}
....
private OnFocusChangeListener focusListener = new OnFocusChangeListener() {
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus){
			 focusedView = v;
		} else {
			 focusedView  = null;
		}
	}
}

This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class. And obviously you would need to create an array with them in it, thus the editList.

Solution 2 - Android

This works for me. It returns a boolean.

myEditText.hasFocus()

Solution 3 - Android

One thing that you can do is declare a global variable evalue which will tell you which is the last selected EditText by using onTouchListener and then based on the value of evalue, you can set the text value to the edittext by button click. hope you understood.

the code for it can be as follow:

EditText e1,e2;
	Button b1,b2;
	String evalue;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        e1=(EditText)findViewById(R.id.editText1);
        e2=(EditText)findViewById(R.id.editText2);
        b1=(Button)findViewById(R.id.button1);
        b2=(Button)findViewById(R.id.button2);
        
        e1.setOnTouchListener(new View.OnTouchListener()
        {
			public boolean onTouch(View arg0, MotionEvent arg1)
			{
				evalue="1";
				return false;
			}
		});
        
        e2.setOnTouchListener(new View.OnTouchListener()
        {
			public boolean onTouch(View arg0, MotionEvent arg1)
			{
				evalue="2";
				return false;
			}
		});
        b1.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View arg0) {
				if(evalue=="1")
				{
					e1.setText("yes");
				}
				if(evalue=="2")
				{
					e2.setText("yes");
				}
			}
		});
        
        
        b2.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View arg0) {
				if(evalue=="1")
				{
					e1.setText("No");
				}
				if(evalue=="2")
				{
					e2.setText("No");
				}
			}
		});
        
    }

Its a logical coding.. not upto the mark.. if you find a better one. then use it. thank you.

Solution 4 - Android

> This worked for me.

e1 = (EditText) findViewById(R.id.editText1);

e1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
   @Override
   public void onFocusChange(View arg0, boolean hasfocus) {
      if (hasfocus) {
         Log.e("TAG", "e1 focused") 
      } else {
         Log.e("TAG", "e1 not focused") 
      }            
   }
});

Solution 5 - Android

With Java 8 lambdas:

EditText editTextTo = findViewById(R.id.editTextTo);

editTextTo.setOnFocusChangeListener((view, b) -> {
    if (view.isFocused()) {
        // Do whatever you want when the EditText is focused 
        // Example:
        editTextFrom.setText("Focused!");
    }
});

Note that the view inside the lambda function is actually an instance of an EditText.

Solution 6 - Android

Have your Fragment implement OnTouchListener & OnFocusChangeListener:

public class AttributesFragment extends Fragment 
                       implements OnTouchListener, OnFocusChangeListener {

Then implement using:

@Override
public boolean onTouch(View view, MotionEvent event) {
	if (view instanceof EditText) {
		view.setOnFocusChangeListener(this); // User touched edittext
	}
	return false;
}

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

Don't forget to set your listener after creating your EditText

editText.setOnTouchListener(this);

This way if an EditText box is focused by default but user has not changed the field it will not fire the onFocusChange event.

Solution 7 - Android

Simple Coding:

if(EditText1.isFocused()){
  //EditText1 is focused
}else if(EditText2.isFocused()){
  //EditText2 is focused
}

Solution 8 - Android

In your activity or fragment implement OnFocusChangeListener.

edittextNeme= (EditText) findViewById(R.id.edittextNeme);
edittextNeme.setOnFocusChangeListener(this);

You will have to check which view changed focus by getting the view id with view.getId() and handle accordingly.

@Override
public void onFocusChange(View view, boolean hasfocus) {
    switch(view.getId()){
    case R.id.edittextNeme:
    //Do something
    break;

    ...etc
    }
}

Solution 9 - Android

This worked for me using Java 8 (lambdas):

EditText myEditText = findViewById(R.id.editText);

myEditText.setOnFocusChangeListener((view, hasFocus) -> {
      if(hasFocus){
          Log.e("TAG", "myEditext has focus") 
      }else{
          Log.e("TAG", "myEditext has no focus") 
      }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEclipse-fanView Question on Stackoverflow
Solution 1 - AndroidMr.MeView Answer on Stackoverflow
Solution 2 - AndroidLilylakshiView Answer on Stackoverflow
Solution 3 - AndroidAnkit DhadseView Answer on Stackoverflow
Solution 4 - AndroidPhilip HerbertView Answer on Stackoverflow
Solution 5 - AndroidolmedocrView Answer on Stackoverflow
Solution 6 - Androiddwp4geView Answer on Stackoverflow
Solution 7 - AndroidUmasankarView Answer on Stackoverflow
Solution 8 - AndroidSayan MannaView Answer on Stackoverflow
Solution 9 - Androidali sampsonView Answer on Stackoverflow