android - button object returning null -
<com.bakar.core.materialdesign.views.buttonfloat android:id="@+id/buttton_float" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_marginright="24dp" android:background="@color/app_primary_color" materialdesign:animate="true" materialdesign:icondrawable="@drawable/ic_action_new" />
this declaration of button in layout
this custom class of button.
package com.bakar.core.materialdesign.views; import android.animation.objectanimator; import android.annotation.targetapi; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmap.config; import android.graphics.canvas; import android.graphics.paint; import android.graphics.porterduff.mode; import android.graphics.porterduffxfermode; import android.graphics.rect; import android.graphics.drawable.drawable; import android.os.build; import android.util.attributeset; import android.view.animation.bounceinterpolator; import android.widget.imageview; import android.widget.imageview.scaletype; import android.widget.relativelayout; import android.widget.textview; import com.bakar.r; import com.bakar.utils.utils; import com.nineoldandroids.view.viewhelper; public class buttonfloat extends button { int sizeicon = 24; int sizeradius = 28; imageview icon; // icon of float button drawable drawableicon; public boolean isshow = false; float showposition; float hideposition; public buttonfloat(context context, attributeset attrs) { super(context, attrs); setbackgroundresource(r.drawable.background_button_float); sizeradius = 28; setdefaultproperties(); icon = new imageview(context); icon.setadjustviewbounds(true); icon.setscaletype(scaletype.center_crop); if (drawableicon != null) { icon.setimagedrawable(drawableicon); } relativelayout.layoutparams params = new relativelayout.layoutparams(utils.dptopx(sizeicon, getresources()), utils.dptopx(sizeicon, getresources())); params.addrule(relativelayout.center_in_parent, relativelayout.true); icon.setlayoutparams(params); addview(icon); } protected void setdefaultproperties() { ripplespeed = utils.dptopx(2, getresources()); ripplesize = utils.dptopx(5, getresources()); setminimumwidth(utils.dptopx(sizeradius * 2, getresources())); setminimumheight(utils.dptopx(sizeradius * 2, getresources())); super.background = r.drawable.background_button_float; // super.setdefaultproperties(); } // set atributtes of xml view protected void setattributes(attributeset attrs) { //set background color // color resource int bacgroundcolor = attrs.getattributeresourcevalue(androidxml, "background", -1); if (bacgroundcolor != -1) { setbackgroundcolor(getresources().getcolor(bacgroundcolor)); } else { // color hexadecimal background = attrs.getattributeintvalue(androidxml, "background", -1); if (background != -1) setbackgroundcolor(background); } // set ripple color // color resource int ripplecolor = attrs.getattributeresourcevalue(materialdesignxml, "ripplecolor", -1); if (ripplecolor != -1) { setripplecolor(getresources().getcolor(ripplecolor)); } else { // color hexadecimal int background = attrs.getattributeintvalue(materialdesignxml, "ripplecolor", -1); if (background != -1) setripplecolor(background); else setripplecolor(makepresscolor()); } // icon of button int iconresource = attrs.getattributeresourcevalue(materialdesignxml, "icondrawable", -1); if (iconresource != -1) drawableicon = getresources().getdrawable(iconresource); final boolean animate = attrs.getattributebooleanvalue(materialdesignxml, "animate", false); post(new runnable() { @override public void run() { showposition = viewhelper.gety(buttonfloat.this) - utils.dptopx(24, getresources()); hideposition = viewhelper.gety(buttonfloat.this) + getheight() * 3; if (animate) { viewhelper.sety(buttonfloat.this, hideposition); show(); } } }); } integer height; integer width; @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (x != -1) { rect src = new rect(0, 0, getwidth(), getheight()); rect dst = new rect(utils.dptopx(1, getresources()), utils.dptopx(2, getresources()), getwidth() - utils.dptopx(1, getresources()), getheight() - utils.dptopx(2, getresources())); canvas.drawbitmap(cropcircle(makecircle()), src, dst, null); invalidate(); } } public imageview geticon() { return icon; } public void seticon(imageview icon) { this.icon = icon; } public drawable getdrawableicon() { return drawableicon; } @targetapi(build.version_codes.jelly_bean) public void setdrawableicon(drawable drawableicon) { this.drawableicon = drawableicon; try { icon.setbackground(drawableicon); } catch (nosuchmethoderror e) { icon.setbackgrounddrawable(drawableicon); } } public bitmap cropcircle(bitmap bitmap) { bitmap output = bitmap.createbitmap(bitmap.getwidth(), bitmap.getheight(), config.argb_8888); canvas canvas = new canvas(output); final int color = 0xff424242; final paint paint = new paint(); final rect rect = new rect(0, 0, bitmap.getwidth(), bitmap.getheight()); paint.setantialias(true); canvas.drawargb(0, 0, 0, 0); paint.setcolor(color); canvas.drawcircle(bitmap.getwidth() / 2, bitmap.getheight() / 2, bitmap.getwidth() / 2, paint); paint.setxfermode(new porterduffxfermode(mode.src_in)); canvas.drawbitmap(bitmap, rect, rect, paint); return output; } @override public textview gettextview() { return null; } public void setripplecolor(int ripplecolor) { this.ripplecolor = ripplecolor; } public void show() { objectanimator animator = objectanimator.offloat(buttonfloat.this, "y", showposition); animator.setinterpolator(new bounceinterpolator()); animator.setduration(1500); animator.start(); isshow = true; } public void hide() { objectanimator animator = objectanimator.offloat(buttonfloat.this, "y", hideposition); animator.setinterpolator(new bounceinterpolator()); animator.setduration(1500); animator.start(); isshow = false; } public boolean isshow() { return isshow; } }
this how using button in fragment class
private buttonfloat mbutton; mbutton=(buttonfloat)getactivity().findviewbyid(r.id.button_float); mbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.e("button","click"); } });
this giving me null pointer exception.
ids different. in xml
use @+id/buttton_float
in java code use findviewbyid(r.id.button_float)
.
Comments
Post a Comment