Home » Android » android – Override the include's background attribute to change background color

android – Override the include's background attribute to change background color

Posted by: admin June 15, 2020 Leave a comment

Questions:
Android Studio 1.5

I have this layout called chat_profile_header that will be used in many layouts. I have set the background to a indigo color. The problem is when I include this header in other layouts, I want to be able to change the background color based on that layout’s theme. Everything in the header will remain the same.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/profile_header_indigo_500">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/photorace"
        android:layout_centerVertical="true"/>

    <TextView
        android:id="@+id/tvProfileName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/profile_image"
        android:layout_toEndOf="@id/profile_image"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:fontFamily="sans-serif"
        android:textColor="@android:color/white"/>
</RelativeLayout>

Here I am using the above header included in this layout. However, based on this theme I want to change the header to another background color grey. However, I don’t think I can override the background color attribute.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include
        layout="@layout/chat_profile_header"
        android:background="@color/child_header_lighter_grey"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvParentList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

Is there way to do this without having to create different header layouts based on background. Seems a lot of duplication.

How to&Answers:

this does not seem to be possible. You can set background from java.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include
        android:id="@+id/layout1"
        layout="@layout/chat_profile_header"/>

    <include
        android:id="@+id/layout2"
        layout="@layout/chat_profile_header"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvParentList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

this java code

Linealayout layout1 = (Linealayout)findViewbyId(R.id.layout1);
Linealayout layout2 = (Linealayout)findViewbyId(R.id.layout2);

layout1.setBackgroundColor(R.color.color_1);
layout2.setBackgroundColor(R.color.color_2);

Answer:

You can always change your background color with layout theme you specified.
Just set the

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorPrimary">
</LinearLayout>

Then set the colorPrimary under your theme style files. Warning: by setting the color primary, you are changing the color of the action bar as well.

<style name="MyCustomTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@android:color/blue</item>
</style>

Using this method, your background will always be the same as the color you specified under the theme style.
Just in case, you doesn’t know how to set the theme for you activity, you can always do in your AndroidManifest.xml

<activity
        android:name=".Activity"
        android:theme="@style/MyCustomTheme"
/>

Answer:

You can not set background color to <include> tag.

As you set the background color of include tag then it will be messed up with the include layout’s background color.

So from my point of view you should make your include layout
transparent and then try to set background color of it.

In this case it won’t get mixed up with the include layout’s color.

Hope it will help you to get some idea regarding it.

Answer:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/rl_profile_header"
android:layout_height="60dp"
android:background="@color/profile_header_indigo_500">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/profile_image"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/photorace"
    android:layout_centerVertical="true"/>

<TextView
    android:id="@+id/tvProfileName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/profile_image"
    android:layout_toEndOf="@id/profile_image"
    android:layout_centerVertical="true"
    android:layout_marginLeft="8dp"
    android:fontFamily="sans-serif"
    android:textColor="@android:color/white"/>

assign id to your RelativeLayout
android:id=”@+id/rl_profile_header”
then Initialize it where you want to use
like RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl_profile_header)
then setColor as you need using rl.setBackgroundColor(R.color.red)

Answer:

It seems not possible.
From https://developer.android.com/training/improving-layouts/reusing-layouts
“You can also override all the layout parameters (any android:layout_* attributes) of the included layout’s root view by specifying them in the tag. “