Override the background enable attribute to change the background color

Android Studio 1.5 

I have this layout called chat_profile_header , which will be used in many layouts. I set background to indigo color. The problem is that when I include this title in other layouts, I want to be able to change the background color based on this layout theme. Everything in the title 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 use the header included in this layout. However, based on this topic, I want to change the title to a different background color, gray. 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 a way to do this without creating different background layouts for the headers. There seems to be a lot of duplication.

+7
android android-layout
source share
4 answers

this is not possible . You can set the 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); 
+11
source share

You can always change the background color with the layout theme you specify. Just install

 <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 colorPrimary under your theme style files. Warning: by setting the primary color, you also change the color of the action bar.

 <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 in the theme style. Just in case, you do not know how to set up a theme for your activity, you can always do it in your AndroidManifest.xml

 <activity android:name=".Activity" android:theme="@style/MyCustomTheme" /> 
+2
source share

You cannot set the background color in the include > tag.

As you set the background color of the include tag, it will be confused with the background color of the layout.

So, from my point of view, you should make your inclusion layout transparent, and then try setting the background color.

In this case, it will not mix with the color of the include layout.

Hope this helps you get an idea about this.

+2
source share
 <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 an identifier to your Android RelativeLayout : identifier = "@ + identifier / rl _profile_header" then initialize it where you want to use for example RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl_profile_header) then setColor as you need using rl.setBackgroundColor(R.color.red)

+2
source share

All Articles