I want to create an android application that gives the battery status (%) in a textView. I also have an ImageView containing an image that I would like to change according the value of the battery status. (I have different images of a battery corresponding to the different level of charge).
I can already print the battery status in the textView but I don't know how to change the Image in the Image View. Here is my main Fragment:
public class Battery extends Fragment{
private TextView contentTxt;
private ImageView battery;
int level;
int scale;
int test;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
contentTxt=(TextView) myFragmentView.findViewById(R.id.monospaceTxt);
getActivity().registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return myFragmentView;
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
contentTxt.setText(String.valueOf(level * 100 / scale) + "%");
}
};
}
The BatteryService source:
public class BatteryService extends Service {
private int APP_ID = 10000;
private int currentPercent = 0;
private Intent intentFrom;
public static boolean isRunning;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
Log.d("BATTERY_STAT", intent.getClass().getName());
currentPercent = level * 100 / scale;
//Notification notification = new Notification(R.drawable.icon,
//context.getResources().getString(R.string.your_battery_status), System.currentTimeMillis());
// notification.number = currentPercent;
// notification.flags = Notification.FLAG_NO_CLEAR;
// Intent contentIntent = new Intent(context,Main.class);
// notification.setLatestEventInfo(context,
// context.getResources().getString(R.string.battery_status) + " " + String.valueOf(notification.number) + "%",
// context.getResources().getString(R.string.your_battery_status),
// PendingIntent.getActivity(context, 0, contentIntent,Intent.FLAG_ACTIVITY_NEW_TASK));
// mManager.notify(APP_ID , notification);
}
};
private NotificationManager mManager;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("BATTERY_STAT", "battery service onCreate");
BatteryService.isRunning = true;
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(this.mBatInfoReceiver);
BatteryService.isRunning = false;
removeServiceNotification();
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
intentFrom = intent;
super.onStart(intent, startId);
}
private int getPercentBatt(){
return currentPercent;
}
private final IBattery.Stub binder=new IBattery.Stub() {
public int getPercent() {
return(getPercentBatt());
}
public void stopNotification(){
removeServiceNotification();
}
};
public void removeServiceNotification(){
mManager.cancel(APP_ID);
}
}
and my xml file:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/fond_correct" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Battery Status"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="80px"
android:layout_marginLeft="10px"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="30sp"
/>
<TextView
android:id="@+id/monospaceTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100%"
android:textSize="50px"
android:typeface="monospace"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35px" />
<ImageView
android:id="@+id/battery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/battery1"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
In the private BroadcastReceiver mBatInfoReceive I tried an "if" condition but it's not working, soplease, if you can help me deal with this problem.
Thank you
Maybe i just not understand your question. Your problem is you don't know how to change an image in imageview programmatically ? You can use the setImageResource(resourceId), in your case battery. setImageResource(resourceId). I guess you have different images for different battery levels.