Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreateDrawableState never call

I have tye to add new state to RelativeLayout but onCreateDrawableState method is never call.

My class is:

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 onCreateDrawableState method never call?

like image 921
Nik Avatar asked Apr 19 '12 07:04

Nik


1 Answers

Try to create separate layout with 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);
    }

}
like image 181
Guzik Avatar answered Oct 14 '22 19:10

Guzik