I have been able to confirm exit before leaving fragment. However my activity has six cards each of which calls a fragment when clicked. The confirm exit applies to all fragments in this activity. How can I apply to individual fragment say to the Garden Mapping card which calls its own fragment(It is called GpsFragment). I need to confirm exit before leaving this Fragment(GpsFragment)
This is my main menu
This is my code in the main activity
#Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 1) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing EzyAgric")
.setMessage("Are you sure you want to close EzyAgric?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
//additional code
} else {
getFragmentManager().popBackStack();
}
}
Define an interface to comunicate with your activity and fragment
public abstract class BackPressFragment extends BaseFragment {
public abstract boolean processBackPress();
}
YourFragment extends BackPressFragment {
.....
}
And process key event in your activity, pass event through the interface.
Hi you need to send the onBackPressed() event to the fragment. Best way to do that is via an interface.
Create an Interface like this:
public interface IOnBackPressListener {
void onBackPressed();
}
Now implement your interface in the fragment like this:
public class MyFragment extends Fragment implements IOnBackPressListener {
public MyFragment(){
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onBackPressed() {
Log.e("Fragment","On Back pressed");
// Alert code comes here
}
}
Now in your activity write the following code in onBackPressed()
#Override
public void onBackPressed() {
IOnBackPressListener fragment=((IOnBackPressListener)
getSupportFragmentManager().findFragmentById(R.id.container));
if(fragment!=null)
fragment.onBackPressed();
}
Related
I am trying to implement confirm message to exit from my app. I need this, because someone can accidentally click back button more than one time and this will close the app and in case of low memory it will be killed after that because now it is not in foreground.
I have tried different approaches, but some of them required a lot of checks, others doesn't work at all.
I have tried to use onKeyDown event, onBackPressed ...
The problem because I am working not with only with activities, but also with nested fragments (inside activities).
I need to handle the last on back pressed click before exit, so it means that all fragments of current activity have to be popped up from the stack, than activity has to be popped up also and that if this is not last activity do the same for the preceding activity until this is not last activity and all fragments are popped up in it.
How can I implement this ? I have tried to do this using backstack, but unfortunately haven't succeed.
Please suggest what is the best to handle such type of event. I guess that there is an easy way to do this.
Thanks everyone for answers and suggestions.
I have reached the desired result by using fragment count in the stack.
As far as my Main Activity will be the first activity and the last before exit, I can override onBackPressed method inside it.
So solution is simple.
public class MainActivity extends BaseSingleFragmentActivity {
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.title_exit_message)
.setMessage(R.string.message_exit)
.setPositiveButton(R.string.button_text_yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MainActivity.super.onBackPressed();
}
})
.setNegativeButton(R.string.button_text_no, null)
.show();
} else {
MainActivity.super.onBackPressed();
}
}
}
So this will work only if your activity is last in Activity Stack , so you have to override it your launcher activity, not in some base activity class or other.
A little bit improved solution to my mind.
package com.crosp.solutions.qrcodereader.dialogs;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import com.crosp.solutions.qrcodereader.R;
import com.crosp.solutions.qrcodereader.constants.FragmentConstants;
/**
* Created by crosp on 7/9/15.
*/
public class ConfirmationDialog extends DialogFragment implements DialogInterface.OnClickListener {
private OnConfirmDialogClickListener mOnConfirmDialogListener;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mOnConfirmDialogListener = (OnConfirmDialogClickListener) activity;
} catch (ClassCastException ex) {
Log.e(getString(R.string.error_tag), "Activty has to implement " + OnConfirmDialogClickListener.class.getSimpleName() + " interface");
}
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setPositiveButton(R.string.button_text_yes, this)
.setNegativeButton(R.string.button_text_no, this);
Bundle bundle = getArguments();
String message = getString(R.string.message_exit);
String title = getString(R.string.title_exit_message);
int iconId = android.R.drawable.ic_dialog_alert;
if (bundle != null) {
String argumentMessage = bundle.getString(FragmentConstants.Arguments.DIALOG_MESSAGE_ARGUMENT);
String argumentTitle = bundle.getString(FragmentConstants.Arguments.DIALOG_TITLE_ARGUMENT);
int argumentIconId = bundle.getInt(FragmentConstants.Arguments.DIALOG_ICON_ID_ARGUMENT);
message = argumentMessage != null ? argumentMessage : message;
title = argumentTitle != null ? argumentTitle : title;
iconId = argumentIconId != 0 ? argumentIconId : iconId;
}
builder.setIcon(iconId);
builder.setMessage(message);
builder.setTitle(title);
return builder.create();
}
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
mOnConfirmDialogListener.onConfirmClick();
} else if (which == DialogInterface.BUTTON_NEGATIVE) {
mOnConfirmDialogListener.onCancelClick();
}
}
public interface OnConfirmDialogClickListener {
void onConfirmClick();
void onCancelClick();
}
}
And in activity
public class MainActivity extends BaseSingleFragmentActivity implements ExitConfirmDialogFactory.OnExitDialogClickListener {
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
Bundle arguments = new Bundle();
if(mExitDialog==null) {
arguments.putInt(FragmentConstants.Arguments.DIALOG_ICON_ID_ARGUMENT, android.R.drawable.ic_dialog_alert);
arguments.putString(FragmentConstants.Arguments.DIALOG_MESSAGE_ARGUMENT, getString(R.string.message_exit));
arguments.putString(FragmentConstants.Arguments.DIALOG_TITLE_ARGUMENT, getString(R.string.title_exit_message));
DialogFragment exitDialog = new ConfirmationDialog();
exitDialog.setArguments(arguments);
mExitDialog = exitDialog;
}
mExitDialog.show(getSupportFragmentManager(),FragmentConstants.Tags.EXIT_DIALOG_TAG);
} else {
super.onBackPressed();
}
}
#Override
public void onConfirmClick() {
super.onBackPressed();
}
#Override
public void onCancelClick() {
}
Basically override onBackPressed() in the Main Activity and avoid calling the parent super.onBackPressed() if the user selects "No" to exiting the app.
Code suggestion:
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Exiting app?")
.setPositiveButton("Yes", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// user really do want to exit
MainActivity.super.onBackPressed();
}
}).create().show();
// If negative, show a Fragment or do nothing
}
I am not sure but I think you can use an integer to count your fragments, increase it on adding a new fragment and decrease on every back press. It may be like that:
#Override public void onBackPressed() {
if( fragCount > 0) {
--fragCount;
super.onBackPressed();
return;
}
new AlertDialog.Builder(this) .setTitle("Exiting app?") .setPositiveButton("Yes", new OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // user really do want to exit
MainActivity.super.onBackPressed(); } }).create().show();
}
Note: Sorry for bad typing I am on phone.
I have a listfragment in an activity, a dialogfragment will open on pressing on a row for the further options, now I rotate the device and choose a option from dialogfragment, its throws an IllegalStateException .... Fragment is not attached.
choiceDialog
.setOnClickListDialogClickListener(new StandardListDialogFragment.OnClickListener<String>() {
#Override
public void onClick(DialogInterface dialog, int which,
String value) {
Intent intent = new Intent(this.getActivity(),
DutyEditor.class);
startActivityForResult(intent, 0);
dialog.dismiss();
}
});
This happens because the Fragment is destroyed and re-created with a FragmentManager whenever orientation changes.
You should stick to these rules when working with Fragments
Avoid setters when using Fragments.
Never hold a field reference to a Fragment in an Activity.
You can hold a field reference to an Activity on a Fragment after onAttach() and before onDetach(), but I find it better to call getActivity() and check for null every time I need it for short operations.
The best option is to call startActivityForResult() in the DialogFragment itself.
But whenever you really need to deliver the DialogFragment click events to the Activity, use an Interface. For example.
public final class SomeDialogFragment extends DialogFragment {
/**
* Callbacks of {#link SomeDialogFragment}
*/
public interface SomeDialogFragmentCallbacks {
/**
* Called when user pressed some button
*/
void onSomeButtonClick();
}
#Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
// Make sure the Activity can receive callbacks
if (!(activity instanceof SomeDialogFragmentCallbacks)) {
throw new RuntimeException("Should be attached only to SomeDialogFragmentCallbacks");
}
}
// now whenever a button is clicked
#Override
public void onClick(DialogInterface dialog, int which) {
final SomeDialogFragmentCallbacks callbacks = (SomeDialogFragmentCallbacks) getActivity();
if (callbacks != null) {
callbacks.onSomeButtonClick();
}
}
}
And all Activities that use this DialogFragment should implelent the callbacks method.
public final class SomeActivity extends Activity implements SomeDialogFragmentCallbacks {
#Override
public void onSomeButtonClick() {
// Handle some DialogFragment button click here
}
}
The dialog:
public class ClearDialog extends Dialog {
private MainActivity context;
public ClearDialog(MainActivity context) {
super(context);
this.context = context;
setContentView(R.layout.clear_dialog);
setTitle("something");
setCanceledOnTouchOutside(false);
setCancelable(true);
}
/* not overriding anymore
#Override
public void onBackPressed() {
return;
}
still doesnt work */
#Override
protected void onStart() {
super.onStart();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
ResourceHelpers.removeAllResources();
context.onResourcesDeleted();
}
}
The Activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.itemLogoff:
loginDialog.show(); //this is another dialog
break;
case R.id.itemSync:
Intent syncer = new Intent(MainActivity.this, SyncActivity.class);
MainActivity.this.startActivity(syncer);
break;
case R.id.itemClear:
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_action_alert)
.setTitle("something")
.setMessage("something")
.setPositiveButton("something", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
showDeleteDialog();
}
})
.setNegativeButton("something", null)
.show();
break;
}
return true;
}
private void showDeleteDialog() {
cd = new ClearDialog(this); //this is the dialog
cd.show();
}
public void onResourcesDeleted() {
cd.dismiss();
loginDialog.show();
}
So.. The user clicks on "Delete all data" from the ActionBar (optionsmenu). I open an AlertDialog asking if he's sure. Then if he's sure, I open a dialog that shows a spinning ProgressBar.
The problem: it won't dismiss!
The loginDialog (all data is lost so I want the user to login again...) comes up in the background. The ClearDialog won't dismiss...
I think that the problem is here (don't override in this way that method):
#Override
public void onBackPressed() {
return;
}
You can already obtain a modal dialog with .setCancelable(false)
Please take a loog at this documentation: http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
Give the following property for dialogue
.setCancelable(true);
its just like .setTitle() or .setMessage in your code....
On top of StErMi's answer, which you should follow, also switch the two lines in your onResourcesDeleted() method. The login dialog is called, and takes over before your dismiss is called.
public void onResourcesDeleted() {
cd.dismiss();
loginDialog.show();
}
I wrote a piece of code that will give the user a prompt asking them to press back again if they would like to exit. I currently have my code working to an extent but I know it is written poorly and I assume there is a better way to do it. Any suggestions would be helpful!
Code:
public void onBackPressed(){
backpress = (backpress + 1);
Toast.makeText(getApplicationContext(), " Press Back again to Exit ", Toast.LENGTH_SHORT).show();
if (backpress>1) {
this.finish();
}
}
I would implement a dialog asking the user if they wanted to exit and then call super.onBackPressed() if they did.
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
WelcomeActivity.super.onBackPressed();
}
}).create().show();
}
In the above example, you'll need to replace WelcomeActivity with the name of your activity.
You don't need a counter for back presses.
Just store a reference to the toast that is shown:
private Toast backtoast;
Then,
public void onBackPressed() {
if(USER_IS_GOING_TO_EXIT) {
if(backtoast!=null&&backtoast.getView().getWindowToken()!=null) {
finish();
} else {
backtoast = Toast.makeText(this, "Press back to exit", Toast.LENGTH_SHORT);
backtoast.show();
}
} else {
//other stuff...
super.onBackPressed();
}
}
This will call finish() if you press back while the toast is still visible, and only if the back press would result in exiting the application.
I use this much simpler approach...
public class XYZ extends Activity {
private long backPressedTime = 0; // used by onBackPressed()
#Override
public void onBackPressed() { // to prevent irritating accidental logouts
long t = System.currentTimeMillis();
if (t - backPressedTime > 2000) { // 2 secs
backPressedTime = t;
Toast.makeText(this, "Press back again to logout",
Toast.LENGTH_SHORT).show();
} else { // this guy is serious
// clean up
super.onBackPressed(); // bye
}
}
}
Both your way and #Steve's way are acceptable ways to prevent accidental exits.
If choosing to continue with your implementation, you will need to make sure to have backpress initialized to 0, and probably implement a Timer of some sort to reset it back to 0 on keypress, after a cooldown period. (~5 seconds seems right)
You may also need to reset counter in onPause to prevent cases when user presses home or navigates away by some other means after first back press. Otherwise, I don't see an issue.
If you want to exit your application from direct Second Activity without going to First Activity then try this code..`
In Second Activity put this code..
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
setResult(RESULT_OK, new Intent().putExtra("EXIT", true));
finish();
}
}).create().show();
}
And Your First Activity Put this code.....
public class FirstActivity extends AppCompatActivity {
Button next;
private final static int EXIT_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivityForResult(new Intent(FirstActivity.this, SecondActivity.class), EXIT_CODE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == EXIT_CODE) {
if (resultCode == RESULT_OK) {
if (data.getBooleanExtra("EXIT", true)) {
finish();
}
}
}
}
}
This is the best way, because if user not back more than two seconds then reset backpressed value.
declare one global variable.
private boolean backPressToExit = false;
Override onBackPressed Method.
#Override
public void onBackPressed() {
if (backPressToExit) {
super.onBackPressed();
return;
}
this.backPressToExit = true;
Snackbar.make(findViewById(R.id.yourview), getString(R.string.exit_msg), Snackbar.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
backPressToExit = false;
}
}, 2000);
}
additionally, you need to dissmis dialog before calling activity.super.onBackPressed(), otherwise you'll get "Activity has leaked.." error.
Example in my case with sweetalerdialog library:
#Override
public void onBackPressed() {
//super.onBackPressed();
SweetAlertDialog progressDialog = new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE);
progressDialog.setCancelable(false);
progressDialog.setTitleText("Are you sure you want to exit?");
progressDialog.setCancelText("No");
progressDialog.setConfirmText("Yes");
progressDialog.setCanceledOnTouchOutside(true);
progressDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
#Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
sweetAlertDialog.dismiss();
MainActivity.super.onBackPressed();
}
});
progressDialog.show();
}
use to .onBackPressed() to back Activity specify
#Override
public void onBackPressed(){
backpress = (backpress + 1);
Toast.makeText(getApplicationContext(), " Press Back again to Exit ", Toast.LENGTH_SHORT).show();
if (backpress>1) {
this.finish();
}
}
I just had this issue and solved it by adding the following method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// click on 'up' button in the action bar, handle it here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can also use onBackPressed by following ways using customized Toast:
enter image description here
customized_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="#drawable/ic_white_exit_small"
android:drawableLeft="#drawable/ic_white_exit_small"
android:drawablePadding="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="16sp"
android:text="Press BACK again to exit.."
android:background="#drawable/curve_edittext"/>
MainActivity.java
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
android.os.Process.killProcess(Process.myPid());
System.exit(1);
return;
}
this.doubleBackToExitPressedOnce = true;
Toast toast = new Toast(Dashboard.this);
View view = getLayoutInflater().inflate(R.layout.toast_view,null);
toast.setView(view);
toast.setDuration(Toast.LENGTH_SHORT);
int margin = getResources().getDimensionPixelSize(R.dimen.toast_vertical_margin);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, margin);
toast.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
Use this, it may help.
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Message")
.setMessage("Do you want to exit app?")
.setNegativeButton("NO", null)
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
UserLogin.super.onBackPressed();
}
}).create().show();
}
implementing onBackPressed() by System time, if pressed twice within 2 sec, then will exit
public class MainActivity extends AppCompatActivity {
private long backPressedTime; // for back button timing less than 2 sec
private Toast backToast; // to hold message of exit
#Override
public void onBackPressed() {
if (backPressedTime + 2000 > System.currentTimeMillis()) {
backToast.cancel(); // abruptly cancles the toast when pressed BACK Button *back2back*
super.onBackPressed();
} else {
backToast = Toast.makeText(getBaseContext(), "Press back again to exit",
Toast.LENGTH_SHORT);
backToast.show();
}
backPressedTime = System.currentTimeMillis();
}
}
I have some fragments that need to show a regular dialog. On these dialogs the user can choose a yes/no answer, and then the fragment should behave accordingly.
Now, the Fragment class doesn't have an onCreateDialog() method to override, so I guess I have to implement the dialogs outside, in the containing Activity. It's ok, but then the Activity needs to report back the chosen answer somehow to the fragment. I could of course use a callback pattern here, so the fragment registers itself at the Activity with a listener class, and the Activity would report back the answer thru that, or something like that.
But this seems to be quite a big mess for a simple task as displaying a "simple" yes-no dialog in a fragment. Also, this way my Fragment would be less self-contained.
Is there some cleaner way to do this?
Edit:
The answer to this question doesn't really explain in detail how one should use DialogFragments to display dialogs from Fragments. So AFAIK, the way to go is:
Display a Fragment.
When needed, instantiate a DialogFragment.
Set the original Fragment as the target of this DialogFragment, with .setTargetFragment().
Show the DialogFragment with .show() from the original Fragment.
When the user chooses some option on this DialogFragment, notify the original Fragment about this selection (e.g. the user clicked 'yes'), you can get the reference of the original Fragment with .getTarget().
Dismiss the DialogFragment.
I must cautiously doubt the previously accepted answer that using a DialogFragment is the best option. The intended (primary) purpose of the DialogFragment seems to be to display fragments that are dialogs themselves, not to display fragments that have dialogs to display.
I believe that using the fragment's activity to mediate between the dialog and the fragment is the preferable option.
You should use a DialogFragment instead.
Here is a full example of a yes/no DialogFragment:
The class:
public class SomeDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setMessage("Sure you wanna do this!")
.setNegativeButton(android.R.string.no, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// do nothing (will close dialog)
}
})
.setPositiveButton(android.R.string.yes, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// do something
}
})
.create();
}
}
To start dialog:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Create and show the dialog.
SomeDialog newFragment = new SomeDialog ();
newFragment.show(ft, "dialog");
You could also let the class implement onClickListener and use that instead of embedded listeners.
Callback to Activity
If you want to implement callback this is how it is done
In your activity:
YourActivity extends Activity implements OnFragmentClickListener
and
#Override
public void onFragmentClick(int action, Object object) {
switch(action) {
case SOME_ACTION:
//Do your action here
break;
}
}
The callback class:
public interface OnFragmentClickListener {
public void onFragmentClick(int action, Object object);
}
Then to perform a callback from a fragment you need to make sure the listener is attached like this:
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement listeners!");
}
}
And a callback is performed like this:
mListener.onFragmentClick(SOME_ACTION, null); // null or some important object as second parameter.
For me, it was the following-
MyFragment:
public class MyFragment extends Fragment implements MyDialog.Callback
{
ShowDialog activity_showDialog;
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
activity_showDialog = (ShowDialog)activity;
}
catch(ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "ShowDialog interface needs to be implemented by Activity.", e);
throw e;
}
}
#Override
public void onClick(View view)
{
...
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(this, 1); //request code
activity_showDialog.showDialog(dialog);
...
}
#Override
public void accept()
{
//accept
}
#Override
public void decline()
{
//decline
}
#Override
public void cancel()
{
//cancel
}
}
MyDialog:
public class MyDialog extends DialogFragment implements View.OnClickListener
{
private EditText mEditText;
private Button acceptButton;
private Button rejectButton;
private Button cancelButton;
public static interface Callback
{
public void accept();
public void decline();
public void cancel();
}
public MyDialog()
{
// Empty constructor required for DialogFragment
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.dialogfragment, container);
acceptButton = (Button) view.findViewById(R.id.dialogfragment_acceptbtn);
rejectButton = (Button) view.findViewById(R.id.dialogfragment_rejectbtn);
cancelButton = (Button) view.findViewById(R.id.dialogfragment_cancelbtn);
acceptButton.setOnClickListener(this);
rejectButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
getDialog().setTitle(R.string.dialog_title);
return view;
}
#Override
public void onClick(View v)
{
Callback callback = null;
try
{
callback = (Callback) getTargetFragment();
}
catch (ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "Callback of this class must be implemented by target fragment!", e);
throw e;
}
if (callback != null)
{
if (v == acceptButton)
{
callback.accept();
this.dismiss();
}
else if (v == rejectButton)
{
callback.decline();
this.dismiss();
}
else if (v == cancelButton)
{
callback.cancel();
this.dismiss();
}
}
}
}
Activity:
public class MyActivity extends ActionBarActivity implements ShowDialog
{
..
#Override
public void showDialog(DialogFragment dialogFragment)
{
FragmentManager fragmentManager = getSupportFragmentManager();
dialogFragment.show(fragmentManager, "dialog");
}
}
DialogFragment layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/dialogfragment_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="#string/example"/>
<Button
android:id="#+id/dialogfragment_acceptbtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/dialogfragment_textview"
android:text="#string/accept"
/>
<Button
android:id="#+id/dialogfragment_rejectbtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignLeft="#+id/dialogfragment_acceptbtn"
android:layout_below="#+id/dialogfragment_acceptbtn"
android:text="#string/decline" />
<Button
android:id="#+id/dialogfragment_cancelbtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_alignLeft="#+id/dialogfragment_rejectbtn"
android:layout_below="#+id/dialogfragment_rejectbtn"
android:text="#string/cancel" />
<Button
android:id="#+id/dialogfragment_heightfixhiddenbtn"
android:layout_width="200dp"
android:layout_height="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_alignLeft="#+id/dialogfragment_cancelbtn"
android:layout_below="#+id/dialogfragment_cancelbtn"
android:background="#android:color/transparent"
android:enabled="false"
android:text=" " />
</RelativeLayout>
As the name dialogfragment_heightfixhiddenbtn shows, I just couldn't figure out a way to fix that the bottom button's height was cut in half despite saying wrap_content, so I added a hidden button to be "cut" in half instead. Sorry for the hack.
I am a beginner myself and I honestly couldn't find a satisfactory answer that I could understand or implement.
So here's an external link that I really helped me achieved what I wanted. It's very straight forward and easy to follow as well.
http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application
THIS WHAT I TRIED TO ACHIEVE WITH THE CODE:
I have a MainActivity that hosts a Fragment. I wanted a dialog to appear on top of the layout to ask for user input and then process the input accordingly.
See a screenshot
Here's what the onCreateView of my fragment looks
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_activity, container, false);
Button addTransactionBtn = rootView.findViewById(R.id.addTransactionBtn);
addTransactionBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_trans);
dialog.setTitle("Add an Expense");
dialog.setCancelable(true);
dialog.show();
}
});
I hope it will help you
Let me know if there's any confusion. :)
public void showAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View alertDialogView = inflater.inflate(R.layout.test_dialog, null);
alertDialog.setView(alertDialogView);
TextView textDialog = (TextView) alertDialogView.findViewById(R.id.text_testDialogMsg);
textDialog.setText(questionMissing);
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
where .test_dialog is of xml custom
public static void OpenDialog (Activity activity, DialogFragment fragment){
final FragmentManager fm = ((FragmentActivity)activity).getSupportFragmentManager();
fragment.show(fm, "tag");
}