I have an EditText that I want to use so people can input a short bio.
So I am trying to make it so it's fixed at, for example, a box that is 4 lines high. I don't want it to "scale" or "shift" with the input -- I'm trying to make it a fixed box of a fixed number of lines with word-wrap.
I did try adding lines="4" and maxLines="4" and inputType="textCapSentences|textMultiLine" but it doesn't quite seem to be right. When I set text, it appears in the middle of the EditText (and not the upper left), and it seems to let me hit enter a whole lot so I can have a word in the first row and then a character like 20 rows down.
Current XML:
<EditText
android:background="#00ff00"
android:id="#+id/editTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="4"
android:maxLines="4"
android:inputType="textCapSentences|textMultiLine"
android:gravity="left|top"/>
I'm using a background of green so I can more easily see it for now. Right now this lets you type as much as you want, but I want to limit it to the space as given.
There is in no built code to achieve what you need. But here is a workaround -
private String enteredText;
edtText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (edtText.getLineCount() > 4) {
edtText.setText(enteredText);
edtText.setSelection(edtText.getText().length()); //This statement is to move the cursor at the end of the text otherwise it'll be moved to the start of the text.
}
else {
enteredText = edtText.getText().toString();
}
}
});
Hope this helps !!
to fix the centered text issue add:
android:gravity="top|left"
To prevent the user from inputting more then 4 lines, you'll need to do it by code.
Add a TextWatcher to the EditText, and check the number of lines after each text-change:
TextWatcher textWatcher = new TextWatcher() {
onTextChanged(CharSequence s, int start, int before, int count) {
// Check number of lines here
}
};
editText.addTextChangedListener(textWatcher);
Just add this attribute to your edittext
android:inputType="textMultiLine"
Related
I am using a view to enter user's four digit PIN. the UI consists of 4 edittext each having maxLength set to 1. On entering value to edit text1, focus is then shifted to the second edittext and so on. each Edittext input type is : numberPassword.
<EditText
android:id="#+id/ed_1"
android:layout_width="20dp"
android:layout_height="20dp"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1" />
In java, to shift the focus, we have applied following text watcher to each of the edittext:
ed1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() == 1) {
ed1.clearFocus();
ed2.requestFocus();
ed2.setCursorVisible(true);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Problem is, value of edittext are not changing to *, it shows the real input number (not changing to password). Only 1st and last are changing to *, where as center two that means edittext 2 and edittext 3 are still have visible passwords.
Right now: We have 4 input Edittexts
desired output should be: | * | * | * | * |
When we fill the values, and switches the focus, by using java code specified, they get change to: (input value: 1234): | * | 2 | 3 | * |
If we focus back on either of the center edit texts, then value changes to password. Is it because of removing focus, and the transformation time required to change a field to password takes more time? Is there any method with which we can forcefully change the value to password.
Try running the following code for the previous text after the transitions
yourEditText.setTransformationMethod(new PasswordTransformationMethod());
it's a difficult method but
String e1,e2,e3,e4;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() == 1 && !ed1.getText().toString().equals("*")) {
e1=ed1.getText();
ed1.setText("*");
ed2.requestFocus();
ed2.setCursorVisible(true); } }
you can use this method
Because you set the maxLength to 1, so it will get single character only. Also you written code for clearFocus in the onTextChanged, so it will set focus to the next EditText. You have to change as like below in your xml.
<EditText
android:id="#+id/ed_1"
android:layout_width="20dp"
android:layout_height="20dp"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="4" />
Also remove the clearFocus in your onTextChanged().
It seems like TextWatcher need some time to chenge it to password type,
so i came up to this solution with using Handler and it works fine. Try below code
ed1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() == 1) {
// use Handler for 1 second delay so that it will change text
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ed2.requestFocus();
}
},1000);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Just remove ed1.clearFocus(); from your code. Do not force the edittext to loose his focus, just tell the other edittext to regain the focus ed2.requestFocus(); . The previous edittext automatically remove its focus as soon as next edittext gain the focus. It will first transform the value then leave its focus.
Just move your code from onTextChanged to afterTextChanged, and you donĀ“t need clearFocus, just requestFocus.
This way you give time to obfuscate the number before change focus.
Below is an example that replicates my lagging problem. Once I set the text on the EditTextView it takes at least 1.5 seconds for the user to be allowed to input another character.
amountEditText.addTextChangedListener(new TextWatcher() {
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override public void afterTextChanged(Editable s) {}
#Override public void onTextChanged(CharSequence s, int start, int before, int count) {
String amount = s.toString();
if( !amount.equals(current )) {
amountEditText.removeTextChangedListener(this);
amountEditText.setText(s);
Selection.setSelection(amountEditText.getText(), amountEditText.getText().length());
amountEditText.addTextChangedListener(this);
}
}
});
I've searched around and have not found a solution.
I identified that the issue was coming from the textView.setText() call.
The solution was to not use setText(), instead use the Editable that is provided to you in the onTextChanged callback.
I tried to use the Editable before, however i couldn't get it working with inputs such as "$12,000".
This was due to having InputFilters still attached to the Editable.
Regards,
Scott.
It lags because you remove and then readd your listener.
You seems to be trying to select the text inside the EditText. To do so, simply set editText.setSelectAllOnFocus(true); or android:selectAllOnFocus="true" in xml. Then remove the entire TextChangedListener from your code.
According to me it is most probably because of trying to remove (amountEditText.removeTextChangedListener(this);) and add (amountEditText.addTextChangedListener(this);) the Text Change Listener every time. I would recommend you to replace your code without those adding and removal.
Hope this helped.
I'm needing to get an integer from the user for one of my apps and I have tried using a text edit but it didn't seem to work, so I'm wanting to know another way of getting an integer from the user. The int will be positive numbers only and no more than 2 digits.
Use EditText
You can limit the number of digits like this
Update:
Then you need to add a listener
http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)
a relevant question:
android edittext onchange listener
You have to use EditText.. then from in Your Activity
String s = ed.getText().ToString();
int i = 0;
if(s!=null)
i= Integer.valueOf(s);
To make sure keyboard only show numbers,
make sure you add
android:input="number"
to your EditText in the XML
UPDATE
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String s = yourEditText.getText().ToString();
int i = 0;
if(s!=null)
i= Integer.valueOf(s);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
you can use properties in your XML .
use this line to limit input text in numbers in XML :
android:inputType="number"
and use this line to set your specific character:
android:digits="1234567890"
i think it is the best way for this purpose.
I have 3 widgets on my screen, Text View, Edit Text and a Button. What ever I insert in my edit text, when I click the button, the text view gets the string from the edit text. Now, what I want to do is that, if I have already inserted the character "\" or "," or what ever character I want, it will not be inputted anymore. It's like, you can only put that character once in the edit text. Do you guys have any idea about it?
Well what I am thinking is that, I have to search from the edit view then validate it. But I don't know what code to use. Could somebody please help me? Thank you!
Use Android TextWatcher on EditText.
There are delegates which returns the charsequence that is entered
onTextChanged
afterTextChanged
beforeTextChanged
Fill the entered character in set everytime. If size is not incrementing that means a duplicate.
then avoid adding of that character in edittext
I think you need to add a TextChangedListener to your editText.
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
I am using EditText and providing some Hint there. I am putting it like this:
android:hint="#string/user_name_hint"
android:textColorHint="#ffffff"
android:gravity="left"
But here hint is coming in the left side. But i want the hint is in the middle of the box and when i am clicking the text should start from the left.
How it is possible??
Use this EditText
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="user_name_hint"
android:textColorHint="#fffff"
android:gravity="center_horizontal"
android:id="#+id/editText1"/>
Then you need to add addTextChangedListener to solve your problem.
final EditText hintText = (EditText) findViewById(R.id.editText1);
hintText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
hintText.setGravity(Gravity.CENTER);
} else {
hintText.setGravity(Gravity.LEFT);
}
}
});
set Gravity to center.. android:gravity="center_horizontal"
Not sure you can do it in a "straight forward way". Probably some workaround - either suggested by nagesh or by placing a button on top of the edit text and switching to edit text when it gets clicked or focused