edittext.setOnFocusChangeListener - android

I am interested in how to make a custom keyboard on android and i look this page
http://www.fampennings.nl/maarten/android/09keyboard/index.htm
i do not understand one methods in this page.i try but every time i found different meanings.
if anybody know this page help me please what does focus_listener.writer wrote this coupled edittext. i know that we create new Edittext Onkey methods .and i understood that when we click edittext , it makes copy and when its make copy focus change ? after when we click edittext again while keyborad is visible , because of copy visible edittext there are no focus change and our keyboard is get unvisible. i understand that but completely i belive i make mistake.help me please
// Find the EditText
EditText edittext= (EditText)findViewById(...);
// Make the custom keyboard appear
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override public void onFocusChange(View v, boolean hasFocus) {
if( hasFocus ) showCustomKeyboard(v); else hideCustomKeyboard();

When OnFocusChangeListener is called?
Interface definition for a callback to be invoked when the focus state of a view changed.
What is onFocusChange?
abstract void onFocusChange(View v, boolean hasFocus)
Called when the focus state of a view has changed.
It's very simple, when you as user click on a EditText, it will get the "focus" and Android will show the system keyboard to let user write something inside it. Since you want to show your custom keyboard instead of the default everytime a EditText (or everything you want) gets the focus you call the method showCustomKeyboard which is:
public void showCustomKeyboard( View v ) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if( v!=null ) ((InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
}
mKeyboardView is our keyboard which should be showed instead of the original, while this line:
if( v!=null ) ((InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
is used to hide the default the keyboard (hideSoftInputFromWindow)
If you understand what i said above, you can understand what else hideCustomKeyboard(); means, it works like normal Android, when the user leave the EditText we don't need anymore to show the keyboard but since this time it's your keyboard you should care about show/hide it
"it makes copy and when its make copy focus change ?"
I don't understand what you mean, it don't copy anything.
"after when we click edittext again while keyborad is visible , because of copy visible edittext there are no focus change and our keyboard is get unvisible"
No, if you click again the same EditText it will still have the focus, the keyboard will be hidden when you change the focus

Related

Android using MaterialDatePicker and TextInputLayout

I am new to android development and currently trying to integrate material design into my app.
I would like to evaluate a simple form, for this purpose I used the components com.google.android.material.textfield.TextInputLayout and com.google.android.material.textfield.TextInputEditText for user input. Besides the text input, I need a date, which I want to read with a MaterialDatePicker.
I tried to display the MaterialDatePicker with OnFocusChangeListener, this works too, but I have two problems.
the display is a little bit delayed because first a keyboard is opened which is closed immediately after calling the MaterialDatePicker.
when the display is closed with the Back button, the focus is still on TextInputLayout. So I would have to change the focus first to open a MaterialDatePicker again.
This is how I implemented the OnFocusChangeListener
#Override
public void onFocusChange(View view, boolean selected) {
if( view.getId() == R.id.myId&& selected ){
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
MaterialDatePicker picker = builder.build();
picker.show( this.getParentFragmentManager(), "DATE_PICKER" );
}
}
Are there alternative components of Material Design that are better suited for the presentation? I would like to keep the behavior within the form, so as soon as the date is entered by the user, a small label should be displayed above, like this:
Thank you for your help.
I recently encountered the same problem.
The first issue concerning the keyboard, is solved by calling:
mTextInputEditText.setInputType(InputType.TYPE_NULL);
By setting the InputType to TYPE_NULL the keyboard won't open by clicking on the text field.
In addition, if you no longer want the user to be able to input any text, you can add:
mTextInputEditText.setKeyListener(null);
The second issue, to show the DatePicker again while it is already in focus, you can set an extra onClickListener:
mTextInputEditText.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
openDatePicker();
}
});
The OnClickListener is called as soon as the user clicks the text field again. Sadly it will not work with the first click.
You can look at this answer https://stackoverflow.com/a/11799891/9612595 for more information. Unfortunately, making the text field unfocusable resolves into weird behavior with the hint from Material.
I hope that helps!
Adding to luk321 answer. Instead of OnClickListener you can use OnTouchListener. For ex -
editText.setOnTouchListener((view, motionEvent) -> {
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
//your code
}
return false;
});
It will work on first touch. Be sure to use ACTION.UP otherwise event will occur while scrolling also.
deliverDatePicker.editText?.setOnClickListener {
viewModel.onDatePickerClick()
}
deliverDatePicker.editText?.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
viewModel.onDatePickerClick()
}
}
Overriding setOnFocusChangeListener as well as setOnClickListener solves the first unregistered click event of #luk321 answer

Why is EditText.setOnFocusChangeListener firing several times?

I'm using an EditText in a row in a ListView. There is no code to programatically request focus. When the ListView is populated none of the EditText views are focused. When I tap on one of them to give it focus, this is the log output:
Comments field focus changed. hasFocus=true
Comments field focus changed. hasFocus=false
Comments field focus changed. hasFocus=true
This is on a Nexus 7 running Lollipop 5.01 and the soft keyboard. All three lines relate to the exact same EditText View.
If I run the same build on a Nexus 5 also running 5.01 I see this, which is the behaviour I'd expect:
Comments field focus changed. hasFocus=true
This is the listener:
commentsView.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Timber.i("Comments field focus changed. hasFocus=" + hasFocus);
}
}
});
You'll see from the code that aside from logging to Timber (logcat) the event is not doing anything. Why is the View gaining, then losing and then gaining focus again on the N7? I need to use this event to trigger a data save and this behaviour is causing problems.
Any ideas?

Is there anyway to get my edittext highlighted?

I disabled softkeypad in my application because I have my own custom keypad. But the problem is when I clicked on the edittexts in order to enter data through my custom keypad ,that edittexts are not getting highlighted at all. Even cursor is not visible inside that respective clicked edittext. Why there are always side effects while disabling soft keypad? I tried all the suggestions that are there in the sources including stackoverflow, but nothing worked. Can I get the perfect solution to get the edittext highlighted when clicked?
you need to call textView.requestFocus() when clicked this way your editText can be highlight
dont forget also to add in your XML File
this attribute android:focusableInTouchMode="true" to your EditText
I don't know why those side effects occur, but in this post there is a workaround how disable the keyboard and still have the cursor. That worked for me except that I also needed to request focus, so it's:
//disable keypad
et.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = et.getInputType(); // backup the input type
et.setInputType(InputType.TYPE_NULL); // disable soft input
et.onTouchEvent(event); // call native handler
et.setInputType(inType); // restore input type
et.requestFocus(); // request focus
return true; // consume touch even
}
});

Obfuscate Password as soon as user exits EditBox

How can password be non-obfuscated until user leaves the EditBox, and then obfuscated afterward? I want the user to be able to see the password as they are entering it but obfuscate it as soon as they are done and have moved on to other fields.
You can do this using the PasswordTransformationManager class and an OnFocusChangedListener. Try this:
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean focused)
{
if(focused)
((EditText)view).setTransformationMethod(null);
else
((EditText)view).setTransformationMethod(PasswordTransformationMethod.getInstance());
}
});
This will essentially be the same as setting the xml attribute android:password="true", but only when EditText does not have focus.
I don't think android has built in functionality to do this, but you could probably do it pretty easily by subclassing TextView and changing the characters in the onFocusChanged method. Store the password chars in your variable and replace them with dots when the window loses focus. When it gains focus, get rid of the dots and put the chars back.

How can I react when an EditText is being focused?

I have an EditText that the user can write in, when the app starts there is already a string in the EditText. When the user clicks the EditText it becomes focused and the curser is where the user clicked the EditText text box.
I know that the code for setting the curser to the start is :
editText.setSelection(0);
But I don't know where to put this code, I tried to it in beforeTextChanged but it didn't do the job.
You can do this by setting an putting an OnFocusChangedListener. You'd do something like this:
et.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasFocus){
if(hasFocus){
((EditText)view).setSelection(0);
}
}
});
Where et is the text edit you want to set the listener on.
Full-discolsure: haven't tried this code out myself.
While there is probably a way to do this, I'm not entirely sure it's the best user experience, because when the user taps a text box at a specific spot, they really expect the cursor to be there. Imagine for instance if the user sees "abcd" written there and wants to edit that to "abcde", so they figure "I'll just tap at the end and append an 'e'". Imagine the user's frustration when that doesn't work as expected.
If you expect the user to edit the textbox, I'd consider leaving it empty. If you are using the existing text as a hint ("email#example.com"), it's probably a better idea to indicate that in some other way.

Categories

Resources