Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can pass the text to the custom dialog as an argument by creating a constructor in your custom dialog class that takes a String parameter. Then, when you create an instance of the dialog in the different activity or class, you can pass the text as an argument to the constructor. Here is an example:

CustomDialog.java:

public class CustomDialog extends Dialog {

    private String text;

    public CustomDialog(Context context, String text) {
        super(context);
        this.text = text;
    }

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

        TextView textView = findViewById(R.id.text_view);
        textView.setText(text);
    }
}

DifferentActivity.java:

public class DifferentActivity extends AppCompatActivity {

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

        String text = "Hello world!";

        CustomDialog customDialog = new CustomDialog(this, text);
        customDialog.show();
    }
}

In this example, we create a new instance of the CustomDialog class in the DifferentActivity class and pass the String "Hello world!" as an argument to the constructor. The text is then set to a TextView in the custom dialog layout.