How to disable back button in all dialogs and layouts? - java

In my code:
#Override
public void onBackPressed() {
}
It works fine, but if I call a dialog, for example
final Dialog dialogPopupGewonnen = new Dialog(Start.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialogPopup.setContentView(R.layout.popup);
I can use the back button (the back button close the dialog popup). But I want to disable the back button in all layouts and dialogs.

You should override onBackPressed in all of your activities and for Dialog you can use setCancelable(false) like:
final Dialog dialogPopupGewonnen = new Dialog(Start.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialogPopup.setContentView(R.layout.popup);
dialogPopup.setCancelable(false);

Related

dismiss dialog window after onbackpressed

i have two activities. On 1st activity i have button after click on button it will show dialog window and after click on dialog button i will jump to 2nd activity.
On 2nd acitivity i have custom button which is calling onBackPressed() method:
private void btnBackListener(){
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
My Problem is when im on 2nd activity and i call onBackPressed(); it will jump into 1nd activity but also dialog is visibile. How to prevent showing that dialog after btnBack clicked in 2nd activity?
In your case, I think best option would be to close dialog before opening 2nd activity.

How to show alert dialog or dialog when press device back button or toolbar back arrow

I am using navigation component, and when I use device back button or toolbar back arrow I want to show alert dialog or custom dialog before navigate to previous fragment, if user press yes then navigate to the previous fragment, anything else dismiss like a dialog functionality. I already tried this:
OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
#Override
public void handleOnBackPressed() {
// Handle the back button event
}
};
this is my code for fragment and actions and i asked to how to handle the backarrow event or callback in navigation component architecture so as i move to previous fragment so before going to back it will display a alert are you sure to move from here like that.
<fragment
android:id="#+id/drawOnImageFragment"
android:name="com.consulthealthcare.app.fragments.DrawOnImageFragment"
android:label="Mark on Prescription"
tools:layout="#layout/fragment_draw_on_image">
<argument
android:name="uri"
app:argType="string" />
<action
android:id="#+id/action_drawOnImageFragment_to_confirmOrderFragment"
app:destination="#id/confirmOrderFragment"
app:enterAnim="#anim/enter_from_right"
app:exitAnim="#anim/exit_to_left"
app:popEnterAnim="#anim/enter_from_left"
app:popExitAnim="#anim/exit_to_right" />
<action
android:id="#+id/action_drawOnImageFragment_to_dialogNavFragment"
app:destination="#id/dialogNavFragment" />
</fragment>
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
This worked for the device back button but not for the navigation component toolbar back arrow.
on back button you have to override the onbackpress method
and inside it you would write your custom alert dialog
something like this:
#Override
public void onBackPressed()
{
// Create the object of
// AlertDialog Builder class
AlertDialog.Builder builder
= new AlertDialog
.Builder(context/* requireActivity() in fragment*/ );
// Set the message show for the Alert time
builder.setMessage("Do you want to go back ?");
// Set Alert Title
builder.setTitle("Alert !");
// Set Cancelable false
// for when the user clicks on the outside
// the Dialog Box then it will remain show
builder.setCancelable(false);
// Set the positive button with yes name
// OnClickListener method is use of
// DialogInterface interface.
builder
.setPositiveButton(
"Yes",
new DialogInterface
.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
// write what you want to do here for yes
}
});
// Set the Negative button with No name
// OnClickListener method is use
// of DialogInterface interface.
builder
.setNegativeButton(
"No",
new DialogInterface
.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
// If user click no
// then dialog box is canceled.
dialog.cancel();
}
});
// Create the Alert dialog
AlertDialog alertDialog = builder.create();
// Show the Alert Dialog box
alertDialog.show();
}
and for back arrow same concept
I would suggest to write a method for back arrow and back button contains the alert and call it when these button pressed
finally i have done the navigation component toolbar back arrow using
binding.toolbar.setNavigationOnClickListener(this);
#Override
public void onClick(View v) {
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navHostFragment);
Fragment fragment = navHostFragment.getChildFragmentManager().getFragments().get(0);
if (fragment instanceof HostFragment) {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawer(GravityCompat.START, true);
} else {
binding.drawerLayout.openDrawer(GravityCompat.START, true);
}
} else {
navController.navigateUp();
}
}
this line make the default functionality of the navigation
navController.navigateUp();

How to create a popup with a scroll view programmatically?

I need to create a popup window programmatically, with a scrollview is this possible? i need to do everything in java side.
I was using a alert dialog but maybe is better a popup window, but didn't find much information on how to do it programatically.
i was using this code for the alert dialog
AlertDialog.Builder alert = new AlertDialog.Builder(OFActivityA.this);
alert.setTitle("Something");
alert.setText("fe");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//what you need to do after click "OK"
}
});
alert.show();
i need to do everything in java side., not really - you can create your custom dialog with a custom layout and have any kind of layout that you would like to have.
For example, create dialogClass:
public class ProgressDialog extends Dialog {
public ProgressDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.progress_dialog); //this is your layout for the dialog
}
}
And all you need to do to show your dialog is call those line:
ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.show(); // this line shows your dialog
You can put all your logic into the class using java as you want to only that now you can control your layout and how it looks in easier way.

No keyboard on AlertDialog due to no focus on dialog box and still on background activity, how to fix?

I have created a edittext inside a list, which is in a Dialog. Initially edittext is disaled, by tapping on another button in dialog, i make it enabled. I debug it further and found that focus is still on background activity, which causes keyboard to come on background activity.
JUST to point out: my edit text is numeric.
Tried multiple options but no outcome:
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(null, InputMethodManager.SHOW_FORCED, null);
I found that keyboard is coming on background activity with this option after dialog open.
splitDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Both options doesn't work to open keyboard on dialog.
I have also added in editText box during XML generation.
My dialog code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
Dialog dialog = builder.create();
builder.setTitle("Split Balance");
builder.setPositiveButton("Ok", new SplitDialogCallback());
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
splitDialog.show();
Please give directions, as i am stuck due to this issue.
When i add one EditText in dialog directly instead of list view of dialog, things work fine, but if i remove it then again same issue. Not able to understand why focus not going if edit text in list view.
The android keyboard is quite intelligent that you would not need to toggle their input in this manner.Try this once.
remove this:
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(null, InputMethodManager.SHOW_FORCED, null);
and
splitDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
then i'd suggest using a custom dialog layout instead of a view.Try this.
final Dialog splitDialog = new Dialog(this);
splitDialog.setContentView(R.layout.abc_action_bar_decoryour_dialog_layout);
//Button that you want to toggle the editText enabled
Button btn = splitDialog.findViewById(R.id.your_button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText editText =splitDialog.findViewById(R.id.your_edit_text);
editText.setEnabled(true);
editText.requestFocus();
}
});
splitDialog.show();
This IMHO is a better way to use a dialog,it gives you more freedom esp since you're anyways using a custom dialog layout.
I know this does not tell why your method is not working..but its merely a better handled solution

how to keep my popup window on orientation change?

I have a popup window in my activity.
Whenever I change the screen orientation to landscape, the popup disappears.
Why is that, and how can I keep the popup visible?
try below code:-
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.i("orientation", "Orientation changed to: Landscape");
else
Log.i("orientation", "Orientation changed to: Portrait");
}
see below link for more info:-
How to keep Popup window opened when orientation changes at run time in Android?
When orientation changes the activity will restart.. So normally the popup window calls again.. In any case if it gone try to call it within onCreate.
Or check the orientation change and take necessary recalls.
if(getResources().getConfiguration().orientation == getResources()
.getConfiguration().ORIENTATION_LANDSCAPE){
// put some flag
}else if(getResources().getConfiguration().orientation != getResources()
.getConfiguration().ORIENTATION_PORTRAIT) {
// change the flag
}
If you put your code fragments may I can help you
You need to use managed Dialogs. Rather than
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.rule_edit_choose_action));
builder.setAdapter(new ArrayAdapter(this, R.array.dummyValues), null);
builder.show();
you should use something like
myActivity.showDialog(0);
and then implement onCreateDialog() in your Activity. Your activity will then manage the dialog and re-show it when you re-orientate and it's closed. If you need to change your dialog every time it is shown, implement onPrepareDialog() also - the Activity will give you access to the Dialog just before it is shown so you can update it (with a custom message, for instance).
There's lots of info here:
http://developer.android.com/guide/topics/ui/dialogs.html
As #Ciril said, your issue is that your Activity is restarted when you re-orientate. You could always fix your activity orientation to portrait or landscape if that is suitable for your app. That would prevent it from restarting.
most likely you use AlertDialog for your popups, something along the lines of:
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.popup_title);
builder.setMessage(R.string.popup_message);
builder.setPositiveButton(R.string.yes, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// do something
}
});
builder.show();
this is bad, because your Activity has no idea there's a popup dialog, and when you change screen orientation, the Activity is restarted with the new parameters, and your popup is gone.
to avoid this you'd better use ShowDialog() to display your popups. to make it work, you need to override onCreateDialog() :
// Called to create a dialog to be shown.
#Override
protected Dialog onCreateDialog(int id, Bundle bundle) {
switch (id) {
case NEW_DIALOG :
return new AlertDialog.Builder(this)
.setTitle(R.string.popup_title)
.setMessage(R.string.popup_message)
.setPositiveButton(android.R.string.ok, null)
.create();
default:
return null;
}
}
then you'd better override onPrepareDialog() (this is not required, actually):
// If a dialog has already been created, this is called
// to reset the dialog before showing it a 2nd time. Optional.
#Override
protected void onPrepareDialog(int id, Dialog dialog, final Bundle bundle) {
AlertDialog dlg = (AlertDialog) dialog;
switch (id) {
case NEW_DIALOG :
dlg.SetTitle("popup title");
// and maybe something else
}
}
after all preparations you may call ShowDialog(NEW_DIALOG) and you Activity will remember it has a popup laid over on the top, and will recreate it after the orientation change.

Categories

Resources