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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With