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();
}
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