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.
Related
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
}
}
});
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.
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
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()
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.