0

i'm created simple widget extends from ImageView, in this simle code i want to set image into ImageView from layout xml,i want change background imageview image and change image to set other image into ImageView, but my problem in first time dont show image

1) i'm create new attribute into attr.xml:

<declare-styleable name="CIconButton">
    <attr name="button_icon"     format="integer" />
    <attr name="background_icon" format="reference" />
</declare-styleable>

2) use CIconButton class into layout:

<!-- xmlns:app="http://schemas.android.com/apk/res/com.sample.app.tpro" -->
<com.sample.widget.CIconButton
    android:id="@+id/imgv_update_selected_phones"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="10dp"
    app:button_icon="@drawable/icon_add_action" />

3) CIconButton class

public class CIconButton extends ImageView implements OnClickListener {
    private Drawable mSelectedBackground;

    public CIconButton(Context context) {
        super(context, null);
    }
    public CIconButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CIconButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CIconButton, defStyle, 0);
        mSelectedBackground = a.getDrawable(R.styleable.CIconButton_button_icon);
        setImageDrawable(mSelectedBackground);
        a.recycle();
        setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    }
}

PROBLEM:

my custom widgets class dont show image set into layout xml, for example this class dont show icon_add_action from drawable

DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • If you're extending `ImageView`, why not just use the `android:src` attribute, and let the `super` constructor take care of loading the image? – Mike M. Feb 24 '15 at 09:50
  • @Mike M. for get variable attr in `CIconButton class` – DolDurma Feb 24 '15 at 09:52
  • Sorry, I don't understand what you mean. – Mike M. Feb 24 '15 at 09:54
  • @Mahdi.Pishguy .you should use android:src for displaying your icon as you are nyways extending Imageview – williamj949 Feb 24 '15 at 09:55
  • @MikeM. i want set background imageview and change image to set other image into ImageView – DolDurma Feb 24 '15 at 09:56
  • I still don't follow, but what you're trying to do is nothing different than would happen if you just used the `src` attribute. Whether your code sets the image, or the superclass sets the image, the result is the same. You might as well just let the `ImageView` class handle it. – Mike M. Feb 24 '15 at 09:59
  • check this link http://stackoverflow.com/questions/4062559/android-appwidget-with-custom-view-not-working – Ramesh Feb 24 '15 at 10:04
  • 1
    @Ramesh That post has nothing to do with this question. – Mike M. Feb 24 '15 at 10:06

1 Answers1

0

My problem resolved , i must be change second constructor as :

public CIconButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

to:

public CIconButton(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

this call last this class constrctor

DolDurma
  • 15,753
  • 51
  • 198
  • 377