EditText formatting - android

Now I've got an EditText with TextWatcher which formats user input as a phone. For example if users input is 89011234567 it will be formatted as +7 (901) 123-45-67.
I wonder is there any way I can extend or implement some classes like CharSequence or Editableor any other like these to make EditText display the formatted output, but when I will call mEditText.getText().toString() it will return original users input?

You could create a class that extends the standard EditText, and then override the getText() method to return what you need.
public class MyEditText extends EditText {
//Implement the methods that need implementation. Calling the method in the super class should do the trick
#Override
public Editable getText() {
//return an Editable with the required text.
}
}
Then you just use the MyEditText instead of the normal EditText.
This solution will work, if you don't use the getText()-method other places (like your TextWatcher).

You could store the user's input into the tag property of your edittext, this way you could access it and it wouldn't be modified by using setText to display the formatted number !

Related

Semantics - Android EditText Class

I'm struggling a bit with some semantics on some basic Android/Java development. If I have the following code (which works) to gather user input from a textfield:
final EditText userInput=(EditText) findViewById(R.id.txtUserInput);
Is userInput an object or a variable? My understanding that it is an object being instantiated form the *EditText * class. What does the (EditText) do to the left of the findViewById. When I see open parens, I think casting. Can anyone provide some simple clarity?
You are correct in saying that userinput is an EditText Object, to be more specific it is an object that is a subclass of View. Everything you get back from the findViewbyId() method will be a View, which you then need to cast to the proper Object. The (EditText) is casting the View you got back from your xml to an EditText. This allows you to access methods from the EditText that are available to the EditText class in particular.
So whenever you use findViewById() you also need to cast the View you get to the Object that it represents.
Let me know if you need further help.
-Dejan
userinput is an object.
findViewById(xxx) returns a View object, but in your case you know that it will return an EditText. Therefore its possible to cast it with (EditText). And you can cast it from a View to EditText since EditText extends View.
When you have cast it to EditText you are able to find all methods exposed by EditText instead of only the methods exposed in View.

some EditTexts with only one TextWatcher

I'm trying to user TextWatcher interface in order to detect which EditText was changed.
I have an activity with 10 EditTexts, and it looks weird to use 10 TextWatchers for each one of them.
There is any way to use only one TextWatcher and to use switch statement on the Editable in the functions afterTextChanged?
What I would do is to create a class that extends EditText and create a TextWatcher in that class. You can then implement those EditTexts in your XML or create them programmatically in Java with the TextWatcher listening for each EditText.
Don't know if this will work for you but you can give it a try.
I have never tried this before, but it should work if you check if the EditText is in focus. There are a few ways to go about this and the most straightforward one is to check the focus of the EditText inside your TextWatcher methods. You'll need to do something like this:
if(mEdit1.hasFocus()) {
...
} else if(mEdit2.hasFocus()) {
...
} else if(mEdit3.hasFocus()) {
...
}
A different approach would be to use an OnGlobalFocusChangeListener on your root view and set a variable indicating with EditText currently has focus. It would still require a lot of if statements to check for which EditText has the focus, but may be a more reusable solution.

EditText: how to enable/disable input?

I have a 7x6 grid of EditText views. I want all of them disabled when the application starts, ie they should behave like normal TextViews and not to be editable. Then the user taps one cell in the grid, it changes its background and performs something visual. If the user clicks on the cell one more time it should allow editing. I'm struggling with OnClick() and OnFocusChange() listeners, but I can't accomplish such a basic interaction.
Playing with setEnabled() and setFocusable() doesn't help. I wonder why even a simple task like this has been made so difficult on Android
I finally found a solution. It's a matter of calling
setFocusableInTouchMode(boolean)
setFocusable(boolean)
when the EditText is first created, so it can intercept the clicks. Then one can set those flags back again to make the EditText editable, request the focus, and manually show/hide the soft keyboard with InputMethodManager methods
Try using this setFocusableOnTouch() instead of setFocusable() method.
Firstly write these line in your xml in EditText:
android:enabled="false"
And than use the code in java as shown below:
Boolean check = true;
yourEditText.setEnabled(check);
check=!check;
Setting input type to null is not enough, since it only suppress soft keyboard and if device has hardware keyboard, there will be input. So in order to suppress any editing you should do following:
editText.setInputType(InputType.TYPE_NULL);
editText.setFilters(new InputFilter[]
{
new InputFilter()
{
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend)
{
return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
}
}
});
That will guarantee that EditText content won't be changed
Since you are using gridview to achieve your concern you can do the following.
setOnItemClicklistener on gridview
Extend Edittext to make your own edittext view
The extedned class will contain boolean property named editable using this property in onItemclicklisterner of gridviewyou can call setEditable or setFocusabel or both for a editetext.
If you share your code i can elaborate more on this issue.
According the Android guide line please use LongKeyPress for the Question you have" If the user clicks on the cell one more time it should allow editing"
You can do the follwoing:
If you want to make edittext not editable then use following method
edittext.setInputtype(Null);
If you want to make edittext editable then use the same method and set the proper inputype
visit the following link for more info

How can I get EditText change?

I have a EditText and I want to inform each time text changes (with entering each character). What implementation should I use and what function should I override?
Use the addTextChangedListener method on your EditText and make your class implement or define an inner class implementing the TextWatcher class:
https://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)
You can declare a boolean variable as false and at each time et.edited make it true

Copy String from an android from EditText into String variable on Android

I have a class that creates a view to gather data via a function getView() that provides a view with an EditText.
This class has also has variable answer.
When the user chances the EditText I want to store the content of the EditText in answer.
If I would use an onKeyListener I fear that the answer will probably get stored before the last letter is entered.
Is there a good way to handle this in the getView() function via some other listener?
You should addTextChangedListener to your EditText and implement in your class TextWatcher
Then you will just take the text from the methods and store in your answer

Categories

Resources