Custom android button with image -


i have problem toggle button. trying extend buttonrectangle library navasmdc , want add image. imagebutton ripple effect.

i have tried set background drawable, doesn't have effect want.

i tried own toggle button, on skills ripple effect.

so asking.

  1. is possible extend layout , add imageview?
  2. if so, how done?

here code, have far:

public class styleabletogglebutton extends buttonrectangle implements styleableview, checkable {    private boolean checked = false; private int primarycolor = config.default_primary_color; private int secondarycolor = config.default_secondary_color; private float ripplespeed = 18f;   public styleabletogglebutton(context context, attributeset attrs) {     super(context, attrs);     applystyle();     setonclicklistener(new onclicklistener() {         @override         public void onclick(view view) {             toggle();             applystyle();         }     });     setripplespeed(ripplespeed);     if (build.version.sdk_int >= build.version_codes.jelly_bean) {         setbackgrounddrawable(getresources().getdrawable(r.drawable.direction_in_w));     } }  @override public void setchecked(boolean b) {     checked = b;     applystyle(); }  @override public boolean ischecked() {     return checked; }  @override public void toggle() {     checked = !checked;     applystyle(); }  @override public void applystyle() {      if (checked){         this.setbackgroundcolor(primarycolor);     }else {         this.setbackgroundcolor(secondarycolor);     } }  @override protected int makepresscolor(){     return !checked ? primarycolor : secondarycolor; } } 

i found out answer later. still not sure question 1., can extend programmaticaly.

answer 2. question addview(view v)

so code looks this:

public class styleabletogglebutton extends buttonrectangle implements    styleableview, checkable {    private boolean checked = false; private int primarycolor = config.default_primary_color; private int secondarycolor = config.default_secondary_color; private float ripplespeed = 18f; private textview textview; private imageview imageview; private linearlayout linearlayout;  private int minheight; private float fontsize = 16f;   public styleabletogglebutton(context context, attributeset attrs) {     super(context, attrs);      minheight = (getresources().getdisplaymetrics().heightpixels - attutils.dptopx(getresources().getdimension(r.dimen.height_att_activity_bar),getresources())) / 4;      setupbutton();       setonclicklistener(new onclicklistener() {         @override         public void onclick(view view) {             toggle();             applystyle();         }     });     setripplespeed(ripplespeed);   }   //this add textview , imageview existing layout. kept previous empty. private void setupbutton() {      setminimumheight(minheight);      linearlayout = new linearlayout(getcontext());     linearlayout.setorientation(linearlayout.vertical);       linearlayout.layoutparams layoutparams;     layoutparams = new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content);     linearlayout.setlayoutparams(layoutparams);      layoutparams = new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content);     layoutparams.gravity = gravity.center_horizontal;      textview = new textview(getcontext());     textview.setlayoutparams(layoutparams);     textview.settypeface(null, typeface.bold);     if (build.version.sdk_int >= build.version_codes.jelly_bean_mr1) {         textview.settextalignment(text_alignment_center);     }     textview.settextsize(fontsize);      layoutparams = new linearlayout.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content);     layoutparams.gravity = gravity.center_horizontal;      imageview = new imageview(getcontext());     imageview.setlayoutparams(layoutparams);      linearlayout.addview(imageview);     linearlayout.addview(textview);     addview(linearlayout); }  @override public void setchecked(boolean b) {     checked = b;     applystyle(); }  @override public boolean ischecked() {     return checked; }  @override public void toggle() {     checked = !checked;     applystyle(); }  @override public void applystyle() {      drawable dr = imageview.getdrawable();     if (checked){         this.setbackgroundcolor(primarycolor);         textview.settextcolor(secondarycolor);         if (dr != null) {             dr.setcolorfilter(secondarycolor, porterduff.mode.multiply);             imageview.setimagedrawable(dr);         }     }else {         this.setbackgroundcolor(secondarycolor);         textview.settextcolor(primarycolor);         if (dr != null) {             dr.setcolorfilter(primarycolor, porterduff.mode.multiply);             imageview.setimagedrawable(dr);         }     }  }  @override protected int makepresscolor(){     return !checked ? primarycolor : secondarycolor; }   @override public void settext(string text){     textview.settext(text); }  public void setimage(drawable drawable){     imageview.setimagedrawable(drawable); }  public void setlayoutmargin(int marginwidth, int marginheight){     relativelayout.layoutparams layoutparams = new relativelayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content);     layoutparams.setmargins(marginwidth, marginheight, marginwidth, marginheight);     linearlayout.setlayoutparams(layoutparams); }  public void settextviewmargin(int marginwidth, int marginheight){     linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content);     layoutparams.setmargins(marginwidth, marginheight, marginwidth, marginheight);     textview.setlayoutparams(layoutparams); }  public void setimageviewmargin(int marginwidth, int marginheight){     linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content);     layoutparams.setmargins(marginwidth, marginheight, marginwidth, marginheight);     imageview.setlayoutparams(layoutparams); } 

}


Comments