Reputation: 83
First of all, I apologize for my bad English Grammar.
There is a toggleButton
and EditText
in my Simple Android Application. When I click the toggle button editText
Box change into the password field and a normal text field.
That is working perfectly, but the problem is when I click the toggle button cursor always goes to the beginning of the Text.
Example: When Type stack it shows as ..... after clicking toggle button that words appear but cursor goes into the front of 's' it does not stay after 'k'
How can I correct this issue?
This is EditText
and toggle Button xml
code
<EditText
android:id="@+id/etCommand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="@string/command"
android:password="true" />
<ToggleButton
android:id="@+id/tglBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="75"
android:checked="true"
android:onClick="changeField"
android:paddingBottom="5dp"
android:text="ToggleButton" />
and I change its behavior using the following code
if (passTog.isChecked())
{
input.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
else
{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
Thanks in advance.
Upvotes: 4
Views: 3121
Reputation: 364
Here test using this code to put the cursor at the end of the EditText
:
yourEditText.setSelection(yourEditText.getText().length());
Upvotes: 0
Reputation: 83
I found the answer by myself
if (passTog.isChecked())
{
input.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);
input.setSelection(input.length());
}
else
{
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setSelection(input.length());
}
Upvotes: 3
Reputation: 6965
I suggest doing the following
int selstart = input.getSelectionStart();
int selend = input.getSelectionEnd();
input.setInputType(passTog.isChecked() ? (InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD) : (InputType.TYPE_CLASS_TEXT));
input.setSelection(selstart,selend);
Upvotes: 0