Add the GIPHY Maven repository to your project's build.gradle
file:
maven {
url "http://giphy.bintray.com/giphy-sdk"
}
Then add the GIPHY SDK dependency in the module build.gradle
file:
implementation 'com.giphy.sdk:ui:1.0.0'
Make sure to configure the GIPHY SDK with your Giphy API key, which you can grab from Giphy Developer Portal.
Here is a basic example of how everything should work:
class GiphyActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GiphyCoreUI.configure(this, YOUR_API_KEY)
GiphyDialogFragment.newInstance().show(supportFragmentManager, "giphy_dialog")
}
}
Before you start using GIPHY SDK, you have to initialize it using your GIPHY API KEY. You can apply for an API key here
GiphyCoreUI.configure(this, YOUR_API_KEY)
All the magic is done by the GiphyDialogFragment
. You can adjust the GIPHY SDK by passing a GPHSettings
object when creating the dialog.
Create a settings object to personalize your GIF picker.
var settings = GPHSettings(gridType = GridType.waterfall, theme = LightTheme, dimBackground = true)
Instantiate a GiphyDialogFragment
with the settings object.
val gifsDialog = GiphyDialogFragment.newInstance(settings)
- Theme: set the theme to be
DarkTheme
orLightTheme
settings.theme = LightTheme
- Layout: set the layout to be
.waterfall
(vertical) or.carousel
(horizontal)
settings.gridType = GridType.waterfall
- Media types: Set the content type(s) you'd like to show by setting the
mediaTypeConfig
property, which is an array ofGPHContentType
s
Note: Emoji only is not available for the carousel layout option.
settings.mediaTypeConfig = arrayOf(GPHContentType.gif, GPHContentType.sticker, GPHContentType.text, GPHContentType.emoji)
- Confirmation screen: we provide the option to show a secondary confirmation screen when the user taps a GIF, which shows a larger rendition of the asset.
This confirmation screen is only available for
.waterfall
mode - this property will be ignored if thelayout
is.carousel
.
setting.showConfirmationScreen = true
- Rating: set a specific content rating for the search results. Default
pg13
.
settings.rating = RatingType.pg13
- Rendition: You can change the rendition type for the grid and also for the confirmation screen, if you are using it. Default rendition is
fixedWidth
for the grid andoriginal
for the confirmation screen.
settings.renditionType = RenditionType.fixedWidth
settings.confirmationRenditionType = RenditionType.original
Show your GiphyDialogFragment
using the SupportFragmentManager
and watch as the GIFs start flowin'.
gifsDialog.show(supportFragmentManager, "gifs_dialog")
To handle GIF selection you need to implement the GifSelectionListener
interface.
giphyDialog.gifSelectionListener = object: GiphyDialogFragment.GifSelectionListener {
override fun onGifSelected(media: Media) {
//Your user tapped a GIF
}
override fun onDismissed() {
//Your user dismissed the dialog without selecting a GIF
}
}
From there, it's up to you to decide what to do with the GIF.
Create a GPHMediaView
to display the media. Optionaly, you can pass a rendition type to be loaded.
val mediaView = GPHMediaView(context)
mediaView.setMedia(media, RenditionType.original)
You can also populate a GPHMediaView
with a media id
like so:
mediaView.setMediaWithId(media.id)
There are three button classes provided for you to use if you choose to.
GIPHY branded button available in the following styles:
logo
- full giphy logologoRounded
- same styles aslogo
with rounded cornersiconSquare
- square giphy icon logo with black backgroundiconSquareRounded
- same styles asiconSquare
with rounded cornersiconColor
- color version of giphy icon logo with transparent backgroundiconBlack
- solid black version of the giphy icon logo with transparent backgroundiconWhite
- solid white version of the giphy icon logo with transparent background
val button = GPHGiphyButton(context)
button.style = GPHGiphyButtonStyle.iconBlack
Generic gif button with the text "GIF", available in the following styles:
rectangle
- rectuangular "pill" style button with solid background and transparent textrectangleRounded
- same styles asrectangle
with rounded cornersrectangleOutline
- rectuangluar "pill" style button with solid text and an outlinerectangleOutlineRounded
- same styles asrectangleOutline
with rounded cornerssquare
- same styles asrectangle
but square with smaller textsquareRounded
- same styles assquare
with rounded cornerssquareOutline
- same styles asrectangleOutline
but square with smaller textsquareOutlineRounded
- same styles assquareOutline
with rounded cornerstext
- transparent background button with "gif" text only
The GPHGifButton
is also available in the following colors:
pink
- pink and purple gradientblue
- blue and purple gradientblack
- solid blackwhite
- solid white
val button = GPHGifButton(context)
button.style = GPHGifButtonStyle.squareRounded
button.color = GPHGifButtonColor.blue
Icon buttons for the different supported icon types. These come in the following styles:
stickers
- solid sticker iconstickersOutline
- outline version of thestickers
buttonemoji
- solid emoji smiley iconemojiOutline
- outline version of theemoji
buttontext
- solid text speech bubble icontextOutline
- outline version of thetext
button
The GPHContentTypeButton
is also available in the following colors:
pink
- pink and purple gradientblue
- blue and purple gradientblack
- solid blackwhite
- solid white
val button = GPHContentTypeButton(context)
button.style = GPHContentTypeButtonStyle.emoji
button.color = GPHGifButtonColor.blue