Search code examples
javaandroidcustomdialog

change Main UI from custom Dialog Class


I have 2 class, Main and DialogOrder

Main

public class Main extends Fragment{
ImageView order;


@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {

     order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               new DialogOrder(getActivity()).show();
            }
        });

  return view;
  }
  public void init(Bundle savedInstanceState) {   
   order = (ImageView) view.findViewById(R.id.order);
   order.setImageResource(R.drawable.orderd);
   RelativeLayout.LayoutParams orderparams = new RelativeLayout.LayoutParams(Main.screenHeight / 8, Main.screenHeight / 8);
   orderparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
   orderparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    orderparams.setMargins(Main.screenHeight / 80, Main.screenHeight / 80, Main.screenHeight / 80, Main.screenHeight / 30);

    order.setLayoutParams(orderparams);


 }

  public void update_UI(){

    order.setVisibility(View.INVISIBLE);

}

}

DialogOrder

public class DialogOrder extends Dialog {
Button button;
Main main;
Activity context;


public DialogOrder(Activity context) {
    super(context);

    this.context = context;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.order_dialog);
 main = new Main();
 button = (Button)findviewbyid(R.id.bd);

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           main.update_UI();
           dismiss();
        }
    });
}}

what I want is to set order image INVISIBLE when the user press the button on dialog right now the code give me java.lang.NullPointerException

probably I try update the UI wrong so please can someone tell me what the right way to update parent UI class from child class ?


Solution

  • You can update your main fragment by passing it to Dialog constructor or you can use Listener/Callback to communicate between your main fragment and dialog.

    The best practice is is using Listener/Callback because a Dialog should not have access to the caller. This is also decouple Dialog from Main fragment.

    First, create the listener via interface in the dialog:

    public class DialogOrder extends Dialog {
      ...
      Activity context;
      private DialogListener listener;
    
      public interface DialogListener {
        void onButtonClicked();
      }
    
      public DialogOrder(Activity context, DialogListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
      }
    
      ...
    
    }
    

    Then, when button click call the listener:

    public class DialogOrder extends Dialog {
    
      Activity context;
      private DialogListener listener;
    
      public interface DialogListener {
        void onButtonClicked();
      }
    
      public DialogOrder(Activity context, DialogListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
      }
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        ...
    
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               listener.onButtonClicked();
               dismiss();
            }
        });
      }
    }
    

    Now, you can create the dialog with the listener. Something like this:

    DialogOrder.DialogListener listener = new DialogOrder.DialogListener() {
       @Ovveride
       public void onButtonClicked() {
         update_UI();
       }
    };
    
    DialogOrder dialogOrder = new DialogOrder(getActivity(), listener);
    dialogOder.show();
    

    The main fragment will be listening for button clicked in the dialog.