Friday, October 16, 2009

[android-developers] Custom View donot Draw in ListView

Hello,

I have a query, When I try to use a customview in the listview, the
custom view donot display. All other views are displayed except the
custom view.
When I try to display the view like a normal layout (not in a list),
it displays fine.

Can anyone please help me to draw the customview in the list?

the main.xml file - layout that contains the custom view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<CheckBox android:id="@+id/check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="checkbox_2" />
<app.customise.tab.CustomBackground
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

the list.xml file - the list view file
<LinearLayout >
<FrameLayout>
<ListView android:id="@android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

</FrameLayout>
</LinearLayout>

the CustomBackground.java - class that extends the View class and
draws a rounded rectangle

public class CustomBackground extends View{

public CustomBackground(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mRect = new Rect(40, 0, 310, 90);

mDrawable = new GradientDrawable
(GradientDrawable.Orientation.TOP_BOTTOM,
new int[] { 0xFFA5C3E9,
0xFF548DD4 });
mDrawable.setShape(GradientDrawable.RECTANGLE);
mDrawable.setGradientRadius((float)(Math.sqrt(2) * 60));
}

private Rect mRect;
private GradientDrawable mDrawable;
public CustomBackground(Context context) {
super(context);
// TODO Auto-generated constructor stub

mRect = new Rect(40, 0, 310, 90);

mDrawable = new GradientDrawable
(GradientDrawable.Orientation.TOP_BOTTOM,
new int[] { 0xFFA5C3E9,
0xFF548DD4 });
mDrawable.setShape(GradientDrawable.RECTANGLE);
mDrawable.setGradientRadius((float)(Math.sqrt(2) * 60));
}

void setCornerRadii(GradientDrawable drawable, float r0,
float r1, float r2, float r3) {
drawable.setCornerRadii(new float[] { r0, r0, r1, r1, r2, r2, r3,
r3 });
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//super.onDraw(canvas);

System.out.println("ON DRAW CALLED");

mDrawable.setBounds(mRect);

float r = 10;
canvas.translate(10, 10);
mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
setCornerRadii(mDrawable, r, r, r, r);
mDrawable.draw(canvas);

invalidate();
}


the DummyActivity.class extends ListActivity

public class DummyActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Display layout of main.xml with custom view.
* Works fine. Displays one checkbox, two radio buttons and
one rounded rectangle
* Class extends Activity
*/
// setContentView(R.layout.main);
/**
* The above display in list.
* Displays one checkbox, two radio buttons. Rounded rectangle
not displayed
*/
setContentView(R.layout.list);
mAdapter ma = new mAdapter(this);
for(int i=0;i<5;i++)
{
ma.add(i);
}
this.setListAdapter(ma);

}

public class mAdapter extends BaseAdapter {

private Context ctx;

List<Integer>count = new ArrayList<Integer>();
public mAdapter(Context c)
{
ctx=c;
}
public void add(int i)
{
count.add(i);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return count.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return count.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup
parent) {
// TODO Auto-generated method stub
//Inflate the view of main.xml
LayoutInflater linf = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = linf.inflate( R.layout.main, parent, false);
// Draw the customview in
new canvas for each list cell
Canvas c = new Canvas();
(v.findViewById(android.R.id.list)).draw(c);

Log.d("LAYOUT", "TIME "+ (v.findViewById(android.R.id.list)).getId
());


return v;
}


}

Can anyone help me to find what I am doing wrong?

Thanks.
Regards,
Purvi

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments:

Post a Comment