There are a few best practices that you can implement in your android application. These are provided by Android which helps in improving performance, design, features, privacy, security, and distribution.
To better understand the best practices of Android, you can refer to the example below:
1.You will need to create an Android application by using Android Studio IDE under the package com.firstapp.ashulakhwan.greatlearning.
2. The next step that you will do is to modify MainActivity.java as shown below:
package com.firstapp.ashulakhwan.greatlearning;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.support.v6.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditContent;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditContent editOne,editTwo;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editOne=(EditContent)findViewById(R.id.editText);
editTWo=(EditContent)findViewById(R.id.editText2);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean chargingStatus = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
boolean chargingonUSB = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean chargingonAC = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if(chargingonUSB){
Toast.makeText(getApplicationContext(), "The mobile is getting charged on USB",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "The mobile is getting charged on AC",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
3.After that, you will have to modify the layout XML file activity_main.xml to add GUI components to the application. After modification, the file will look like this:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="Android Best Practices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Great Learning"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#fb2930"
android:textSize="40dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="First Name"
android:layout_below="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:numeric="integer" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:hint="Security Key"
android:layout_below="@+id/editText"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText"
android:password="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Try"
android:id="@+id/button"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Now, all modifications are done from your end and you can run the application on an android device or Emulator. This way you can try the best practices while working on your application development.