Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override the include's background attribute to change background color

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.

like image 217
ant2009 Avatar asked Oct 22 '15 09:10

ant2009


People also ask

How to change background color of list in HTML?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.

How do I change my background on Kotlin?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.


2 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);
like image 125
Mohammad Hossein Gerami Avatar answered Oct 16 '22 21:10

Mohammad Hossein Gerami


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.

like image 36
KishuDroid Avatar answered Oct 16 '22 21:10

KishuDroid