I have created custom dialog and I want to close it on Cancel button click. I searched on google, most of the people are using Dialog or AlertDialog but I am not using anything like that. This is my TextDialogActivity
which is loading on button click in my app. From MainActivity
I am just rendering another activity as custom dialog. When I click Save
button on the dialog I want to access data in parent activity, which is stored in a variable textData
in child activity.
public class TextDialogActivity extends AppCompatActivity {
TabHost tabHost;
private static final int FILE_SELECT_CODE = 0;
private String textData;
private Button browse;
private Button cancel_button1;
private Button cancel_button2;
private TextView text_preview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_dialog_layout);
browse = findViewById(R.id.browse_file_button);
text_preview = findViewById(R.id.text_preview);
cancel_button1 = findViewById(R.id.cancel_button);
cancel_button2 = findViewById(R.id.cancel_button2);
tabHost = findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec=tabHost.newTabSpec("tag1");
spec.setContent(R.id.encode_dialog_text_tab);
spec.setIndicator("Edit Text");
tabHost.addTab(spec);
spec=tabHost.newTabSpec("tag2");
spec.setContent(R.id.encode_dialog_browse_tab);
spec.setIndicator("Browse");
tabHost.addTab(spec);
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileChooser();
}
});
cancel_button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// close dialog
}
});
cancel_button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// close dialog
}
});
}
}
I added @style/Theme.AppCompat.Dialog
to my AndroidManifest.xml to make my dialog.
<activity
android:name=".activity.TextDialogActivity"
android:theme="@style/Theme.AppCompat.Dialog"
android:label="Secret Message">
</activity>
This is the screenshot.
My custom dialog/popup is an activity itself. I am not using Dialog or AlertDialog. For closing this dialog on Cancel button click, this is what I did:
cancel_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish(); // Call this method to close the dialog
}
});
And for accessing data from child activity(TextDialogActivity
) to parent(MainActivity
) activity, I did the same as @Kemo suggested here in answers. This is the code:
// From Parent activity(MainActivity) opening popup on button click
popUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getContext(), TextDialogActivity.class);
startActivityForResult(intent, TEXTFILE);
}
});
// From Child activity(TextDialogActivity) sending data to parent activity
save_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!secretText.isEmpty()) {
Intent intent = new Intent();
intent.putExtra("popup_data", secretText);
setResult(RESULT_OK, intent);
finish();
}
}
});
Now I want the result from child activity to parent activity for that I did this in onActivityResult
method:
// In parent activity get data on onActivityResult
if (requestCode == TEXTFILE && resultCode == getActivity().RESULT_OK){
secretText = data.getExtras().getString("popup_data", "");
}