This should be a simple task, but for some reason I can find a way to set the title of a DialogFragment. (I am setting the dialog contents using onCreateView overload).
The default style leaves a place for the title, but I can't find any method on the DialogFragment class to set it.
The title is somehow magically set when the onCreateDialog method is used to set the contents, so I wonder if this is by design, or there is a special trick to set it when using the onCreateView overload.
You can use getDialog().setTitle("My Dialog Title")
Just like this:
public static class MyDialogFragment extends DialogFragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set title for this dialog
getDialog().setTitle("My Dialog Title");
View v = inflater.inflate(R.layout.mydialog, container, false);
// ...
return v;
}
// ...
}
Does overriding onCreateDialog and setting the title directly on the Dialog work? Like this:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("My Title");
return dialog;
}
Jason's answer used to work for me, but now it needs the following additions to get the title to show.
Firstly, in your MyDialogFragment's onCreate() method, add:
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
Then, in your styles.xml file, add:
<style name="MyDialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
After hours of trying different things, this is the only one that has done the trick for me.
NB - You may need to change the Theme.AppCompat.Light.Dialog.Alert to something else in order to match the style of your theme.
DialogFragment could be represented as dialog and as Activity. Use code below that would work properly for both
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getShowsDialog()) {
getDialog().setTitle(marketName);
} else {
getActivity().setTitle(marketName);
}
}
You can take a look at the official docs.
The way i did is like this:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("My Title");
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.my_layout, null);
builder.setView(view);
return builder.create();
}
Similar to Ban Geoengineering's answer, but with a few modifications, so instead of coding what specific theme to use in the DialogFragment, I override the default style used by DialogFragments in my styles.xml.
set the title in the androidx.fragment.app.DialogFragment.
class EditBatteryLevelFragment:DialogFragment(),SelfClosingFragment.Host
{
override fun onCreateView(
inflater:LayoutInflater,container:ViewGroup?,savedInstanceState:Bundle?
):View
{
// set dialog title
requireDialog().setTitle(R.string.edit_battery_level__title)
// .....
return someView
}
}
in your app theme in styles.xml, override android:dialogTheme, which is the default style used by DialogFragment instances.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents">
<!-- BONUS READING: override material colors here, too https://material.io/develop/android/theming/color -->
<!-- override DialogFragment theme of those spawned by activities with this theme -->
<item name="android:dialogTheme">#style/AppTheme.Dialog</item>
</style>
<!-- ... -->
also in styles.xml, declare the dialog theme that will be used by DialogFragment instances. it's important for this style to inherit from ThemeOverlay so that it will preserve your app's theme colors.
<!-- ... -->
<!-- define the style for your dialog -->
<style name="AppTheme.Dialog" parent="ThemeOverlay.MaterialComponents.Dialog">
<!-- add a minimun width to the dialog, so it's not too narrow -->
<item name="android:windowMinWidthMajor">#dimen/abc_dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#dimen/abc_dialog_min_width_minor</item>
<!-- display the title for dialogs -->
<item name="android:windowNoTitle">false</item>
</style>
</resources>
make sure that the activity that is spawning the DialogFragment is using the defined AppTheme.
If you are using view binding:
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
binding = YourDialogXmlBinding.inflate(getLayoutInflater());
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle("Your title here")
.setView(binding.getRoot());
return builder.create();
}
Related
I cannot hide the title's view from an Activity that I'm styling as a Dialog.
android:windowNoTitle is set to false in the reference style, and RequestWindowFeature(WindowFeatures.NoTitle) is called from OnCreate.
Is there another setting for completely removing the title's view from an Activity?
styles.xml
<style name="Theme.AppCompat.Light.Dialog_Configuration" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
ConfigurationView
[Activity(
Label = "Configuration",
MainLauncher = false,
Theme = "#style/Theme.AppCompat.Light.Dialog_Configuration",
ExcludeFromRecents = true
)]
public class ConfigurationView : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RequestWindowFeature(WindowFeatures.NoTitle);
SetContentView(Resource.Layout.Configuration);
}
}
Are you using toolbar? You can try this
getSupportActionBar().setDisplayShowTitleEnabled(false);
or
requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title.
getSupportActionBar().hide(); //hide the title bar.
While you have removed the title, you also need to remove the title bar as well. Try adding the following line to your onCreate method:
getSupportActionBar().hide();
Write this in the OnCreate method just after SetContentView:
ActionBar.Hide();
Alternatively, you could try this
NavigationPage.SetHasNavigationBar(this, false);
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.
I am trying to use activity as a dialog, and I have done the following, but still it shows as an activity rather than dialog. I wonder what I am missing or doing wrong?
AndroidManifest.xml
<activity
android:name="AboutView"
android:theme="#style/Dialog">
</activity>
Themes.xml
<style name="Dialog" parent="#android:style/Theme.Dialog">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
AboutView.cs
[MvxFragment(typeof(MainViewModel), Resource.Id.MainViewContainer)]
[Activity(Label = "AboutView", Theme = "#style/Dialog")]
[Register("views.AboutView")]
public class AboutView : MvxFragment<AboutViewModel>
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.AboutView, null);
return view;
}
}
You're only specifying that the Activity's theme should inherit from Dialog's theme. The AboutView class still inherits from MVXFragment, which isn't a Dialog.
I'm not too familiar with Xamarin's class structure, but you'll need to ensure that the AboutView class inherits from Xamarin's version of a Dialog.
In Android studio it would be the DialogFragment class. You could start looking there.
I think MvxDialogFragment is exactly what you're looking for. Here you have an example how to use it
I have multi-pane view with left and right fragment. On right fragment am launching a PreferenceFragment. Problem is the fragment looks completely distorted without any style. Is there a way to apply theme just to the PreferenceFragment alone ?
I tried this but it did not work
My code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppTheme_PreferenceTheme);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View view = super.onCreateView(localInflater, container, savedInstanceState);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_settings_preference_layout);
}
I think the solution did not work because I have already inflated preference-layout in onCreate. Is there a way to inflate preference-layout without using the method addPreferencesFromResource and just using LayoutInflater service?
I use this to set styles to PreferenceFragments:
<style name="PreferenceTheme" parent="#style/AppTheme">
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:background">#color/cpWhite</item>
...
</style>
And then, in the onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
...
container.getContext().setTheme(R.style.PreferenceTheme);
}
Does the trick for me. I hope it helps you too.
In 2022, if you use PreferenceFragmentCompat to create preference screen, you can find in it's source code an attempt to get theme from R.attr.preferenceTheme. So, if you set this value in your app theme to a desired theme, that desired them will be applied. Something like this:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="preferenceTheme">#style/SettingsTheme</item>
...
</style>
<style name="SettingsTheme" parent="#style/PreferenceThemeOverlay">
<item name="preferenceCategoryTitleTextColor">?attr/colorPrimary</item>
</style>
Worked perfectly for me
What I want to do:
I want each Fragment of my MainActivity to use a different theme, so that the ActionBar has different background-colors, depending on the visible Fragment.
The Situation:
I created a MainActivity which uses the Tabs + Swipe Navigation. I Added 7 Tabs (=7 Fragments). I created one Theme which should be applied only to the first Fragment (fragment_main_1).
Here the Theme:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Blue" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/Blue.ActionBarStyle</item>
</style>
<style name="Blue.ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">#style/Blue.ActionBar.TitleTextStyle</item>
<item name="android:background">#33B5E5</item>
</style>
<style name="Blue.ActionBar.TitleTextStyle" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
After creating 6 more Themes it should be possible to swipe through the Tabs while the ActionBar changes its background-color automatically.
What didn't work:
Adding those lines (which I found here on stackoverflow) to the Fragment1.java:
// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.Blue);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.fragment_main_1,container, false);
I hope you can help me:) Thank you.
Many fragments (such as PreferenceFragment) read the styled attributes directly from the Context returned by Fragment.getContext() method, so you might need to override that too:
private var themedContext: Context? = null
override fun onAttach(context: Context) {
super.onAttach(context).also {
themedContext = ContextThemeWrapper(context, R.style.ThemeForThisFragment)
// if you want to apply a theme overlay:
// themedContext.theme.applyStyle(R.style.MyThemeOverlay, true)
}
}
override fun onDetach() {
super.onDetach()
themedContext = null
}
override fun getContext(): Context? {
return themedContext ?: super.getContext()
}
Try LayoutInflater localInflater = inflater.from(contextThemeWrapper); instead.