Is there anyway to get my edittext highlighted? - android

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
}
});

Related

android spinner shows unwanted keyboard

I have an activity in which there are several buttons and a spinner containing a list of preset values. Unfortunately, if the user accidentally or mistakenly taps the spinner, the soft keyboard appears. Why? There is nowhere on the screen that expects typed input from the user.
More to the point, how can I prevent this from happening? After some research, I tried adding the following code:
m_TricksPicker.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm=(InputMethodManager)getApplicationContext().
getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getParent().getCurrentFocus().getWindowToken(), 0);
return false;
}
}) ;
Unfortunately, this throws a null pointer exception. Any other suggestions how to stop the keyboard from appearing (and I'd really like to understand why the system thinks it's necessary in the first place, when no user input is required)?
Sorry for the delay, but I have redesigned the interface not to use a spinner at all.

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?

edittext.setOnFocusChangeListener

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

Can I add special button to android default keyboard

I know that there is done button that appear in keyboard , but I want to put button on my keyboard and name it save , so the user can use it to save the record which he is entering now
if there is another suggestion to do that , please don't hesitate to say it :)
Note : I have some multi line text-boxs so I cannot use done button.
Put a Button in your layout to save it. It is much easier. Put this line in your xml below the record fields like in a registration page :-
<Button id=#+id/save
layout_width="wrap_content"
layout_height="wrap_content"/>
In your main activity, on this Button listener, write the code which you were supposed to write on the save button for keyboard.
You cannot customize Android default keyboard.
However for the last EditText to be filled, you can specify an action such as Done:-
android:imeOptions="actionDone"
You can fire a listener for that which is called onEditorAction :-
lastEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do something
}
return false;
}
});

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