Change textview after select an option in a RadioButtonGroup - android

I have toolbar where a have a menu. In that menu I have lots of options, one of them, when is pressed open an alertDialog with a RadioButtonGroup (with a setSingleChoiceItems) where the user can choose a property. After user choose the property and clicks on OK button of that AlertDialog I return to my Activity which was in background.
The problem is that after user choose the property in alertDialog and clicks in OK button (alertDialog disappears as normal) the textViews of my activity (that which is on the background) should change (according to the property chosen by user) and they are not changing.
All my activities (that can run in background while the user choose the property in alertdialog) extends an activity I have with the all events of Toolbar (including choose that property, share app, etc). I mean I have an activity for all things relate to toolbar menu. Every time user choose a different property I save it in database for then I could read it.
How can I do for when user choose a value of that property, the texviews of my background activity change? (I thought with the onResume it was easy to do, but it doesn't work.). Thanks
Please check if the screenshot helps you to understand my problem.In red what I want to change.

I don't think a dialog changes the activity stack, meaning onPause/onResume are not called.
As #sahu commented you can determine that your dialog closed in the positive/negative buttons:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do it?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
// change your textviews
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
// cancel pressed, probably don't need to do anything
}
});
builder.create().show();
}
EDIT:
If your dialog code is in a different class you will need to add an interface to communicate from the toolbar back to your activity. This is not much different than fragment to activity communication:
http://developer.android.com/training/basics/fragments/communicating.html
In your toolbar you will need to define an interface that your activity can call.
public class MyToolbar extends Toolbar {
OnInfoChangedListener mCallback;
// Don't forget to call this from your activity to setup the listener
public setOnInfoChangedListener(OnInfoChangedListener callback) {
mCallback = callback;
}
// Container Activity must implement this interface
public interface OnInfoChangedListener {
// you can add arguments here as needed
public void onInfoChanged();
}
...
}
Then in your setPositive button tell your activity that the info changed:
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
if (mCallback != null) mCallback.onInfoChanged();
// change your textviews
}
})
Then in your activity:
public static class MainActivity extends Activity
implements OnInfoChangedListener {
...
#Override
public void onCreate(bundle) {
super.onCreate(bundle);
Toolbar toolbar = // get the toolbar;
toolbar.setOnInfoChangedListener(this); // set myself as listener
}
public void onInfoChanged() {
// The user selected something from your toolbar
// update your views
}
}
You can define your interface to pass whatever you want from your toolbar into your activity, this is just a simple 'hey something changed notification'.

try
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkID)
case 0: textview.setText("your text");
break;
...
it may help !

Related

Dismiss DialogFragment onClick not working properly

I have an dialogfragment and its view consists of a simple seekbar and positive and negative buttons. When I dismiss dialogfragment onclick, a new dialogfragment is created and showed. After second click on button, dialog is dismissed, but my desire is dismissing after first click. Also I should note that my activity hosts two fragments and I call dialog.show() from first fragment.
MyListFragment.java
FragmentManager manager = getFragmentManager();
fndSrchFragment dialog = new fndSrchFragment();
dialog.setTargetFragment(MyListFragment.this, REQUEST_COLOR);
dialog.show(manager, SRCH_FND);
fndSrchFragment.java
public class fndSrchFragment extends DialogFragment {
.
.
//variable definition
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker, null);
rSb=(SeekBar) view.findViewById(R.id.r_seek_bar);
.
.
//code for interacting with seekbar
.
.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
int ccolor = android.graphics.Color.rgb(rComponent, gComponent, bComponent);
sendResult(Activity.RESULT_OK, ccolor);
// two click is needed for dismiss which I don't know why?
dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// two click is needed for dismiss which I don't know why?
dismiss();
}
});
return builder.create();
}
Thank You in Advance.
Set up a log command inside your onMenuItemActionExpand(). I bet you'll see this method is called right after you dismissed the dialog. Most likely, this method is being called by the system once focus is returned from the dialog to the activity. You should but your call to dialog.show() inside a something like onOptionsItemSelected(MenuItem item) instead.

How to prevent DialogFragment from being dismissed when activity gets resumed

I have an Activity which shows a DialogFragment when an event occurs in the Activity. When my application is running the user can press home button to make the app run in background. When app is running in background, as soon as the event occurs, the activity should show the DialogFragment, so that when user resumes the app the dialog should be on top of the activity, however I am not getting the results I want. As soon as I resume the app from launcher icon or notification, the dialog does not appear on the activity.
Here is my DialogFragment class:
public static class MyDialog extends DialogFragment {
public static DialogInterface.OnClickListener mListener;
public static String mTitle, mBody;
public static boolean mConfirm;
public static MyDialog getInstance(String body, boolean confirm,
DialogInterface.OnClickListener listener) {
mListener = listener;
mTitle = "MyDialog";
mBody = body;
mConfirm = confirm;
MyDialog fragment = new MyDialog();
return fragment;
}
#Override
public Dialog onCreateDialog(Bundle savedIntanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle(mTitle).setMessage(mBody)
.setPositiveButton("Ok", mListener);
if (mConfirm)
builder.setNegativeButton("Cancel", mListener);
return builder.create();
}
}
The dialog works fine when activity is in foreground.
Here is how I show the dialog:
MyDialog.getInstance("This is dialog body text", true,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
// positive button pressed
}else //negative button pressed
}
}).show(getSupportFragmentManager(), "dialog");
I want a dialog that remains on the activity even if the activity is in background, and as soon as I resume the activity, the dialog should be on top of the activity. How can I achieve this behavior?
You can declare a global boolean to determine if you should show the dialog or not. Then in your activity onResume() method you show the dialog depending on the boolean value.
If the user is finished interacting with the dialog you can set this boolean to false which will cause it not to show again when your activity is in the foreground (onResume).
Try using Fragment's or Activity's showDialog(int) methods and your dialog will be retained automatically.

How can I re use my alert DialogFragment for a result?

I recently created a standard list DialogFragment to build an AlertDialog in my Activity as can be seen as the answer here:
What is the best way to recreate an AlertDialog when the screen is rotated?
Now I would like to re-use this fragment for 3 different "Pop Up" selection lists in my activity. For each of the three buttons I need to identify the calling button to determine what action to take when the item from the list is selected.
What is the best way to achieve this?
Currently I am thinking that I need to pass the calling button ID to the DialogFragment and then pass it back to the activity with the result when the dialog completes. Is there a better way to achieve this goal?
I think probably the easiest way to achieve what you're going for is to just have three different listeners inside of your DialogFragment, and then have setters for each. Then when you build the alert dialog as a fragment, you can define what the onClick method for each listener will do in the calling method. So something like this:
protected DialogInterface.OnClickListener mListener1;
protected DialogInterface.OnClickListener mListener2;
protected DialogInterface.OnClickListener mListener3;
public void setListener1(final YourDialogFragment.OnClickListener passedListener) {
mListener1 = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listener.onClick(getActivity(), dialog, which);
}
};
}
Then inside of the code that calls the DialogFragment, call something like:
// Building the Dialog Fragment here
YourDialogFragment.setListener1(new YourDialogFragment.OnClickListener() {
#Override
public void onClick(FragmentActivity activity, DialogInterface dialog, int which) {
// Whatever you want to happen when you click goes here
}
});
Ideally you make some sort of helper to just take parameters so you're not explicitly calling the set methods from an activity, but that's the gist of it.
I would recommend you to show the dialog fragment from another fragment where you can implement the onClick listeners and use setTargetFragment() to tell the dialog fragment that who it is working with..
dialogFragment.setTargetFragment(this, 0);
and use getTargetFragment() to get the parent fragment from DialogFragment.
here is some code snippets from sample programs..
// Retrieve the progress bar from the target's view hierarchy.
mProgressBar = (ProgressBar)getTargetFragment().getView().findViewById(
R.id.progress_horizontal);
And also you can use setRetainInstance(true) in onCreate() method to tell the framework to try to keep this fragment around during a configuration changes
See this answer to get more idea, hope this helps..

switch statement: is layout visible versus is tab selected

So I am using a custom dialog fragment, when it pops up it asks a question and when the positive response "yes" is selected I want to load a specific fragment based on which ActionTab is selected. Obviously right now the code just loads 1 default fragment but I would be implementing a switch statement that checked either which Tab was selected or which fragment was currently visible. My question is there a preferred method for doing this? As in selecting .isVisible() for the fragment, or using the .getSelectedTab() method for the current ActionTab.
Here is my code, and thank you in advance for your time
current .java
public class StoreDialog_Fragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder storeD = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
storeD.setView(inflater.inflate(R.layout.fragment_store_dialog, null))
.setMessage(R.string.store_question)
.setPositiveButton(R.string.yes_q,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// switch statement here
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.replace(R.id.header_fragment_container,
new Assets_Fragment());
ft.commit();
}
})
.setNegativeButton(R.string.no_q,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// No it isn't!
}
});
// Create the Dialog object and return it
return storeD.create();
}
}
isVisible() is for when you are physically removing a view that you may not need anymore until later; tied to View.Visible, View.Invisible, View.Gone. So you would be better off checking against the selected tab for what you are doing.
Additionally, as per your request you can simply find the ActionBar from the Activity easy enough and make small modifications like the following as per your followup question:
actionBar_ = getActionBar();
actionBar_.setSubtitle("subtitle");
actionBar_.setTitle("title");
actionBar_.setDisplayHomeAsUpEnabled(true);//If navigation from home needed
actionBar_.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#BB000000"))); //Allow for some transparency in the ActionBar.

Reset Android DialogFragment

I am using a custom DialogFragment to let a user change his login credentials. There are some text fields and two buttons (save/cancel). The layout is set in DialogFragment's onCreateView method.
If I open the dialog text fields are filled with default values. When the user changes text in a text field and clicks the cancel button the dialog is dismissed. Next time the dialog opens the text field changed before does not contain the default value as i expected but the text the user changed before. The text fields are not reset. This is almost the same problem mentioned here Reset an Android Dialog. The problem is that the solution provided refers to a Dialog which is deprecated in API level 11 and i cannot use onPrepareDialog with a DialogFragment.
Is there a similar way to reset the content of a DialogFragment?
You can override onResume() in your class, which extends DialogFragmet, as follows:
private static class MyDialogFragment extends DialogFragment {
public static MyDialogFragment newInstance() {
// ...
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// ...
}
#Override
public void onResume() {
super.onResume();
Dialog dialog = getDialog();
// reset code goes here - use dialog as you would have in onPrepareDialog()
}
}
You can also use .setText() method in Your activity as reaction after negative button click. Eg:
In DialogFragment.java, onCreateDialog(...)define AlertDialog.Builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
then
//this is better than creating button in layout
builder.setNegativeButton(R.string.button_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainAct) getActivity()).cancelDialog(DialogFragment.this);
}
}
);
In MainActivity.java create method cancelDialog(DialogFragment df) {
//here use df to reset text fields
}

Categories

Resources