5

I want to display some locations on my MapView. These locations will show pins which will have numbers 1,2,3.. so on(Similar to Google maps result but it is A,B,C..). I think it is not feasible to have pins of all numbers.

Is there any way that i can just create a layout with pin background with TextView and I will dynamically put number in TextView and that layout ill use as drawable pin??

Or any other method to achieve this?? Please provide some suggestions.

(I am able to show different drawable pins on MapView.I just want to create drawables dynamically)

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Harshad
  • 7,904
  • 3
  • 24
  • 42

1 Answers1

5

Solved this.

Inflated the TextView with pin background and dynamically set the position into TextView.

public Drawable createFromView(int positionNumber)
{
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.drawable.pin_icon, null, false);
    TextView tv = (TextView) view.findViewById(R.id.pin_background);
    tv.setText("     "+ (positionNumber+1) );   // +1 since position is starting from 0
    tv.setDrawingCacheEnabled(true);
    tv.layout(0, 0, 50, 50);
    tv.buildDrawingCache();
    Bitmap b = Bitmap.createBitmap(tv.getDrawingCache());
    tv.setDrawingCacheEnabled(false);
    Drawable d = new BitmapDrawable(b);
    return d;
}
Harshad
  • 7,904
  • 3
  • 24
  • 42
  • This is really great, just what I've been looking for. Do you by any chance also have a code snippet for the layout xml file? – kfox Apr 01 '12 at 02:57
  • @kfox for R.drawable.pin_icon. It is nothing but a Linear Layout containing one TextView.Linear Layout background is your pin. Just align TextView in center. Let me know if you still need it.I'll provide it to you. – Harshad Apr 04 '12 at 18:52
  • If R.drawable.pin_icon is a Linear layout with a text view. Does that mean R.id.pin_background is a linear layout containing the pin image? – Andrew S Feb 04 '13 at 14:35
  • 1
    @AndrewS Sorry for confusion above- R.drawable.pin_icon is an xml file which has LinearLayout. R.id.pin_background is an TextView in some that LinerLayout whose background is your actual pin image. – Harshad Feb 04 '13 at 16:41
  • Got it. Great this means I can set the background dynamically and have different colour pins. How I do this is set the background of the TextView to one of several drawable resources via a switch case. Be sure also to ensure that the tv.layout width and height are set to the size of the drawable. It works well. – Andrew S Feb 04 '13 at 17:06
  • @AndrewS :) I hope your issue is sorted. – Harshad Feb 05 '13 at 14:03