Android Studio set a string in various textview

Asked

Viewed 495 times

1

I am studying the Android Studio and I ended up facing a problem, and I needed a light.

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

In this case I was using findViewById(); and setText(); to change the content of each of them, and it’s working so well.

More in the case are several textview and some of them will receive the same value for example 10 of them I will put the same String and other 3 will receive a different string

Thinking about the fact that there are several textviews that will receive the same string, is there any way to create a kind of clone of her that when she changes the others change together? to decrease repetitive commands.

1 answer

2


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

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>

Done this just filter all the elements of the type textView who own the tag definite.

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");
                    }
                }
            }
        });
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.