0

I have a ListView in one of my activity. I have one customview which is created extending ImageView.I want to add this customview as background of listview.As we have two functions setBackGroundDrawable(Drawable d) and setBackGroundResource(resource).I cant use any one of this ?

Anyone having any good idea how i can do this? I searched but could not get any solution.

Ron
  • 24,175
  • 8
  • 56
  • 97
Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • What is your need? You are trying to set background image for List or for each row of List? Is there any problem to use background property of List? – Pankaj Kumar Aug 20 '11 at 09:51
  • I want to use background for list not for each row? Problem is how can i set a custom view as i said above as the background of listview.Got my point? If possible can you tell me how can i set the id for my customview programmatically ? – Android Killer Aug 20 '11 at 09:58
  • You are setting image (like png files) or custom drawables? – Pankaj Kumar Aug 20 '11 at 10:19
  • custom drawables.means my custom view has one onDraw() method where i m drawing circles. – Android Killer Aug 20 '11 at 10:20

2 Answers2

1

Edited : As I got a tutorial

When you want to dynamically draw some two-dimensional graphics, a ShapeDrawable object will probably suit your needs. With a ShapeDrawable, you can programmatically draw primitive shapes and style them in any way imaginable.

A ShapeDrawable is an extension of Drawable, so you can use one where ever a Drawable is expected — perhaps for the background of a View, set with setBackgroundDrawable(). Of course, you can also draw your shape as its own custom View, to be added to your layout however you please. Because the ShapeDrawable has its own draw() method, you can create a subclass of View that draws the ShapeDrawable during the View.onDraw() method. Here's a basic extension of the View class that does just this, to draw a ShapeDrawable as a View:

public class CustomDrawableView extends View {
    private ShapeDrawable mDrawable;

    public CustomDrawableView(Context context) {
        super(context);

        int x = 10;
        int y = 10;
        int width = 300;
        int height = 50;

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }

    protected void onDraw(Canvas canvas) {
        mDrawable.draw(canvas);
    }
}

In the constructor, a ShapeDrawable is defines as an OvalShape. It's then given a color and the bounds of the shape are set. If you do not set the bounds, then the shape will not be drawn, whereas if you don't set the color, it will default to black.

With the custom View defined, it can be drawn any way you like. With the sample above, we can draw the shape programmatically in an Activity:

CustomDrawableView mCustomDrawableView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCustomDrawableView = new CustomDrawableView(this);

    setContentView(mCustomDrawableView);
}

If you'd like to draw this custom drawable from the XML layout instead of from the Activity, then the CustomDrawable class must override the View(Context, AttributeSet) constructor, which is called when instantiating a View via inflation from XML. Then add a CustomDrawable element to the XML, like so:

<com.example.shapedrawable.CustomDrawableView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />

The ShapeDrawable class (like many other Drawable types in the android.graphics.drawable package) allows you to define various properties of the drawable with public methods. Some properties you might want to adjust include alpha transparency, color filter, dither, opacity and color.

You can also define primitive drawable shapes using XML. For more information, see the section about Shape Drawables in the Drawable Resources document.

Try it :

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.example.shapedrawable.CustomDrawableView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"/>
</RelativeLayout>

And set your custom draw-ables to ImageView.

Hope this will work. :)

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • You got me wrong.I have no drawables.I have custom View which has onDraw() method which is drawing circles on canvas.Any idea now ? – Android Killer Aug 20 '11 at 10:48
0

You cannot use your custom View as Background of ListView. However a workaround could be:

  • Use RelativeLayout as parent layout container.
  • Add a LinearLayout with property of fill_parent for both width and height
  • Give any id as list_background to the LinearLayout.
  • Then add the ListView to the parent RelativeLayout.
  • Add property android:layout_centerInParent="true" to both of the ListView and LinearLayout
  • Remove background of the ListView and your CustomRow if you have any.
  • Now get the LinearLayout in Java and add the View to that LinearLayout
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • In this case if i will scroll then will both scroll or not ? Because using some way(not what u said here) i am getting able to display both but when i m scrolling only listview is scrolling not my custom view as it has canvas to draw.As my custom view extends ImageView.So is it possible to set it's id and then call setBackGroundResource(R.id.xyz) on listview. Is it possible? – Android Killer Aug 20 '11 at 10:18
  • The case which you are describing is not possible using `ListView`. – Adil Soomro Aug 20 '11 at 11:06