Friday, 13 February 2015

Custom Battery icon and time display

Hi!

          If we wants to create our application should have the uniqueness, we have to crate tour own theme. today i am going to give you the tips and code. How to Custom Our Own Battery Icon & Time Displaying..,


1) On the activity class create your own  Broadcast receiver
2) Have to create own battery icon for according to the status of the battery like..,
(Charging, Battery full, 5%,10,50%,80%,100%  and etc..,)


//TextView for display the Time
TextView header_tv_time;
header_tv_time = (TextView) findViewById(R.id.header_tv_time);
header_tv_time.setText(getCurrentTime());

// ImageView for battery 
ImageView header_imv_btry;
header_imv_btry = (ImageView) findViewById(R.id.header_imv_btry);
this.registerReceiver(this.BRecer, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));

// BroadcastReceiver for Battery 
private BroadcastReceiver BRecer = new BroadcastReceiver() {
@Override
public void onReceive(Context ctxt, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

if (level == 0) {
header_imv_btry.setImageResource(R.drawable.zero);
} else if (level >= 1 && level <= 5) {
header_imv_btry.setImageResource(R.drawable.five);
} else if (level >= 6 && level <= 10) {
header_imv_btry.setImageResource(R.drawable.ten);
} else if (level >= 11 && level <= 15) {
header_imv_btry.setImageResource(R.drawable.fiften);
} else if (level >= 16 && level <= 20) {
header_imv_btry.setImageResource(R.drawable.twenty);
} else if (level >= 19 && level <= 25) {
header_imv_btry.setImageResource(R.drawable.twentyfive);
} else if (level >= 24 && level <= 30) {
header_imv_btry.setImageResource(R.drawable.thrty);
} else if (level >= 29 && level <= 35) {
header_imv_btry.setImageResource(R.drawable.thrtyfive);
} else if (level >= 36 && level <= 40) {
header_imv_btry.setImageResource(R.drawable.fourty);
} else if (level >= 41 && level <= 45) {
header_imv_btry.setImageResource(R.drawable.fourtyfive);
} else if (level >= 46 && level <= 50) {
header_imv_btry.setImageResource(R.drawable.fifty);
} else if (level >= 51 && level <= 55) {
header_imv_btry.setImageResource(R.drawable.fiftyfive);
} else if (level >= 56 && level <= 60) {
header_imv_btry.setImageResource(R.drawable.sixty);
} else if (level >= 61 && level <= 65) {
header_imv_btry.setImageResource(R.drawable.sixtyfive);
} else if (level >= 66 && level <= 70) {
header_imv_btry.setImageResource(R.drawable.seventy);
} else if (level >= 71 && level <= 75) {
header_imv_btry.setImageResource(R.drawable.seventyfive);
} else if (level >= 76 && level <= 80) {
header_imv_btry.setImageResource(R.drawable.eigty);
} else if (level >= 81 && level <= 85) {
header_imv_btry.setImageResource(R.drawable.eigtyfive);
} else if (level >= 86 && level <= 90) {
header_imv_btry.setImageResource(R.drawable.ninty);
} else if (level >= 91 && level <= 95) {
header_imv_btry.setImageResource(R.drawable.nintyfive);
} else if (level >= 96 && level <= 100) {
header_imv_btry.setImageResource(R.drawable.sentm);
}
}
};

// Method for provide the current time in Hour:Minutes AM / PM
private String getCurrentTime() {
String delegate = "hh:mm aaa";
return (String) DateFormat.format(delegate, Calendar.getInstance()
.getTime());
}


Thats all..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,