0

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.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49

2 Answers2

1

Try this,

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

from dialog fragment ,

((Your activity)getActivity()).changeText(text);
Shubham Vala
  • 1,024
  • 7
  • 18
  • Sorry for being a noob, but What's the name of my activity? Can I just put the name there or must I pass the value from the activity to the DialogFragment? – Raúl Parra Molina Aug 13 '18 at 11:18
1

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);
}
Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212