Lets discuss some tools, libraries and techniques which will save you some time as a Android developer.
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.| privateTextView mProgressText;    @Override    protectedvoidonCreate(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    protectedvoidonCreate(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.
| privateButton mDownloadButton;    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mDownloadButton = (Button) findViewById(R.id.btn_download);        mDownloadButton.setOnClickListener(newView.OnClickListener() {            @Override            publicvoidonClick(View v) {                //Download Code            }        });    } | 
| @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);    }    @OnClick(R.id.btn_download)    publicvoiddownloadFile(){    //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.
While there are many websites I tried http://www.jsonschema2pojo.org/ which works well.
 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 doesFor 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(newView.OnClickListener() {            @Override            publicvoidonClick(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()); | 
 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