Reputation: 544
I'm facing with big problem with StateListDrawable on a custom view. I've a custom view that inherits directly from LinearLayout, and in its XML layout, the background is a simple state list drawable that sets the view's background enabled or disabled in order of its current state. What i don't understand is why if i call
this.SetEnabled(false);
in my custom view, background does not change.
Maybe my question is not very clear, so i've done a simple example. You can download it here. Look at the "ViewCustom.java" file and find FIXME tag.
Hope someone can help me.
P.S. The same state list drawable associated to a Button works, but on my custom view don't.
ViewCustom.java
public class ViewCustom extends LinearLayout {
public ViewCustom(Context context, AttributeSet attrs) {
super(context, attrs);
//Inflate layout
final LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_custom, this);
/**FIXME: Try to set "enabled" to false in order to get the
* "panel_disabled" background, but it doesn't work.
*/
this.setEnabled(false);
}
}
View.custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sld_panel"
android:orientation="vertical" >
</LinearLayout>
sld_panel.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/panel_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/panel_enabled" android:state_enabled="true"/>
</selector>
Upvotes: 1
Views: 1541
Reputation: 39698
The problem is, you can't disable just a Linear Layout. What you would need to do is disable everything in it. This question answers that, but it boils down to one of two things.
setVisibility(View.GONE)
.You can iterate through each item in the View, by something like this:
for ( int i = 0; i < myLayout.getCount(); i++ ){
View view = getChildAt(i);
view.setEnabled(false); // Or whatever you want to do with the view.
}
Upvotes: 1