When creating a custom DialogFragment, i set the title using the following:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog_add, container, false);
// Setting title here
getDialog().setTitle("Add New");
return v;
}
The above code works fine for me on API level older than 23. For API 23 the title is not showing at all.
Any idea why? and how to make the title show on API 23?
Solved by adding the following to the styles.xml:
<item name="android:dialogTheme">#style/CustomDialog</item>
<style name="CustomDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
In your styles:
<style name="CustomDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
If your dialog is a fragment:
MyFragment myFragment = new MyFragment();
myFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
I solved using .setMessage instead of .setTitle. I know it's not the same appearance but in my case, I just needed a hint for the user.
Related
I try to write an application using one activity-multiple fragment approach. Some pages should has translucent toolbar but not others.
new MyFragment(isTranslucentToolbar)
But theme property belongs to application.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="#style/AppTheme">
If I set this property as below, I cannot change theme of the fragment.
<style name="AppTheme" parent="#style/Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDisablePreview">true</item>
</style>
<style name="AppTheme.Transparent" parent="AppTheme">
<item name="windowActionBarOverlay">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
If I set the theme as transparent, how do I create a fragment with non-translucent toolbar? I can arrange the margin&padding values of fragment root layout and statusbar bar color programmatically.
Is this any way of this task?
Keep the normal theme with the ActionBar enabled.
When you want to hide/show the ActionBar:
activity.getSupportActionBar().hide(); //for AppCompatActivity
activity.getActionBar().hide(); //for Activity
activity.getSupportActionBar.show(); //for AppCompatActivity
activity.getActionBar().show(); //for Activity
When you want to change the translucency of the status bar:
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
In your fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getContext().getTheme().applyStyle(R.style.AppTheme_Transparent, true);
View view = inflater.inflate(R.layout.Your_fragment_layout, container, false);
return view;
}
try setTheme() method in your java file
EDIT 1:
Setting Theme in manifest is usually used for Activity.
If you want to set Theme for Fragment, add next code in the onCreateView() of the Fragment:
#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.yourCustomTheme);
// 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.yourLayout, container, false);
}
Also give a try to this code segment if above doesn't work
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
I am trying to customize the background color of a DialogFragment I created. By default, when I set the window title text, I get white text on a black background. How can I change this to my own colors?
Looking at similar questions I find that some people recommend creating a custom style in styles.xml. I have tried this but haven't succeeded so far.
Here are the relevant parts of my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.event_planner_fragment, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.drawerList);
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), null));
//getDialog().setTitle("Fubar");
//getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
return view;
}
In styles.xml I have tried:
<style name="MyDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#color/md_orange_900</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>
Have a look at this. It shows you step-by-step exactly what you want to do:
I have a DialogFragment which I want to show in fullscreen. I do however still want a StatusBar present, and the hardware buttons at the bottom. I also want to set a background color of the StatusBar (for Lollipop).
My problem is that if I set the following flags in the DialogFragment:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Both the StatusBar and Hardware keyboard becomes translucent, and the DialogFragment stretches behind these.
Here is the code, which has been greatly reduced to become readable:
public class CardDetailsDialog extends DialogFragment {
Setup parameters...
public static CardDetailsDialog newInstance(final long cardId, final long projectId){
CardDetailsDialog frag = new CardDetailsDialog();
frag.setStyle(DialogFragment.STYLE_NORMAL, R.style.CardDetailsDialogStyle);
return frag;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getDialog() != null) {
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnimation;
getDialog().getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
getDialog().getWindow().setStatusBarColor(Color.RED);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.card_details, container, false);
Handle everything that happens inside the view...
return view;
}
}
Here is the referred theme:
<style name="CardDetailsDialogStyle" parent="#style/Theme.AppCompat.Light.Dialog" >
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
And the style of the fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/pp.whiteBackgroundColor" >
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_details_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/PopupMenutheme">
</android.support.v7.widget.Toolbar>
<ScrollView
android:id="#+id/details_scrollview"
android:layout_height="wrap_content"
android:layout_width="match_parent">
All subview elements here...
</ScrollView>
</RelativeLayout>
This is the result:
As you can see, the ToolBar extends over the StatusBar and hardware buttons. I don't know if I am approaching this correctly. Am I missing something?
EDIT
This is what the same view look likes when I remove
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
For anyone who's still having this problem, do the following. This just solves half of the problem that is posted i.e. black status bar.
Add following theme to res/value-v21/style
<style name="DialogTheme" parent="#style/Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowTranslucentStatus">true</item>
</style>
And then apply Style on DialogFragment in onCreate
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}
Edit
if you've problem with your dialog theme then use this style e.g. colorAccent or colorControlHighlight etc
<style name="DialogTheme" parent="#style/ThemeOverlay.AppCompat.Dialog">
<item name="android:windowTranslucentStatus">true</item>
</style>
In my case SYSTEM_UI_FLAG_LAYOUT_STABLE solved problem with overlapping
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width,height);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
dialog.getWindow().setStatusBarColor(getResources().getColor(R.color.darkGrayTransp));
dialog.getWindow().getDecorView().setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_STABLE);//solves issue with statusbar
dialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL| Gravity.TOP);
}
Try use the same Style from your App. I tested with simple dialog without fragment and works fine.
Like that:
new Dialog(context, R.style.CardDetailsDialogStyle);
You have to set fitsystemwindows = true. Other way is to add a Space with 0dp and change its height to 25dp when the dialog is going to show.
To change the space size, use layout params, check this post: How to create a RelativeLayout programmatically with two buttons one on top of the other?
<style name="DialogTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">false</item>
</style>
Just apply android:fitsSystemWindows="true" to your root ViewGroup:
`<FrameLayout
android:fitsSystemWindows="true">
<View .../>
I am using a PreferenceFragment (without a ListView), but am unable to set the background color, and it seems to be transparent.
How can I set the background color to white with PreferenceFragment?
EDIT:
first screenshot:
Overriding onCreateView not work -
This question was also answered here
Adding the following code to your PreferenceFragment will let you add a background color, image, etc.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(android.R.color.your_color));
return view;
}
Also you can do something like changing the view's background in the onViewCreated() method.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(getResources().getColor(R.color.YOUR_COLOR));
}
I faced with the same requirement (Androidx Preference Screen background for settings fragment).
The below code has worked for me. (in themes.xml)
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
...............
<!-- Add below -->
<item name="preferenceTheme">#style/preference</item>
</style>
<style name="preference" parent="Theme.AppCompat">
<item name="android:background">#color/purple_200</item>
</style>
</resources>
I need to create a dialog over a fragment (that takes up the whole screen). The dialog needs to be a floating dialog that will be positioned over the fragment with the fragment darkened out outside of the fragment..
For the custom Dialog, i have a linearLayout that has curved edges, no matter what i do, the dialog has a black bordering on all sides (very small). I've tried everything to make it transparent and go away (so that all of the dialog is just the linear layout - curved box)
For the DialogFragment, this is what I have for onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
LinearLayout layout =(LinearLayout)inflater.inflate(R.layout.custom_dialog, null);
LinearLayout item = (LinearLayout)layout.findViewById(R.id.display_item);
populateItemData(item, inflater);
return layout;
}
custom_dialog is just a LinearLayout that has android:backgroung set to #000000
This is my style for the custom Dialog
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:alwaysDrawnWithCache">false</item>
<item name="android:windowContentOverlay">#null</item>
</style>
I tried all kinds of combinations in this style (from what I've seen online) and I can't get rid of that annoying black bordering, I can paint it white or any other color if i set that LinearLayout background to anything other than #000000...
I've spent literally 3-4 hours on this, i hope someone else can help out...
Try
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
in your DialogFragment's onCreateView
Try this (How to I create a 100% custom DialogFragment)
this work for dialog
Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// layout to display
dialog.setContentView(R.layout.add_edit);
// set color transpartent
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
Set your theme like this worked for me
<style name="MyDialog" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
And in your dialog fragment set like this
public class Progress extends DialogFragment {
int style = DialogFragment.STYLE_NO_TITLE;
int theme = R.style.MyDialog;
public Progress() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(style, theme);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.progress, container, false);
}
}
You can achieve by adding this in
Dialog Fragment or BottomSheetDialogFragment
In onCreateDialog Method
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
return dialog;
}
In onActivityCreated
getDialog().getWindow().getAttributes().alpha = 0.9f; // An alpha value to apply to this entire window. An alpha of 1.0 means fully opaque and 0.0 means fully transparent
for DialogFragment transparent
For completely transparent use:
setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
For custom background -create a style file in you values folder (values/style.xml) and use it:
setStyle(DialogFragment.STYLE_NO_FRAME, yourpackagename.R.style.YOURE_CUSTOM_STYLE);
in your style file override the atribute:
android:windowBackground to be #color/DialogBackgroundBlackSemiTransparent
<style name="BaseDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlNormal">#color/colorAccent</item>
<item name="colorControlActivated">#color/colorAccent</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:colorBackground">#android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowActionModeOverlay">false</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:backgroundDimAmount">.00</item>//this line is changed alpha from 1.0 to 0.0(full transparent)
</style>
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, R.style.BaseDialogTheme);
}
Those who are using AlertDialog builder in onCreateDialog instead of onCreateView can assign theme like following code. Complete set of themes can be found from R.style. Don't forget that some of them supported recently and are not available on old device phones.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Translucent);
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_album, null);
builder.setView(view);
return builder.create();
}
Try this if you like to:
public TransparentDialog()
{
super();
setStyle(STYLE_NO_FRAME, R.style.AppTheme);
}
Per accepted answer, in Kotlin
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var v = super.onCreateView(inflater, container, savedInstanceState)
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
return v
}