Reputation: 10520
All,
I've got a custom RelativeLayout
and after the view loads, I'd like to change the background of the RelativeLayout
to a drawable. I can't figure out why the heck it's not working...seems like it should be so simple.
Here is my code for my custom layout:
public class InputBoxView extends RelativeLayout {
//My variables
//Irrelevants initalizers
//Method in question
public void addErrorBox() {
setBackgroundResource(R.drawable.error_border);
}
}
Here is the layout file for the custom view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/standard_line_height"
android:background="@android:color/white" >
<View
style="@style/space_view"
android:layout_alignParentTop="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<TextView
android:id="@+id/input_box_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".35"
android:padding="10dp"
android:text="Key" />
<EditText
android:id="@+id/input_box_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:layout_marginRight="10dp" />
</LinearLayout>
<View
style="@style/space_view"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Here is my error_border
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="2dp" android:color="@color/red"/>
</shape>
When I call the addErrorBox
method from my activity, my background doesn't visibly change, but if I put a breakpoint on the instance of the InputBoxView
, the mBackground
property changes after I call the addErrorBox
, so the background I believe is changing, but not updating.
I tried calling invalidate()
on the instance of the InputBoxView
, but it still didn't work.
Anyone have an idea?
Upvotes: 3
Views: 3012
Reputation: 5122
From what I tested with the code you gave, the issue is the
android:background="@android:color/white"
on the relative layout for the custom view xml. It seems to neglect whatever you set with setBackgroundResource
for InputBoxView
.
Once I removed that attribute, I was able to see the red border background.
One solution to this is to remove the RelativeLayout
from the xml since your InputBoxView
already extends from it and set the white background, width height and what not when you inflate your custom view.
So your xml can look something like this
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Your views in here without the RelativeLayout -->
</merge>
and in your InputBoxView
public InputBoxView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.some_custom, this, true);
// Set what you need for the relative layout.
setBackgroundColor(Color.WHITE);
}
public void addErrorBox() {
setBackgroundResource(R.drawable.error_border);
invalidate();
}
Or you can add an ID to the RelativeLayout and modify the background directly instead of on the InputBoxView
.
And for invalidate, you'll need it after onCreate so that it is redrawn.
Hope that helps resolve your issue or at least point you in the right direction.
Upvotes: 3