How can I react when an EditText is being focused? - android

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.

Related

EditText methods in java android

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

UI with two EditTexts - When one loses focus, how can I capture any text that was changed?

My UI has two EditText fields. When a user selects one of these EditTexts, then enters in some data, then clicks on the second EditText, the information from the first EditText is lost and it reverts to what was originally in the field.
Example:
EditText 1 = "Hello"
EditText 2 = "World"
User taps on the first EditText, and changes the text to "Hello,", then presses enter: Normal behaviour. Text is saved.
User taps on the first EditText, changes the text to "Hello,", then taps on the second EditText: Wrong behaviour. EditText 1 reverts back to "Hello".
How can I fix this behaviour? Both EditTexts use the same OnFocusChangeListener.
#Override
public void onFocusChange(View v, boolean hasFocus) {
String s = ((EditText) v).getText().toString().trim();
s = s.replaceAll(",", ".");
if (hasFocus) {
System.out.println(s);
//stuff
} else {
System.out.println(s);
//other stuff
}
I found that I can get the text that was lost in the else block. I can use this to set the text of the EditText. However, I've run into a new problem - If I add a radiobutton, I am not getting the lost text in that else block when the user goes from changing the EditText to clicking on the radiobutton. Instead, I am seeing the original text from this else block.
The easiest solution is to use two separate OnFocusChangeListener classes. This is a good situation to use anonymous inner classes.
Performing action on when edittext lost focus solved my first problem.
Android: Evaluate EditText after the user finishes editing
The answer from this question solved my second problem.

required field validation on android

In android how do I create a required field validation? like on a page where use can enter some data into some EditText. I wanted to make sure user enter something before continuing, like if the user forgot to enter some data, the app would not crash. I have done other validation like number only using those input-type provided. but so far I research I only found ways to validate content entered but not whether there is something entered.
So I should put something in the onCreate method like
if(EditText text is !=null){ do the stuff} else {error message}
But if I did that, the moment the app is run there will be error displayed.
And how do I write the "text of EditText" in c# I believe is TextBox Text. But I do not know how to do that in java android. I know setText but do not know how to refer to the content without changing it.
To get text user entered in the EditText you can call getText() method. I recommend you to perform validation after user clicks some button. Validating content of EditText inside onCreate() method is useless.
It's always better to tell the user that they need to put the correct information the earliest possible. Why? Because it allows the user to quickly identify the problem and fix it. That being said, I would recommend checking if its null or whatever you want the textfield to contain as soon as it looses focus.
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
//We get the text and then convert it to a string and check
if(v.getText().toString() == ""){
//Do what you want in this area
}
}
});
String str = textview.getText().toString().trim()

Watch over multiple EditText for text change in Android

This is what I intend to do:
I have an activity which have 20-25 EditText. When an EditText's text changes some corresponding TextView's text has to be changed. i.e. user inputs his monthly expenditure for groceries so the TextView which shows his yearly expenditure for grocery gets updated and so does the total monthly and yearly expenditure TextView.
I know that I can do it with TextWatcher.But in that case I'll need a separate TextWatcher for each of the EditText. Even if I write a custom TextWatcher which holds the EditText's Id and does the operation appropriate within a switch-case block but in that case I'll need a new instance of that class for each EditText. Isn't that very inefficient and memory consuming?
I was hoping to have something like the OnClickListener so that I would only have to Override some functions and it will do all the tasks like for all the Buttons in my Activity I just have to override the onClick(View v) function and its done. Is there a way to accomplish this?
How about something like this:
yourEditText1.setOnFocusChangeListener(yourFocusChangeListener);
yourEditText2.setOnFocusChangeListener(yourFocusChangeListener);
View.OnFocusChangeListener yourFocusChageListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// Update your textview depending on which edittext lost focus
}
}
});
The benefit to this over a TextWatcher (IMO) is that you know the user is done since they have navigated away from the EditText. The only issue is that they need to navigate away for it to fire (not sure if that would be a showstopper for you or not).
You can't avoid using a TextWatcher to "see" the input events on your EditText. Some time ago I answered a similar question, with a small modification you could probably use it to make things easy. In that class from my answer you would probably need to modify the onInputChange() method to also return the id of the EditText so you know who triggered the text input.

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.

Categories

Resources