1

I have a custom textview class with this code:

    public CustomTextView extends TextView {

        public CustomTextView(Context c, AttributeSet a) {
            super(c, a);
            TextView tv = (TextView) findViewById(R.id.myTv);
            String str = tv.getText().toString();

            setText(str);
        }
    }

And my main.xml contains this:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myTv"
        android:text="Unkown" />

    <com.test.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="100.0sp" />

What I want to do is I want to get the string value of the TextView(with ID of myTv) and set the text of my CustomTextView same as the TextView. Something like TextView = CustomTextView.

The string value of myTv is changed by a sharedpreferences so everytime the text changes, the custom text also will change.

My problem is how to do this correctly?

1 Answers1

0

This is really simple you just initialize both textviews and get string from one and set it in another. Extending your class with TextView gives you all methods TextView can use.

CustomTextView customtv = (CustomTextView) findViewById(R.id.customTv);
TextView tv = (TextView) findViewById(R.id.myTv);
customtv.setText(tv.getText());

Just set id on your CustomTextView whatever you want i used customTv id in this case.

EDIT:

As far as i understood you want textChangeListener, here is example.

Community
  • 1
  • 1
Ivan Marincic
  • 58
  • 2
  • 7