Home » Android » android – Coloring the Space widget

android – Coloring the Space widget

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to color the Space widget. I tried using both the android:foreground and android:background attributes, but still it is displaying a transparent View.

 <android.support.v4.widget.Space
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:foreground="@android:color/black"
    android:background="@android:color/black" />
How to&Answers:

As per the official docs definition:

Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.

They probably didn’t think of “coloring an empty space”.
Where “empty” means “without color”, “invisible”.

That’s what I’d do:

<View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@android:color/black"
/>

Using a bare View as a divider is one of my 2/3 favourite tricks with bare Views.
I also use them as spacers and as “center point” in RelativeLayouts.

Answer:

I tried Paul’s “view trick” but had GridLayout horizontal / vertical constraints problem (and during execution drawing stops as soon as this view is encountered).
For me (Studio 2.3.3, api 21) this worked :

<TextView
        android:layout_width="4dp"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:background="@color/housetoolsBlue" />