How can I change the textView of an Activity from a DialogFragment?

This is the java file of the activity

private TextView tv;

 private Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_agente);
        tv = (TextView) findViewById(R.id.Estado);
    }

    //Método para el bottón rojo

    protected void BotonRojo(View view){
        DialogFragment dialogFragment = new DialogoOcupado();
        dialogFragment.show(getSupportFragmentManager(),"Un Dialogo");
    }


Thats the DialogFragment class(extends DialogFragment)


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle("Aviso")
                .setMessage("Vas a pasar a estado Ocupado, estás seguro?")
                .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }
                )
                .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setNeutralButton("Más Tarde", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

        return builder.create();
    }

I want to change the TextView(tv) when the user press the button "Aceptar". How can I do it? I'm learning programming Android sorry.

Upvotes: 0

Views: 535

Answers (2)

Khemraj Sharma
Khemraj Sharma

Reputation: 58964

There can be many ways to do this. Simplest I think is this...

Create a method in your Activity. and call it from Dialog.

((YourActivity)getActivity()).changeStatus("pass anything");

make changeStatus() method in your Activity.

public void changeStatus(String s){
   tv.setText(s);
}

Upvotes: 1

Shubham Vala
Shubham Vala

Reputation: 1043

Try this,

public void changeText(String text){
    textView.setText(text);
}

from dialog fragment ,

((Your activity)getActivity()).changeText(text);

Upvotes: 1

Related Questions