views:

559

answers:

2

I'm writing a simple caesar-encryption-activity. Two EditTexts on screen, one clear-text, one crypted. Here's an example for the crypted EditText - the cleartext one is similar.

<EditText 
    android:layout_below="@id/Caesar_Label_CryptText"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:id="@+id/Caesar_Text_CryptText" 
    android:hint="Enter crypted text"
    android:maxLines="2" 
    android:lines="2" 
    android:minLines="2"
    android:inputType="text|textMultiLine|textVisiblePassword"
    android:scrollbars="vertical" 
    android:gravity="top" />

Now when entering cleartext I have an TextChangedListener running that programatically crypts and fills that crypto-EditText. So far, so good.

When the cleartext entered gets really long, the cleartext-EditText scrolls with my imput, but the crypto-EditText stays at the top of the text. I'd really like the crypto-EditText to scroll so that it always shows the bottom line of its content.

How can that be done, preferably from the onTextChanged()-Method of the TextWatcher?

A: 

The base class android.view.View has methods getScrollX(), getScrollY() and scrollTo() that may be helpful, though I haven't tried it.

http://developer.android.com/reference/android/view/View.html#scrollTo(int, int)

Luke Dunstan
I tried that. I log the getScrollY()-value of my cleartext-EditText. When entering text this value increases. Then I scrollToY() in the crypto-EditText with the same value for a start. But the 2nd EditText won't scroll. I debugged scrollTo() but the initially changed mScrollY-variable in View is reset to 0 by the time i call scrollTo() the next time. I currently suspect the cursor-position to be the culprit. Looking into that.
rflexor
A: 

Ok, found it. It was the cursor (called Selection on EditText and TextViews).

This is how I got it to work:

ivClear    // assigned the EditText that has the input
ivCrypt    // assigned the target EditText, that I want to scroll
aText      // the input from ivClear, crypted

Then use:

    ivCrypt.setText(aText);                               // assign the Text
    ivCrypt.setSelection(ivClear.getSelectionStart());    // scroll

Phew, finally :) Always underestimated the power of the Spannable ;)

rflexor