It could be the charging port on your phone going bad or coming loose. It could also be an improperly sized microusb plug on your cable.
Is your cable a Samsung cable? Or even a well respected brand?
If it's not, that might be the issue. I have several "generic" cables that I've accumulated over the years that don't have a tight, snug grip when I plug then into my phone. They wiggle a bit and will randomly connect/disconnect from my phone which sends my phone charging indicator into a fit.
If your cable is good, then it could either be the charging port or the battery. I'd recommend trying a new battery as you can get a good Anker replacement on amazon for like $15.
If the issue persists, you know it's not the battery.