How to add content to a textview without having to copy what’s already in it?

Asked

Viewed 556 times

1

Guys, here’s the deal. I would like to know how to add a paragraph or a single word to a textview that already contains content without necessarily copying what it already has in it. I’d like you to show me something that fits any text.

An example of exactly what I want:

inserir a descrição da imagem aqui

If the checkbox is checked the displayed text is "Neo is". Otherwise "Neo is not".

inserir a descrição da imagem aqui

As I click on the "+1" button I wish it to be added after the content already existing in textview (Neo is/Neo is not) the status corresponding to the value of the cont variable.

If cont == 1 => existing text + unmarried. If cont == 2 => existing text + dating. If cont == 3 => existing text + married.

package genesysgeneration.wwww;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView tv01;
    private Button btnMais1;
    private int cont=0;
    private CheckBox cbNeo;

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

        tv01=(TextView)findViewById(R.id.tv01);

        addCheckBoxCondiction();

        addComplementText();

        btnMais1=(Button)findViewById(R.id.btnMais1);
        btnMais1.setOnClickListener(this);

    }
    public void addCheckBoxCondiction(){

        cbNeo=(CheckBox)findViewById(R.id.cbNeo);
        cbNeo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(((CheckBox)v).isChecked()){

                    tv01.setText(String.valueOf("Neo está"));

                }else{

                    tv01.setText(String.valueOf("Neo não está"));

                }

            }
        });

    }

    public void addComplementText(){

        if(cont==1){

            //tv01 => texto já existente + solteiro

        }

        if(cont==2){

            //tv01 => texto já existente + namorando

        }

        if(cont==3){

            //tv01 => texto já existente + casado

        }

    }

    public void onClick(View v){

        cont+=1;

    }

}

1 answer

2


TextView tv;

tv.setText(
    tv.getText().toString() +
    "seu novo texto"
);

I don’t understand if this is exactly what you need, but this way you will concatenate the String that is already inside Textview with your new text.

Particularly, I do not find this the most elegant solution, I recommend you to do some formatting class that receives some standard inputs and generate the text for you, because in the long term this current strategy is more difficult to maintain.

  • That’s exactly what I want to do. I wish to realize the concatenation of this String.

  • @Puppeteer if this is what you needed, accept the answer so that in the future other people know that this is the solution.

Browser other questions tagged

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