java - Image view setBackground is not acting properly -
i have class makes me imagebutton. image button contains image view, tow text view , button.
i put width in image view, , if put xml 1 image background, works properly, if java code don't works, acts src atribute.
(the image being shown if has no width, src, , not backgorund).
this java code:
public class miboton extends relativelayout{ protected int idimagen; protected string nomproducte; protected double preuproducte; protected string spreuproducte; //protected mainactivity maprincipal; public miboton(context context, int imgimageresource, string sname, double iprice){ super(context); this.idimagen = imgimageresource; this.nomproducte = sname; this.preuproducte = iprice; this.spreuproducte = double.tostring(iprice) + " €"; layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); inflater.inflate(r.layout.products, this, true); imageview imageproduct = (imageview)findviewbyid(r.id.imatgeproducte); textview productname = (textview)findviewbyid(r.id.nomproducte); productname.settextsize(10); textview productprice = (textview)findviewbyid(r.id.preuproducte); drawable new_image= getresources().getdrawable(imgimageresource); imageproduct.setbackground(new_image); // imageproduct.setbackgroundresource(imgimageresource); productname.settext(sname); productprice.settext(spreuproducte); button btnpedir = (button)findviewbyid(r.id.btnpedir); } public int getidimagen() { return idimagen; } public string getnomproducte() { return nomproducte; } public double getpreuproducte() { return preuproducte; } }
this xml code:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mi_boton_layout" > <imageview android:id="@+id/imatgeproducte" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_margintop="20dp" android:clickable="true" /> <textview android:id="@+id/nomproducte" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imatgeproducte" android:textsize="14sp" /> <textview android:id="@+id/preuproducte" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/nomproducte" android:textsize="14sp" /> <button style="@style/btnstylegenoa" android:layout_width="wrap_content" android:layout_height="32dp" android:text="demanar" android:textsize="10dp" android:id="@+id/btnpedir" android:layout_below="@+id/preuproducte" /> </relativelayout>
use method:
imageproduct.setimagedrawable(new_image);
Comments
Post a Comment