Ask Your Question
2

How can I assign the text to a custom dialog that I have generated from a different activity or class?

asked 2023-07-18 20:05:56 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-18 20:13:01 +0000

lakamha gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-18 20:05:56 +0000

Seen: 8 times

Last updated: Jul 18 '23