How to use Android DialogFragment setStyle() - android

I'm trying to setStyle to my custom dialog that extends dialogFragment,
constructor looks like:
MyCustomDialog() {
super();
setStyle(STYLE_NO_FRAME, 0);
}
and I still see the frame around my layout.
Does anyone have any ideas?

Try calling in onCreate(...) instead and not in the constructor.

According to the setStyle() description in the DialogFragment documentation:
Calling this after the fragment's Dialog is created will have no
effect

In my opinion, it is better to override theme getter
override fun getTheme(): Int {
return R.style.BottomSheetDialogTheme
}

try adding this to the onCreate method in DialogFragment:
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Material_Dialog_MinWidth);

If you want to set a custom theme for your a dialog you can do it this way...
AlertDialog.Builder(context, R.style.MyDialogTheme) ...

Related

onAttachFragment not being called when showing a dialog from fragment

I'm showing a BottomSheetDialogFragment from a Fragment. The idea is to get the callback of BottomSheetDialogFragment inside that Fragment instead of activity so I was hoping to receive the fragment inside
override fun onAttachFragment(childFragment: Fragment?) {
super.onAttachFragment(childFragment)
callback = childFragment as? Callback
}
This method is not being called. I tried using fragmentManager and childFragmentManager when showing the dialog to see if I can get onAttachFragment called but no luck.
AccountBottomSheetDialog dialog = AccountBottomSheetDialog.Companion.newFragment();
dialog.show(getChildFragmentManager(), AccountBottomSheetDialog.Companion.getTAG());
AccountBottomSheetDialog dialog = AccountBottomSheetDialog.Companion.newFragment();
dialog.show(getFragmentManager(), AccountBottomSheetDialog.Companion.getTAG());
Does anyone know how to get this called?
Thanks.
I think the reason it's not calling onAttachFragment is because DialogFragment control its life cycle and other method overrides differently than the normal ones, see documentation.
If you simply want to have a callback to the parent fragment, you can either override the onAttach method inside the DialogFragment and use the context parameter as the callback (cast it), or have a public method inside your DialogFragment that sets the callback, which you would call after creating that fragment.
onAttach method:
override fun onAttach(context: Context?) {
super.onAttach(context)
callback = context as? Callback
}
public callback setter method:
//parent fragment: after initializing it
childFragment.setCallback(this#ParentFragment)//or pass in other callbacks
//child fragment:
fun setCallback(callback: Callback) {
this.callback = callback
}
Maybe it a little bit late but now in 2019 with support library androidx.appcompat:appcompat:1.1.0 if using variant with child fragment manager then onAttachFragment called as I wait.
Here is correct fragment of code on Kotlin
ChildInfoDialog.getInstance(it.data).show(childFragmentManager, ChildInfoDialog.TAG)
Cannot answer your question directly but here is the workaround you could take:
Pass your parent fragment as newFragment's parameter
Set it on your child fragment as target fragment with any request code (setTargetFragment)
Use it in your code as getTargetFragment() and cast it to any interface you like (the interface your parent fragment implements of course).
PS.: for the above to work, fragment manager must be the same for parent and child.

Fragment theme not appliying

I would like to change dynamically the theme of my Fragment.
I have tried these solutions:
1st try, set the getTheme method on the activity. It only changes the colors of the activity:
#Override
public Resources.Theme getTheme() {
Resources.Theme theme = super.getTheme();
theme.applyStyle(R.style.AppTheme_RED, true);
return theme;
}
2nd try not changing anything:
final Context contextThemeWrapper = new
ContextThemeWrapper(getActivity(), R.style.AppTheme_RED);
LayoutInflater localInflater = inflater.from(contextThemeWrapper);
final View view = localInflater.inflate(R.layout.fragment_workout_list, container, false);
3rd try work only for activity, not fragment:
setTheme(R.style.AppTheme_RED);
Any help is welcomed.
Thanks in advance.
Edit: I have found that floatingbutton have the good color when i apply theme to fragment.
as google says we must inflate our theme before super.oncreate()
inside your activity or fragment and in oncreate() or on oncreateview() simply add the code below:
setTheme(R.style.AppTheme_RED);

Can not set theme/style in FragmentDialog

class BottomBarFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val contextThemeWrapper = ContextThemeWrapper(getActivity(), R.style.Theme_BaseDarkTheme)
val localInflater = inflater.cloneInContext(contextThemeWrapper)
binding = FragmentBottomBarBinding.inflate(localInflater, container, false)
// tried setStyle also
setStyle(0, R.style.Theme_BaseDarkTheme)
}
}
style
<style name="Theme.BaseDarkTheme" parent="Theme.AppCompat">
<item name="dividerColor">#color/divider_dark</item>
</style>
in fragment dialog layout
<LinearLayout
style="#style/llParent"
android:background="?attr/dividerColor"
>
According to this, my layout should be dark, but it is light always. What am I doing wrong?
The documentation for setStyle() says this:
Call to customize the basic appearance and behavior of the fragment's
dialog. This can be used for some common dialog behaviors, taking care
of selecting flags, theme, and other options for you. The same effect
can be achieve by manually setting Dialog and Window attributes
yourself. Calling this after the fragment's Dialog is created will
have no effect.
Fragment's Dialog will be created after onCreate() and before onCreateView().
Try calling setStyle() from the onCreate() method
Basicly, if you are trying to use something, what have "dialog" in name, you should build dialog. To do this, there is one method like onCreateDialog().
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),your_style);
...
return builder.create();
}
Otherwise, you should extend just a Fragment, and there you can call in onCreateView() something like this:
Context contextThemeWrapper = new ContextThemeWrapper(getActivity(),your_style);
LayoutInflater newInflater = inflater.cloneInContext(contextThemeWrapper);
View view = newInflater.inflate(your_layout,container,false);
Sorry for java code, but there should be similar code.

Android - Dialog getSupportFragmentManager undefined

I'm trying to implement a simple Dialog into my code. But it does not work. I have searched every available tutorial, including the official developer guide but nothing works. The error I got from logcat is that I'm getting a nullPointerException, I'm guessing that's on the getActivity. Any help?
This is what I have: This is my Custom Dialog class.
public class SaveDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Save Password");
builder.setView(getContentView());
Dialog dialog = builder.create();
dialog.show();
return dialog;
}
private View getContentView() {
LayoutInflater inflater = getActivity().getLayoutInflater();
return inflater.inflate(R.layout.dialog, null);
}
}
and this is my main activity where the onclick occurs
private void savePassword() {
SaveDialog savePasswordDialog = new SaveDialog();
savePasswordDialog.show(savePasswordDialog.getSupportFragmentManager(), "tag");
}
Every single time I fire up the onClick, the app crashes. On top of that, currently I am trying to use getSupportFragmentManager, but it says it's undefined.
You should use getSupportFragmentManager(), which is only available in FragmentActivity.
You should change your activity to a fragment one.
Check this answer
Try this..works!!
((AppCompatActivity)activity).getSupportFragmentManager()
Just call getFragmentManager() from your android.support.v4.app.DialogFragment or android.support.v4.app.Fragment. It will return a android.support.v4.app.FragmentManager (this is, the support FragmentManager)
You don't have to manually show the dialog in onCreateDialog(), just returning it is sufficient for DialogFragment to work its magic (and show the dialog) when you call savePassword().
So remove this line from onCreateDialog :
dialog.show();
and it should work. Good luck!

Clickable Hyperlinks in DialogFragment

This is my static inner class for creating an AlertDialog inside my MainActivity class:
public static class AboutDialogFragment extends DialogFragment {
public static AboutDialogFragment newInstance() {
AboutDialogFragment frag = new AboutDialogFragment();
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_dialog_about)
.setTitle(R.string.about)
.setMessage(R.string.about_message)
..........
.create();
}
}
And I'm showing it when you press a menu item which is inside MainActivity:
case R.id.about:
DialogFragment aboutFragment = AboutDialogFragment.newInstance();
aboutFragment.show(getSupportFragmentManager(), "about_dialog");
// Make links clickable
((TextView) aboutFragment.getDialog().findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
return true;
I'm trying to make the links in the message text clickable using the commented line.
I found this method here and it has worked for me when using a regular Dialog (no fragments). However, this is the first time I have tried using it on a DialogFragment and I always get a NullPointerException when trying to find the view.
I've also tried aboutFragment.getView().findViewById(android.R.id.message) but that returns null as well.
Maybe I am calling the code too early/in the wrong place?
Any ideas would be great!
EDIT: Just tried ((TextView) v.getRootView().findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
and
((TextView) v.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance()); in onCreateView() and also tried in onCreateDialog() without success.
Still getting null pointer exception...
Hopefully you've already figured this out but I just did this same thing and wanted to document it somewhere. Put this in your DialogFragment class:
#Override
public void onStart() {
super.onStart();
((TextView) getDialog().findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());
}
Maybe I am calling the code too early/in the wrong place?
That's my suspicion. Is there any reason you can't do your "make links clickable" inside your onCreateDialog() method?
As the time progressed, adding some updated inputs to the query.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupView(view)
}
private fun setupView(view: View) {
toolbar.setNavigationOnClickListener({ v -> dismiss() })
toolbar.title = "New Message"
view.tvTitle.text = arguments?.getString("title")
view.tvBody.autoLinkMask = Linkify.ALL
view.tvBody.text = arguments?.getString("body")
view.tvBody.movementMethod = LinkMovementMethod.getInstance()
}
Most important 2 steps in the above code (which can be used in any dialog textviews) are:
To Linkify
setMovementMethod
After this all links will work fine.

Categories

Resources