Android Best Practices Tutorial For User Input - 2015
There are some practices that you can follow while developing android application. These are suggested by the android itself and they keep on improving with respect to time.
These best practices include interaction design features , performance , security and privacy , compatibility , testing , distributing and monetizing tips. They are narrowed down and are listed as below.
Best Practices - User input
Every text field is intented for a differnet job. For example, some textfields are for text and some are for numbers. If it is for numbers then it is better to dispaly the numeric keypad when that textfield is focused. Its syntax is.
Other then that if your field is for password , then it must show a password hint , so that the user can easily remember the password. It can be achieved as.
Best Practices - Background jobs
There are certain jobs in an application that are running in an application background. Their job might be to fetch some thing from the internet , playing music e.t.c. It is recommended that the long awaiting tasks should not be done in the UI thread and rather in the background by services or AsyncTask.
ASYNCTASK VS SERVICES.
Both are used for doing background tasks , but the service is not affected by most user inteface life cycle events, so it continues to run in circumstances that would shut down an AsyncTask.
Best Practices - Performance
Your application peformance should be upto the mark. But it should perform differently not on the front end , but on the back end when it the device is connected to a power source or charging. Charging could be of from USB and from wire cable.
When your device is charging itself , it is recommended to update your application settings if any, such as maximizing your refresh rate whenver the device is connected. It can be done as this.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
// Are we charging / charged? Full or charging.
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
// How are we charging? From AC or USB.
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
Best Practices - Security and privacy
It is very important that your application should be secure and not only the application , but the user data and the application data should also be secured. The security can be increased by the following factors.
- Use internal storage rather then external for storing applications files
- Use content providers wherever possible
- Use SSl when connecting to the web
- Use appropriate permissions for accessing different functionalities of device
Example
The below example demonstrates some of the best practices you should follow when developing android application. It crates a basic application that allows you to specify how to use text fields and how to increase performance by checking the charging status of the phone.
To experiment with this example , you need to run this on an actual device.
Steps | Description |
---|---|
1 | You will use Eclipse IDE to create an Android application and name it as BestPractices under a package com.example.autocomplete. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs. |
2 | Modify src/MainActivity.java file to add the code |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 | Modify res/values/string.xml file and add necessary string components. |
5 | Modify AndroidManifest.xml to add necessary permissions. |
6 | Run the application and choose a running android device and install the application on it and verify the results. |
Here is the content of src/com.example.bestpractices/MainActivity.java
package com.example.bestpractices;
import android.os.BatteryManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button Check;
private BatteryManager battery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Check = (Button)findViewById(R.id.button1);
}
public void check(View view){
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED,
-1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if(usbCharge){
Toast.makeText(getApplicationContext(),"Mobile is
charging on USB",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Mobile is
charging on AC",Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is the content of activity_main.xml
Here is the content of Strings.xml
BestPractices
Settings
Hello world!
Username
Password
Hello world!
Charging check
Here is the content of AndroidManifest.xml
Let's try to run your BestPractices application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen.
Now jsut type on the username field and you will see the built in android suggestions from the dictionary will start coming up. This is shown below.
In the end , just connect your device to AC cable or USB cable and press on charging check button. In my case , i connect it with a PC via USB cable so it shows the following message.