OnCreateDrawableState never calls

I have tye to add a new state to a RelativeLayout, but the onCreateDrawableState method never calls.

My class:

public class UnreadableRelativeLayout extends RelativeLayout { private static final int[] STATE_UNREADED = {R.attr.state_unreaded}; private boolean isUnreaded = false; public UnreadableRelativeLayout(Context context) { super(context); } public UnreadableRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public UnreadableRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setUnreaded(boolean isUnreaded) { if (this.isUnreaded != isUnreaded) { this.isUnreaded = isUnreaded; refreshDrawableState(); } } @Override protected int[] onCreateDrawableState(int extraSpace) { final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); if (isUnreaded) mergeDrawableStates(drawableState, STATE_UNREADED); return drawableState; } 

}

Why is the onCreateDrawableState method never called?

+6
source share
2 answers

Try creating a separate layout using android:duplicateParentState="true" and inflate it in your class. It works for me on Android 4.0.4:

unreadable_relative_layout.xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:duplicateParentState="true" > ... </RelativeLayout> 

UnreadableRelativeLayout.java:

 public class UnreadableRelativeLayout extends RelativeLayout { public UnreadableRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater factory = LayoutInflater.from(context); factory.inflate(R.layout.unreadable_relative_layout, this); } } 
+1
source

I had the same problem and I finally found what the problem was ... This is related to the Android OS, I used 2.2, and when I switched to 2.3.X, it worked ...

0
source

Source: https://habr.com/ru/post/913606/


All Articles