I have three edittext. In first and second i get data from user with keyboard. In third edittext i open DatePickerDialog , get data from it and show edittext.
Here is my question. While user skip to other edittext with keyboard next button , how can i understand that third edittext is activated ?
You can use onfocus changelistenerlike this:
name = (EditText) findViewById(R.id.liganame);
name.setOnFocusChangeListener(this);
email = (EditText) findViewById(R.id.email);
email.setOnFocusChangeListener(this);
date = (EditText) findViewById(R.id.date);
date.setOnFocusChangeListener(this);
and check the id of your datepicker edittext in onfocuschange like this:
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId() == R.id.date && hasFocus){
//your code here....
}
}
Related
My scenario is as it follows: I have a search fragment and in that fragment, I have 3 text fields(Search by name, by zip code and by distance). When the focus is applied on the "Search by name" field, the other 2 fields dissapear and the width of the selected field increases. My problem is that I can't lose focus of the first field and so, after I am done with writing the information, I can't use the other 2 fields.
What I am looking for is a way to lose focus of the fields when I press "enter" or when I press anywhere else on the screen but the text field.
Here is the code that I am running right now:
final EditText searchByName = (EditText) getActivity().findViewById(R.id.search_by_name);
final EditText searchByZipcode = (EditText) getActivity().findViewById(R.id.search_by_zipcode);
final EditText searchByDistance = (EditText) getActivity().findViewById(R.id.search_by_distance);
searchByName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (hasFocus) {
searchByName.getLayoutParams().width=900;
searchByZipcode.setVisibility(View.INVISIBLE);
searchByDistance.setVisibility(View.INVISIBLE);
} else {
searchByName.getLayoutParams().width=405;
searchByZipcode.setVisibility(View.VISIBLE);
searchByDistance.setVisibility(View.VISIBLE);
}
}
});
searchByZipcode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (hasFocus) {
searchByZipcode.getLayoutParams().width=700;
searchByName.setVisibility(View.INVISIBLE);
} else {
searchByZipcode.getLayoutParams().width=240;
searchByName.setVisibility(View.VISIBLE);
}
}
});
Inside your Activity Class , add this below method , it will work for you .It will hide keyboard and will clear focus anywhere outside you click in that one it will work .
#Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return true;
}
How show hint text when open edittext in fullscreen mode?
I'm can show hint text with OnFocusChangeListener.
editChangeName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
editChangeName.setHint(b ? getResources().getString(R.string.name) : "");
}
});
But.
How hide hint text after closing edittext?
// assuming you have a EditText view with id
View viewById = getView().findViewById(R.id.editTextId);
if(viewById!=null) {
// set listener to this view "many views could share one listener"
viewById.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// if view match id && has no focus
if(v.getId() == R.id.editTextId && !hasFocus)
// clear hint
((EditText) v).setHint(null);
// else show hint
}
});
}
hasFocus <- as name says
has (true) -> show
doesn't have (false) -> hide
if you are not sure if view by id is one you assign to an edit text do a check ( double check will not hurt)
if (EditText.class.isAssignableFrom(v.getClass()))
// do cast
EditText editText = (EditText) v;
This code is correct. But. How hide hinttext, after closing softkeyboard.. edittext have focus – Eugenu Yansky
Detecting when user has dismissed the soft keyboard
How to remove focus without setting focus to another control?
How to clear focus and remove keyboard on Android?
I have a table with multiple rows, each containing an EditText which corresponds to a dimension. I want to have some sort of listener that will return whichever EditText is currently selected. Can this be done with getCurrentFocus(); or onFocusChange();?
My idea is to have the user select/focus the dimension field they want edited, and then completing the field using an output slope/angle from the device's gyroscope when a "write data" button is pressed. I'd probably do this using selected_dimension_edittext.setText(gyro_value);
You can know which EditText has Focus by the ID of the view like this:
EditText edText1 = (EditText)findViewById(R.id.editText1);
EditText edText2 = (EditText)findViewById(R.id.editText2);
OnFocusChangeListener mFocusChangeListener = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){ //Check if View has focus
switch (v.getId()){
case R.id.editText1: {
//Code A
} break;
case R.id.editText2: {
//Code B
} break;
}
}
}
};
edText1.setOnFocusChangeListener(mFocusChangeListener);
edText2.setOnFocusChangeListener(mFocusChangeListener);
If the editTexts are in a ArrayList you could pass a Tag to each editText which is the index.
For example:
for(int i=0;i<editTextList.size();i++){
editTextList.get(i).setTag(i);
}
Then you can check with a listener (textChangedListener or TextWatcher-->http://lecturesnippets.com/android-textchanged-listener-textwatcher/) which EditText is changed or selected by reading the tag. Just iterate through your editTextList and call the setText()-Method if the edit text has the selected tag.
I build an android application. I have an EditText. I want to save changes (or anything else) automatically after the user change the text.
Now I use
editText.addTextChangedListener(textWatcher);
and
TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
...
}
But it saves the changes after every little change (add \ remove a letter), and I want that it'll be saved only after the user finished (closed the keyboard, clicked on a different edittext, etc. ).
How can I do that?
Implement onFocusChange of setOnFocusChangeListener and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control and you should save the data of editext. for example
EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
//SAVE THE DATA
}
}
});
Implement setOnFocusChangeListener for all EditTexts and you should handle Back Press event too. If the user change the data of edittext and didn't goto another EditText and pressed back then edited vallue will not save. So should use onKeyDown or onBackPressed too
As Kotlin is now official android language:
var editText = findViewById<EditText>(R.id.editText)
editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (!hasFocus) {
//SAVE THE DATA
}
}
I have a editText contianing text Enter ur name.. so i want to when user just click on that editText then the text disappear and editText should be empty and user can write anything their. so how can i do like this. plz give me ans
final EditText edittxt = (EditText) findViewById(R.id.youredittext);
edittxt.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus == true)
{
if (edittxt.getText().toString().compareTo("Your hint") == 0) // default text
{
edittxt.setText("");
}
}
}
});
For your requirement you don't need onClick event listener.
try this
<EditText
android:hint="Enter you name"
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
or from java code
editText.setHint("Enter you name");
android:hint = "text that you want the user to be assisted with while typing" is used for this feature.
This works well with EditText, autocompleteText view as well both in android and monodroid.
Try this code..
editText.setOnClickListener(new OnClickListener(){
public void onClick(View v){
((EditText)v).setText("");
}
});