Created
February 19, 2019 22:59
-
-
Save CostaFot/36a4b1460baecf27d64f605c76851f45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// the URL we want to load | |
val url = "https://media.giphy.com/media/cYxLgjZI5ezI2lrItX/giphy.gif" | |
val circularProgressDrawable = createSpinner() | |
loadUrlIntoImageView(url, circularProgressDrawable) | |
} | |
/** | |
* a basic spinner created programmatically | |
* this is something that you typically stumble upon on Stack Overflow | |
*/ | |
private fun createSpinner(): CircularProgressDrawable { | |
val circularProgressDrawable = CircularProgressDrawable(this@MainActivity) | |
circularProgressDrawable.strokeWidth = 5f | |
circularProgressDrawable.centerRadius = 30f | |
circularProgressDrawable.start() | |
return circularProgressDrawable | |
} | |
/** | |
* The spinner acts as a placeholder while loading the url | |
* On error will show R.drawable.ic_launcher_foreground | |
*/ | |
private fun loadUrlIntoImageView(url: String, circularProgressDrawable: CircularProgressDrawable) { | |
GlideApp.with(this@MainActivity) | |
.load(url) | |
.placeholder(circularProgressDrawable) | |
.error(R.drawable.ic_launcher_foreground) | |
.into(imageView) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment