Wednesday, 28 August 2013

Wednesday, 17 July 2013

Custom ProgressBar - Horizontal progress bar with dots

This project provides a horizontal progress bar with dots. 
1. Create a class extends View.

public class DotsProgressBar extends View {
public DotsProgressBar(Context context) {
  super(context);
  init(context);
 }

 public DotsProgressBar(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(context);
 }

 public DotsProgressBar(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init(context);
 }

 private void init(Context context) {
  mRadius = context.getResources().getDimension(R.dimen.circle_indicator_radius);
  // dot fill color
  mPaintFill.setStyle(Style.FILL);
  mPaintFill.setColor(Color.BLACK);
  // dot background color
  mPaint.setStyle(Style.FILL);
  mPaint.setColor(0x33000000);
  start();
 }
}

2. Draw dos in onDraw method

@Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);

  float dX = (widthSize - mDotCount * mRadius * 2 - (mDotCount - 1) * margin) / 2.0f;
  float dY = heightSize / 2;
  for (int i = 0; i < mDotCount; i++) {
   if (i == mIndex) {
    canvas.drawCircle(dX, dY, mRadius, mPaintFill);
   } else {
    canvas.drawCircle(dX, dY, mRadius, mPaint);
   }

   dX += (2 * mRadius + margin);
  }

 }
3. Set dots count
The default cout is 3. You can also set the dots count using setDotsCount(int count) method.


Source Code

Friday, 11 January 2013

Read data from a plist

I am learning iPhone development these days. I need to read data from a plist file into NSArray

Creating a plist

My plist (ClothesArray.plist) is shown as below. It is a little bit complex. There are two dictionaries: US and JP. In each dictionary, it includes a clothes array.
<plist version="1.0">
<dict>
    <key>US</key>
    <dict>
        <key>Clothes</key>
        <array>
            <string>ABC</string>
            <string>DEF</string>
        </array>
     </dict>
     <key>JP</key>
     <dict>
        <key>Clothes</key>
        <array>
            <string>AAA</string>
            <string>XXX</string>
        </array>
     </dict>
</dict>
</plist>

Reading from a plist
What I need is to get the clothes array in each dictionary.

    //Get the path of the plist
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ClothesArray" ofType:@"plist"];
    //Build the Dictionary from the plist
    NSMutableDictionary * dict =  [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    //Get the array by key from the plist
    NSMutableArray *clothesUSMutableArray = [[dict objectForKey:@"US"] objectForKey:@"Clothes"];
     NSMutableArray *clothesJPMutableArray = [[dict objectForKey:@"JP"] objectForKey:@"Clothes"];
    //print
    for(NSString *str in clothesUSMutableArray){
     NSLog(@"%@", str);
    }
Now, I can get the data from the plist into NSArray.