LearningBasics
LearningBasics

Reputation: 680

Android: How to apply style to all the children of ViewGroup

I have a style which I have applied to view group but I want to apply that style to all the children of viewgroup. How can it be done ?

Upvotes: 2

Views: 3314

Answers (3)

austinw
austinw

Reputation: 734

I know I'm late to the party but hopefully, this helps future viewers! I will detail how to create and apply a theme to a parent View and its children. For example, how to change the background and text colors of a CardView and its children:

1. Create a set of colors in colors.xml (this is optional, as you can instead simply include the color hexes in the colors.xml items' values)

<resources>
    <!-- You can name the colors whatever you want, however, I like to follow 
Material naming conventions -->
    <color name="colorPrimary">#00796b</color>
    <color name="colorPrimaryLight">#48a999</color>
</resources>

2. Create a theme in styles.xml

<!-- You can name the style whatever you want, and extend whatever parent theme you want -->
<style name="CardView.Light.Details" parent="Theme.MaterialComponents.DayNight">

    <!-- These colors are referencing the colors defined above in colors.xml -->
    <item name="android:backgroundTint">@color/colorPrimaryLight</item>
    <item name="android:textColor">@color/colorOnPrimary</item>
</style>

3. Insert the style name as the value for your View's android:theme attribute

<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    app:cardCornerRadius="5dp"
    android:theme="@style/CardView.Light.Details"> <!-- Notice the theme here! -->

    ....

    <!-- No need to apply any theme or color attributes on child views
         (unless you would like to override the parent theme) -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stock_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="14sp" />

    ....
</com.google.material.card.MaterialCardView>

Upvotes: 3

Roberto Orozco
Roberto Orozco

Reputation: 599

You can apply a style to all the view children elements of another view if you apply the style as theme in xml.

android:theme="@style/AppTheme.Inverted"

Upvotes: 2

There is no way to do it other than this:
You can iterate by using next ViewIterator over LinearLayout and check by instanceof for EditText and then change any property you need.

    public class ViewIterator implements java.util.Iterator<View>
{
    private java.util.Stack<View> mNodes;

    public ViewIterator(View view)
    {
        mNodes = new java.util.Stack<View>();
        if (view != null)
        {
            mNodes.push(view);
        }
    }

    public boolean hasNext()
    {
        return !mNodes.empty();
    }

    public View next()
    {
        View view = null;

        // if remaining nodes
        if (hasNext())
        {
            // return the last element
            view = mNodes.pop();
            assert (view != null);
            // if it is a container, add its children to the stack
            if (view instanceof ViewGroup || view instanceof AdapterView)
            {
                ViewGroup viewGroup = (ViewGroup)view;
                for (int i = 0; i < viewGroup.getChildCount(); i++)
                {
                    assert (viewGroup.getChildAt(i) != null);
                    mNodes.push(viewGroup.getChildAt(i));
                }
            }
        }

        // return result
        return view;
    }

    public void remove()
    {
        // not supported
    }
}

Taken from here.

Upvotes: 2

Related Questions