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);
Related
I have a tab based application which has only one activity. I would like to apply the android material transition effects when moving from one fragment to other. I have followed this samples. Here is what I am doing,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_contact, container, false);
ButterKnife.bind(this,view);
setupWindowAnimations();
return view;
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setupWindowAnimations() {
Fade fade = new Fade();
fade.setDuration(1000);
getActivity().getWindow().setEnterTransition(fade);
Slide slide = new Slide();
slide.setDuration(1000);
getActivity().getWindow().setReturnTransition(slide);
}
But this has no effect when I enter and exit the fragment which has this code added.
In my styles(V21).xml I have this added
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowContentTransitions">true</item>
</style>
How to make this work?
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.
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 .../>
In Android, is it possible to properly have different background color for the views, the empty space and the action bar?
For example I have a PreferenceScreen in a PreferenceFragment. I have these codes:
Style XML:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:Theme.Holo">
<item name="android:textColor">#color/blue1</item>
<item name="android:background">#color/bg</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:titleTextStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar.Solid">
<item name="android:background">#color/brown1</item>
<item name="android:windowBackground">#color/brown1</item>
</style>
</resources>
my PreferenceFragment class: this already looks like a hack!
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=super.onCreateView(inflater, container, savedInstanceState);
((ListView) v.findViewById(android.R.id.list)).setBackgroundColor(getResources().getColor(R.color.blue1));
return v;
}
This already makes the ListView items have bg background color, and the empty space after them blue1 background color (note that I had to set them in the opposite way to make this work!), and the ActionBar has brown1 background but the text on it does not and that's really ugly! Also, what the heck does windowBackground do anyway?
EDIT: I've been able to change the background of the title with this hack:
int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
TextView title = (TextView) findViewById(actionBarTitleId);
if (title != null)
title.setBackgroundColor(getResources().getColor(R.color.brown1));
}
Now how do I do this for the logo and the "Up" arrow to the left of the logo?
The solution is to remove the titleTextStyle from the style XML.
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>