How to call a method that is inside my Fragment through the onclick of a button that is also inside the Fragment?

Asked

Viewed 642 times

0

How to call a method that is within my java Fragment through the onclick of a button contained in the same Fragment? Follow code:

public class Fragmentminhaconta extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.layout_fragment_minhaconta, null);
    Button botao = (Button) view.findViewById(R.id.editarMinhasInformacoes);

    return (view);
}

public void clicar(View v){
    mostrarMsg("Titulo teste", "Mensagem teste");
}

public void mostrarMsg(String titulo, String mensagem) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setCancelable(true);
    builder.setTitle(titulo);
    builder.setMessage(mensagem);
    builder.show();
}

}

1 answer

3


You need to use the Onclicklistener on the button, this way:

botao.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       mostrarMsg("Titulo teste", "Mensagem teste");
   }
});

No need to call the click() method, you can directly call the method that displays the message.

Hugs.

  • Thank you. Solved my problem.

Browser other questions tagged

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