I saw different questions on the subject, but not exactly what I'm looking for. The question is: how to foce the IME to show up in full screen (extract) mode when editing a particular edit text? What I want is the IME to behave as it sees fit, except for that particular edit text, which I need the IME to go full screen on it.
I have tried the following, but that doesn't do it:
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(input != null) {
input.showSoftInput(mEditText, InputMethodManager.SHOW_FORCED);
}
}
}
});
Aside the 'why I'm doing this', any idea? I thought I could start an activity with a single edit text in it, but I'd prefer to use the IME's capabilities. Thanks.
I think the correct solution for this would be to start a new activity from your ime when you so desire that may contain just a text box, and once you are done editing, the ime can simply keep a copy of the text and paste the same to the original text box automatically.
I am not sure if this can be done as easily as i have said it, but i do believe this will be the correct way to go about it.
Related
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
Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});
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
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.
My question is: How can I update the display of the action button of the soft keyboard on the fly?
Another post seems to be about changing the editor action BEFORE the keyboard is shown: Android: Can't figure how to use setImeActionLabel. I would like to change the soft keyboard action button WHILE it is shown.
I have partially succeeded:
- I can change the action by using: editText.setImeOptions(EditorInfo.IME_ACTION_GO);
- I can also redraw the keyboard using: InputMethodManager.restartInput(editText);
I do this using TextWatcher.afterTextChanged.
- However, I have a problem with this. If I press a key, on top of that the keyboard shows which key has been pressed. But when I call restartInput(...), this also hides the view that shows which key has been pressed. This seems normal behaviour to me, but I need a way around it.
Extending EditText and overriding onKeyUp is not a good idea, see: https://groups.google.com/forum/?fromgroups#!topic/android-developers/RIxGfx5qOjM.
The KeyboardView class has exactly what I need (invalidateKey), but I think that is only accessible when you create your own input method.
Anybody else have some better ideas?
Please try to be more specific when you describe what do you want to do ... I needed to read your post many times to be able to understand your request ..
So what you want to do is how to react to any key pressed on keyboard and show info about it in action key on keyboard.
If that is not your target plz to explain more...
If yes, so I guess you would need to use additionaL EditText ( not visible ) lets call et0. after finishing inserting a letter or any pattern you define through ontextchanged in your edit text, change the focus to et0.. so you can change the actionKey.. and then send the focus back to your editText .. so you can thread or timertask, or any way to manage this temporary changing of focus into and from et0 ..
here its not necessary to restart the input.
hope that will help you ..
Have you tried changing /system/usr/keychars/default.kcm on-the-fly before invoking your redraw code? (filename might be different depending on version of Android).
There, you are allowed to set not only key output characters, but also key display values. I haven't tried it, it might also incur a delay, but it's worth a try.
This should be very useful for you.
EditText in = new EditText(this);
in.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int s, int b, int c) {
Log.i("Key:", cs.toString());
}
public void afterTextChanged(Editable editable) {}
public void beforeTextChanged(CharSequence cs, int i, int j, int k) {}
});