android - Implement preference item programmatically -


i'm trying implement preference custom layout programmatically.i qundo try change components of view not appear. here code of preference:

public class avatarpreference extends preference {  private string name; private int drawableavatardefault;  public avatarpreference(context context, attributeset attrs,         int defstyleattr, int defstyleres) {     super(context, attrs, defstyleattr, defstyleres); }  public avatarpreference(context context, attributeset attrs,         int defstyleattr) {     super(context, attrs, defstyleattr); }  public avatarpreference(context context) {     super(context); }  public avatarpreference(context context, attributeset attrs) {     super(context, attrs); }  @override protected void onbindview(view view) {     super.onbindview(view);     imageview icon = (imageview) view.findviewbyid(android.r.id.icon);     textview title = (textview) view.findviewbyid(android.r.id.title);     icon.setimageresource(drawableavatardefault);     title.settext(name); }  public void setname(string name) {     this.name = name; }  public void setdrawableavatardefault(int drawableavatardefault) {     this.drawableavatardefault = drawableavatardefault; } 

this code setting preference:

  avatarpreference pref = new avatarpreference(getactivity());   pref.setname(pkg.getnome());   pref.setdrawableavatardefault(pkg.getdrawabledefault());   pref.setlayoutresource(r.layout.pref_child); 

this custom layout xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_item_match" android:gravity="center_vertical" android:minheight="?android:attr/listpreferreditemheight" android:paddingleft="16dip" android:paddingright="?android:attr/scrollbarsize" >  <linearlayout     android:layout_width="wrap_content"     android:layout_height="match_parent"     android:gravity="center"     android:orientation="horizontal" >      <imageview         android:id="@+android:id/icon"         android:layout_width="@dimen/avatar_size"         android:layout_height="@dimen/avatar_size"         android:layout_marginright="10dp"         android:layout_gravity="center" /> </linearlayout>  <relativelayout     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_marginbottom="6dip"     android:layout_marginright="6dip"     android:layout_margintop="6dip"     android:layout_weight="1" >      <textview         android:id="@+android:id/title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:ellipsize="marquee"         android:fadingedge="horizontal"         android:textstyle="bold"         android:singleline="true"         android:textappearance="?android:attr/textappearancemedium" />      <textview         android:id="@+android:id/summary"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignleft="@android:id/title"         android:layout_below="@android:id/title"         android:maxlines="4"         android:textappearance="?android:attr/textappearancesmall"         android:textcolor="?android:attr/textcolorsecondary" /> </relativelayout>  <!-- preference should place actual preference widget here. -->  <linearlayout     android:id="@+android:id/widget_frame"     android:layout_width="wrap_content"     android:layout_height="match_parent"     android:gravity="center"     android:orientation="vertical" /> 

the layout composed of icon , text. when set in method not onbindview displayed. tried redefine method getview same result. hope can me. thanks

edit : settings.xml:

<preferencecategory android:title="@string/pref_cat_title_avatar"   android:key="avatars"> <!-- point preference added progammaticamente -->  </preferencecategory> 

this complete method of creation of preference:

private void installpreferenceavatarpackage() {     avatarmanager.installpackage(getactivity());     preferencecategory category = (preferencecategory)findpreference("avatars");     category.removeall();     list<packageavatar> packages = avatarmanager.getavatarpackage();     for(packageavatar pkg : packages) {         avatarpreference pref = new avatarpreference(getactivity());         pref.setname(pkg.getnome());         pref.setdrawableavatardefault(pkg.getdrawabledefault());         pref.setlayoutresource(r.layout.pref_child);          category.addpreference(pref);     } } 

this displayed:

enter image description here

in category avatar appears imageview , textview set part of method onbindview avatarpreference


Comments