I read somewhere that it is due to the fact that the metal parts arent grounded, but still why isnt it happening when it is not charging? Can someone explain this to me a little better?
When plugged into the charger, there is likely some leakage current coming in from the mains and since the adapter is only a two-pin thing, there is nowhere to reference the ground to so you end up with a floating voltage.
You will often have a 1Meg or 10Meg resistor from output ground to mains neutra" to discharge static but if your AC plug is reversed, it will be connected to live instead, same if there is a Y-cap between output ground and mains neutral.
If you get shocks from a laptop which has a three pin power plug, then you have a grounding issue.
Your building wiring or outlets may have live and neutral swapped.