4

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.

Younes
  • 462
  • 7
  • 15
sachith
  • 83
  • 1
  • 7

3 Answers3

3

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());
    }
sachith
  • 83
  • 1
  • 7
0

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);
SteelBytes
  • 6,905
  • 1
  • 26
  • 28
0

Here test using this code to put the cursor at the end of the EditText:

yourEditText.setSelection(yourEditText.getText().length());
rekire
  • 47,260
  • 30
  • 167
  • 264
krunal shah
  • 364
  • 2
  • 14