Saturday, 30 July 2016

Few Tips for Android developers



Lets discuss some tools, libraries and techniques which will save you some time as a Android developer.

Reducing code with Butter knife

Butter Knife
is a View binding library by
Jake Wharton
. It reduces some boilerplate code required and makes your code neat and readable.
For example consider using a TextView in Activity. Check the code before and after using Butter Knife.
private TextView mProgressText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressText = (TextView) findViewById(R.id.progress_text);
    }
@BindView(R.id.progress_text) TextView mProgressText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
Still not impressed, Take a look at using a OnClickListener for a Button. Check the code before and after using Butter Knife.
private Button mDownloadButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDownloadButton = (Button) findViewById(R.id.btn_download);
        mDownloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Download Code
            }
        });
    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
    @OnClick(R.id.btn_download)
    public void downloadFile(){
    //Download Code
    }
While working with Web service API you may need to parse JSON response and store in POJO classes. For simple JSON structure you may create classes and generate Getters and Setters manually. For much complex JSON structures it would be time consuming. There are some websites which do this task for you. You give the JSON data, it generates the POJO class for you.
While there are many websites I tried http://www.jsonschema2pojo.org/ which works well.
Lambda
expressions is a feature which is added to Java 8. You need to use the Google’s
Jack
compiler to use it in your app.
The only disadvantage with Jack is, it is very slow and also it does


For example with lambda expressions you can replace the anonymous inner class code in a interface with -> operator. Example code before and after using lambda expressions.
mDownloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Start Download", Toast.LENGTH_SHORT).show();
            }
        });
    }
mDownloadButton.setOnClickListener(v -> Toast.makeText(MainActivity.this, "Start Download", Toast.LENGTH_SHORT).show());
Gradle please
is a online tool which helps you to find the correct Gradle dependency which you should add to your
build.gradle
.
For example I need to find the Gradle dependency required to add the retrofit library.

Source: learn2crack

No comments:

Post a Comment