Custom Picker

screenCustom Picker is an easy to use and customizable alternate to UIPickerView. It is written in Swift language. The Custom Picker started as normal UIPicker but it was to limited in its design and methods. Also, I want to achieve a flat-picker with selected area background customizable which was not possible with default UIPicker.

So I made a use UITableView and customize its scroll to get the look and feel of UIPickerView. With the use of UITableView, I have made the picker flat. Now the second limitation of UIPickerView i.e. non-customizable selected area, for this I used an overlay view and customize it according to my need.

Requirements

It requires Xcode 7.0+ and Swift 2.3.

Application Usage

Custom Picker includes a sample of the project of Unit Conversion where a user can select the to and from units and input the number in one UITextField and the output will be shown in other UITextField.

Create a new picker

To use this picker you have to instantiate the CutomPickerView like instantiate any normal UIPickerView and implements it CustomPickerViewDelegate and CustomPickerViewDataSource methods

    let leftPickerView = CustomPickerView(frame: CGRectMake(0.0, 0.0, self.contentViewOfLeftpicker.frame.width,  self.contentViewOfLeftpicker.frame.height))

    leftPickerView.delegate = self
    leftPickerView.dataSource = self

    self.contentViewOfLeftpicker!.addSubview(leftPickerView)

Implement the Delegate and DataSource

The delegate methods that needs to be overridden are

func customPickerView(customPickerView: CustomPickerView, titleForRow row: Int, forLabel label: UILabel) -> String

 func customPickerView(customPickerView: CustomPickerView, didSelectRow row: Int)

func rowHeightForCustomPickerView(customPickerView: CustomPickerView) -> CGFloat

func labelStyleForCustomPickerView(customPickerView: CustomPickerView, forLabel label: UILabel)

func imageStyleForCustomPickerView(customPickerView: CustomPickerView, forImageView imageView: UIImageView)

Firstly you must rowHeightForCustomPickerView(_:)

func rowHeightForCustomPickerView(customPickerView: CustomPickerView) -> CGFloat {
        return 50.0
    }

Then you have to implement titleForRow method it sets the title for each row in a picker

func customPickerView(customPickerView: CustomPickerView, titleForRow row: Int, forLabel label: UILabel) -> String {

        return (label.tag == -1) ? pickerDataFullForm[row] as! String : pickerDataShortForm[row] as! String
    }

Next is the method that provides the didSelectRow

func customPickerView(customPickerView: CustomPickerView, didSelectRow row: Int) {

        leftPickerView.labelUnitFull.text = pickerDataFullForm[row] as? String
        leftPickerView.labelUnitShort.text = pickerDataShortForm[row] as? String

    }

Next method is labelStyleForCustomPickerView method this method is used to give custom style to UILabel in CustomPickerView

func labelStyleForCustomPickerView(customPickerView: CustomPickerView, forLabel label: UILabel) {
        if (label.tag == -1) {

            label.textColor = UIColor.blackColor()
            label.font = UIFont(name: "HelveticaNeue-Bold", size: 20.0)
        }else{

            label.textColor = UIColor.lightGrayColor()
            label.font = UIFont(name: "HelveticaNeue-Bold", size: 15.0)
        }
    }

And the last delegate method is imageStyleForCustomPickerView this is used to give any background image to the overlay view.

    func imageStyleForCustomPickerView(customPickerView: CustomPickerView, forImageView imageView: UIImageView) {
        if customPickerView == leftPickerView {
            leftPickerView.arrowImage.image = UIImage(named: "LeftArrow")
        }
    }

CustomPickerViewDataSource

The CustomPickerView consists of only one data source method. This method returns the number of rows in that picker:

 func numberOfRowsInCustomPickerView(customPickerView: CustomPickerView) -> Int

Using Testfairy for an Android App

TestFairy is a beta testing platform for mobile apps allowing developers to see videos showing users behavior. It allows Android apps as well as iOS apps.

This includes App Distribution, Video Recording, Logs, Crash Reports, User feedback on shake, Community Pages. It gives 2 months free trial.

After that, you have to choose the plan, according to the plan they will provide the features. In basic plan, they have given, App Distribution and Crash Reports features.

Also, they have provided the SDKs for Android and iOS as well. You can refer this link for Testfairy SDK implementation in Android and this link for Testfairy SDK implementation in iOS.

For developer level testing you can use Genymotion and for tester level testing you can use Testfairy.

Do’s and Don’ts for Android Apps

There are few things most of the new android developers face. To avoid these issues while developing one should be following best practices to improve the functionality of the android app and to attain optimum results.

Do’s  for Android App Development

  • Prefer ConstraintLayout while making designs in xml file of android. It is more flexible. If necessary, look for others.
  • Give meaningful id name for each view in xml. For example, If you are using EditText for username and password then their names should be editTextUsername and editTextPassword.
  • If any group of views is repeatable in more than one xml file, make a different file with that group of views and include it where you want. Eg. <include layout=“@layout/toolbar”/> : toolbar is a different layout which has the design for common toolbar used in the app.
  • Write all the texts, used in the app, in string.xml file and give reference of that string in layout xml file. Also, all the messages should be defined in string.xml file. Later on, it will be easier to modify it.
  • Write all the dimensions, used in the app, in dimen.xml file and give reference of that dimensions in layout xml file.
  • Write all the colors, used in the app, in color.xml file and give reference of that color in layout xml file.
  • Try using style.xml file for custom view design instead of doing it programmatically.
  • All constants should be declared in separate java file say, Constants.java, Each constant should be static(it will allocate memory only one time and can be reusable) and final(not changeable).
  • Define all commonly used methods in one class say, Utils.java.
  • If code lines are exceeding 15-20 lines, put the code in a method.
  • The naming of variables, objects, methods, classes, interfaces, etc should be self-explanatory.
  • It’s better to write a separate class for any reusable functionality.
  • Project code should be divided into different modules. According to the module, you can put the java files in different packages under the main package.
  • The module can be Login, DrawerItems (if an app has the drawer, list out all the main files of drawer items), etc.. and it will depend on the project’s features.
  • Use fragment, if you want to show more than one screen in single Activity. A fragment is nothing but the part, part of design. It is totally dependent on Activity.
  • Interfaces can be used to pass the data of one class to other class(it can be Activity, Fragment, Service, or simple Java class) on any triggered event.
  • Service: It is used as a background task. Eg. To do network call, to play music in the background, etc.
  • Use Glide/Picasso library to load image URLs.
  • Use Retrofit library to make network calls. See this blog how to use it in android.
  • Use GreenDAO library to do local database transactions.
  • Use Genymotion virtual device to run the android app.
  • You can use Testfairy to distribute your android app remotely.

 

Similarly, you should know what are bad practices in android development, what to avoid while developing an android app. Below are some points developers should avoid.

Don’ts for Android App Development

  • Try to avoid using Relative layout, if it is not necessary.
  • Avoid using nested Linear layout it affects the UI performance.
  • Never use ‘my’ prefix in naming conventions. Also never use shortcut names. Eg tvNm, etPwd, etc.
  • Your code length for a class should not exceed 1000 lines. If possible try to add a supporting class.
  • Avoid using hard-coded values in Activity/Fragment as much as possible.
  • Avoid using libraries as a project module, instead, add dependencies in build.gradle file of app Module of the project.
  • Don’t use too many libraries.