Android - Change background color of margin
Asked Answered
F

5

7

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!

Flabbergast answered 17/9, 2015 at 1:45 Comment(3)
Try giving paddingTop instead of marginTop and then change the color of the view in onResume by mView.setBackgroundColor(...)Impercipient
@AbhishekV Add your comment as an answer.Flabbergast
@Flabbergast Done. Added it as an answer.Impercipient
I
14

Try giving paddingTop instead of marginTop and then change the color of the view in onResume by mView.setBackgroundColor(...).

  • Margin is the space outside the View so background color of a view won't reflect in margin space.
  • Padding is the space inside of a view and background color given to a View will be applied to padding space as well.
Impercipient answered 24/9, 2015 at 5:13 Comment(0)
C
2

try this,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/c1_cnxlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black" >

    <RelativeLayout 
        android:id="@+id/c2_cnxlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@android:color/darker_gray" />

</RelativeLayout>
Contemplate answered 24/9, 2015 at 5:21 Comment(0)
K
1

Setting the color of a margin is not possible. To achieve something that looks like that though, there's two things to do.

1) Use padding instead of margin. Margin is outside of an element, while padding is inside of the element. This means that the element will grow in size, and that the color you've given your element as background-color will also apply to the area around the content.

2) Use a border, or drawable. This way of doing it requires more work, but is very highly configurable. Creating a border is done by simply setting a drawable as background, and giving it a stroke width and color. For more information (and an example implementation) see https://mcmap.net/q/118265/-android-how-to-draw-a-border-to-a-linearlayout.

For more information on margin, padding, border etc. see http://www.w3schools.com/css/css_boxmodel.asp. This website explains it for CSS, but the concept is the same pretty much anywhere.

Knowing answered 22/9, 2015 at 10:4 Comment(0)
P
0

In order to change the color .setBackgroundColor(...) should work in onResume(), but you should be aware that as it has been pointed out above, a margin area is the space that is left outside from your view within the reference of its parent view. That is why changing a view´s background color would not have effect on the margin. What you could do is add a FrameLayoutthat wraps your TableLayout so that your TableLayout has a reference to set the margin from. In that case, you should be able to change FrameLayout's background and it should affect the desired margin area.

In the image below, the red rectangle represents your TableLayout as you can see on the left, it is the root view of your HostFragmentand the margin area is outside of your reach.On the right, the root view of your HostFragment is a FrameLayout and the red rectangle is still your TableLayout. In the later case, you can change the color of the FrameLayout.

image : http://oi59.tinypic.com/jz8q46.jpg

Polash answered 22/9, 2015 at 16:57 Comment(0)
F
0

best way is to use background resource specifying multiple shapes with different color and use margin or padding..

Forrestforrester answered 19/10, 2016 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.