0

In this block of code which is inside a broadcast receiver I have a calculation. My goal is to get a battery bar with the correct amount of Green inside it. So for the Green I have an image which is essentially a block of solid green. I am not sure if this is the best way to do this but I decided to resize the block using this calculation:

(Height of Block) * (Battery Level/100)

The actual code:

BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context ctxt, Intent intent) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100);
        percentageView.setText(String.valueOf(level)+"%");
        int height_val = (363) * (level / 100); //<-- That's the int which always returns 0
        Log.e("MyTag", String.valueOf(height_val).toString());
        Log.e("MyTAgg", String.valueOf(363 * (level/100)));
        bar.requestLayout();
        bar.getLayoutParams().height= height_val;
    }
};

And it is inside a class. It's registered in the class not in the manifest. My Text View shows me the battery percentage but my Tags always show me that my calculations returns 0. Please do help me understand. Why does this calculation return 0?

Thank you.

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
  • Integer math. This, `int height_val = (363) * (level / 100);` should be `int height_val = (int) (363 * (level / 100.0));` – Elliott Frisch Aug 13 '16 at 19:43
  • are you aware of the result of dividing to integers?? – ΦXocę 웃 Пepeúpa ツ Aug 13 '16 at 19:43
  • No. I did some searching and when I tried casting to integer. But it told me it was redunant. I guess the .0 will change that, Thanks so much. Also any site where I can refer to some integer math? Thank you so much once again. – KISHORE_ZE Aug 13 '16 at 19:53

0 Answers0