Print Friendly and PDF


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.
<EditText
    android:id="@+id/phone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="phone" />
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.
<EditText
    android:id="@+id/password"
    android:hint="@string/password_hint"
    android:inputType="textPassword" />

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.
  1. Use internal storage rather then external for storing applications files
  2. Use content providers wherever possible
  3. Use SSl when connecting to the web
  4. 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.
StepsDescription
1You 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.
2Modify src/MainActivity.java file to add the code
3Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4Modify res/values/string.xml file and add necessary string components.
5Modify AndroidManifest.xml to add necessary permissions.
6Run 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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >


   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="22dp"
      android:layout_marginTop="20dp"
      android:text="@string/username"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
      android:id="@+id/message"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView1"
      android:layout_below="@+id/textView1"
      android:ems="10"
      android:inputType="textCapSentences|textAutoCorrect" >

   <requestFocus />
</EditText>

   <EditText
      android:id="@+id/password"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView2"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="34dp"
      android:ems="10"
      android:hint="@string/password_hint"
      android:inputType="textPassword" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/textView1"
      android:layout_below="@+id/message"
      android:layout_marginTop="50dp"
      android:text="@string/password"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/password"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="34dp"
      android:onClick="check"
      android:text="@string/check" />

</RelativeLayout>
Here is the content of Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="app_name">BestPractices</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="username">Username</string>
   <string name="password">Password</string>
   <string name="password_hint">Hello world!</string>
   <string name="check">Charging check</string>

</resources>
Here is the content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.bestpractices"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" />

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name="com.example.bestpractices.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
   </application>

</manifest>
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.Anroid Capture Tutorial
Select your mobile device as an option and then check your mobile device which will display following screen.Anroid BestPractices Tutorial
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.Anroid BestPractices Tutorial
Now you will see the hint in the password field. It would disappera as soon as you start writing in the field. It is shown below.Anroid BestPractices Tutorial
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.Anroid BestPractices Tutorial

zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: