Android Studio set a string in several textview

1

I'm studying Android Studio and I ended up with a problem, and I needed a light.

I made several textView on the screen of my application and when I hit a button I would change the contents of all textViews that are on the screen.

In case I was using findViewById (); and setText (); to change the content of each of them, and is working well.

More in the case are several textview and some of them will receive the same value for example 10 of them will go by the same String and another 3 will receive a different string

Thinking about having multiple textviews that will receive the same string, is there any way to create a kind of clone of it that when it changes the other changes together? to decrease repetitive commands.

    
asked by anonymous 24.02.2018 / 03:51

1 answer

2

Use the android:setTag="sua-tag" attribute in XML or the textView.setTag("sua-tag") method.

Example:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="br.com.valdeirsantana.stackoverflow.MainActivity"
    android:id="@+id/container">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="sua-tag"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.392" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="sua-tag"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.247" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="sua-tag"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.14" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="sua-tag"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.032" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="271dp" />

</android.support.constraint.ConstraintLayout>

This is done by filtering all elements of type textView that have the defined tag .

public class MainActivity extends Activity {

    private ViewGroup container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        container = findViewById(R.id.container);

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /* Percorre todos os filhos da view Container */
                for (int count = 0; count < container.getChildCount(); count++) {

                    /* Captura a view filho */
                    View currentView = container.getChildAt(count);

                    /* Verifica se ela é do tipo TextView, converte a tag para String e verifica se é igual a tag definida */
                    if ( currentView instanceof TextView && String.valueOf(currentView.getTag()).equals("sua-tag")) {
                        ((TextView) currentView).setText("Novo Texto");
                    }
                }
            }
        });
    }
}
    
24.02.2018 / 04:12