I suspect almost everyone has had this experience, and I can assure you that the devil must be very pleased when it happens! Perhaps this is one reason why the Bible urges us to "Set your minds on things above, not on earthly things" (Colossians 3:2).
How can you keep your mind from wandering when you pray or read your Bible? Some people find it helpful to keep a "prayer list" -- a list of the people and events for which you're praying regularly that you can refer to as you pray. (A prayer list can also help you keep track of specific answers to your prayers.) Some people also keep a pen and paper handy as they read the Bible, ready to jot down any truths God is showing them.
The real key, however, is to realize that we are speaking to God when we pray -- and God is speaking to us as we read the Bible. If you were invited to meet with a president or a king, I doubt if your mind would wander. How much greater should our concentration be when we enter the presence of the King of Kings! "Yours, Lord, is the greatness and the power and the glory and the majesty and the splendor, for everything in heaven and earth is yours" (1 Chronicles 29:11).
Never forget that prayer is a privilege, and so is the opportunity to hear God speak to us through His Word, the Bible. Give Him the best minutes of your day.