-1

I have a dialog class with a custom layout. Now I want to change the text value and different on button click listener from different Activities. I am trying to do that But getting error. Here is my source code. Any help or suggestion will be very appreciating.

public class MyDialog extends Dialog {

    TextView dialogTitle, dialogMessage;
    Button dialogCancel, dialogOk;

    public MyDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

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

        //..................REFERENCE
        dialogTitle = (TextView) findViewById(R.id.txt_ttl);
        dialogMessage = (TextView) findViewById(R.id.txt_msg);

        dialogCancel = (Button) findViewById(R.id.btn_cancel_id);
        dialogOk = (Button) findViewById(R.id.btn_ok_id);

        //dialogTitle.setText("title has been changed");
    }

    private void dialogCancel(){

    }

    private void dialogOk(){

    }

    public void changeDialogTitle(String dTitle){
        dialogTitle.setText(dTitle);
    }
}

from other activities I am using this

    ...onCreate{...
    MyDialog myDialog = new MyDialog(this);
}

// on button click show dialog
public void showDialog(View view) {
    myDialog.changeDialogTitle("Title");
    myDialog.show();
}
Ravi
  • 319
  • 4
  • 13
  • what i get is you want different titles for different activities ? – Abdul Kawee Aug 16 '18 at 06:45
  • I have a custom layout which has two text view- 1) for title and 2) for dialog message. now I want to change the text String value from any activity of my choice – Ravi Aug 16 '18 at 06:49
  • you are calling `showDialog()` before dialog is even created, best solution is to pass the title as the argument and then set in the dialog – Abdul Kawee Aug 16 '18 at 06:58

4 Answers4

0

it's True maybe you can pass the title, as the argument

first add the parameter in your MyDialog Class

       public class MyDialog extends Dialog {

    TextView dialogTitle, dialogMessage;
    Button dialogCancel, dialogOk;
    String title;
    public MyDialog(@NonNull Context context, String title) {
        super(context);
        this.title = title;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

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

        //..................REFERENCE
        dialogTitle = (TextView) findViewById(R.id.txt_ttl);
        dialogMessage = (TextView) findViewById(R.id.txt_msg);

        dialogCancel = (Button) findViewById(R.id.btn_cancel_id);
        dialogOk = (Button) findViewById(R.id.btn_ok_id);
        // set your title in here
        dialogTitle.setText(title);
    }

    private void dialogCancel(){

    }

    private void dialogOk(){

    }

    public void changeDialogTitle(String dTitle){
        dialogTitle.setText(dTitle);
    }
}

and after that on your Activity call the function like this

MyDialog myDialog = new Mydialog(this,"My New Title");
myDialog.show
MOF
  • 163
  • 1
  • 10
0

Please see the below code snippet:

public class MyDialog extends Dialog {

    TextView dialogTitle, dialogMessage;
    Button dialogCancel, dialogOk;
    String title;

    public MyDialog(@NonNull Context context, final String title) {
        super(context);
    this.title=title;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

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

        //..................REFERENCE
        dialogTitle = (TextView) findViewById(R.id.txt_ttl);
        dialogMessage = (TextView) findViewById(R.id.txt_msg);

        dialogCancel = (Button) findViewById(R.id.btn_cancel_id);
        dialogOk = (Button) findViewById(R.id.btn_ok_id);

        dialogTitle.setText(title);
    }

    private void dialogCancel(){

    }

    private void dialogOk(){

    }

    public void changeDialogTitle(String dTitle){
        dialogTitle.setText(dTitle);
    }
}


----------


// on button click show dialog
public void showDialog(View view) {
 MyDialog myDialog = new MyDialog(this, "Title");
  myDialog.show();
}

MyDialog myDialog = new MyDialog(this, "Title"); You can send the title and message into the constructor and it will be set on the UI at the time of Dialog onCreate method.

Sunny
  • 3,134
  • 1
  • 17
  • 31
0

you got null pointer exception because you changed dialog title before showing Dialog. So, first show dialog and then change title. Change it to:

myDialog.show();
myDialog.changeDialogTitle("Title");
Rishav Singla
  • 485
  • 4
  • 10
0

Looks like you are trying to create a utility class independent to life cycle, you simply need to move the code of onCreate method to MyDialog Constructor like below.

public class MyDialog extends Dialog {

    TextView dialogTitle, dialogMessage;
    Button dialogCancel, dialogOk;

    public MyDialog(@NonNull Context context) {
        super(context);
        setContentView(R.layout.dialog_layout);

        //..................REFERENCE
        dialogTitle = (TextView) findViewById(R.id.txt_ttl);
        dialogMessage = (TextView) findViewById(R.id.txt_msg);

        dialogCancel = (Button) findViewById(R.id.btn_cancel_id);
        dialogOk = (Button) findViewById(R.id.btn_ok_id);

        //dialogTitle.setText("title has been changed");
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    private void dialogCancel(){

    }

    private void dialogOk(){

    }

    public void changeDialogTitle(String dTitle){
        dialogTitle.setText(dTitle);
    }
}

Now feel free to change the contents of your view anytime. It should work.

Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23