I have a fragment named HostFragment
which nests one to four other fragments.
This is the layout of HostFragment
:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hostFragmentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:id="@+id/fragmentContainer1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
<RelativeLayout
android:id="@+id/fragmentContainer2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:id="@+id/fragmentContainer3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
<RelativeLayout
android:id="@+id/fragmentContainer4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
The important part of this is android:layout_marginTop="12dp"
.
Background: The nested fragments cover the entirety of HostFragment
except for this margin. When the nested fragments change their background color (by calling Canvas#drawColor
), HostFragment
needs to also change the color of this margin to match. I store the needed color in SharedPreferences
.
Behavior: If the user goes from HostFragment
to SettingsActivity
, changes the color, and comes back to HostFragment
, the nested fragments will change their color immediately (through their onResume()
methods), but HostFragment
's margin will still be the old color. If the user then leaves HostFragment
and goes to another fragment, then returns to HostFragment
, the margin will update its color. I don't know how or why - I have no code in HostFragment
to update the color. The code in HostFragment
only deals in swapping in and out nested fragments.
Problem: I need the margin color to update right away, so in onResume()
, I've tried something like mTableLayout.setBackgroundColor(...)
or even mView.setBackgroundColor(...)
(mView
is the layout I inflate in onCreateView()
). This still doesn't work, and the color will only update if the user leaves and comes back.
Question: How can I change the color of the margin to match an int
value in SharedPreferences
once the user returns to HostFragment
from another Activity
(i.e. Right after the user returns from the Settings)?
Thank you in advance!
Margin is completely transparent, and it does not have any background color.
you can't color a margin, but the element exposed on either side is the body – you can set the background-color of that (it's in the style tags in the head of your html doc).
Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent). Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object.
3. android:layout_marginTop: It specifies the extra space on the top side of the view.
Try giving paddingTop
instead of marginTop
and then change the color of the view in onResume
by mView.setBackgroundColor(...)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With