Polls on Android
Adding the widget
The widget is a fragment, created through a builder.
Java
VFSettings settings = new VFSettings(new VFColors(colorPrimary, colorPrimaryLight));
VFPollFragment poll = new VFPollFragmentBuilder("YOUR_CONTAINER_ID", settings)
.build();
poll.setTheme(VFTheme.light);
poll.setActionCallback(this);
poll.setLayoutCallback(this);
poll.setCustomUICallback(this);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.poll_container, poll)
.commit();Kotlin
val poll = VFPollFragmentBuilder(containerId, settings)
.build()
.apply {
setActionCallback(this@ArticleActivity)
setLayoutCallback(this@ArticleActivity)
setCustomUICallback(this@ArticleActivity)
setTheme(VFTheme.light)
}
supportFragmentManager.beginTransaction()
.replace(R.id.poll_container, poll)
.commit()Call .sectionUUID(uuid) on the builder if the content lives in a section other than your site's default; otherwise the site UUID is used.
To open a poll you already hold the content container UUID for, use the builder's other constructor:
val poll = VFPollFragmentBuilder(contentContainerUUID, settings).build()Jetpack Compose
import com.viafourasdk.compose.VFPoll
VFPoll(
containerId = "YOUR_CONTAINER_ID",
settings = settings,
modifier = Modifier.fillMaxWidth(),
sectionUUID = null,
theme = VFTheme.light,
actionCallback = actionCallback,
layoutCallback = layoutCallback,
)VFPoll(contentContainerUUID = ..., settings = ...) opens a poll from its content container UUID instead. The composable hosts the fragment and recreates it when the container id, content container UUID or section changes; the callbacks are plain VFActionsInterface, VFLayoutInterface and VFCustomUIInterface implementations, the same ones the fragment takes.
Actions
Actions arrive through VFActionsInterface:
@Override
public void onNewAction(VFActionType actionType, VFActionData action) {
switch (actionType) {
case authPressed:
// Reader tried to vote while logged out — start your login flow.
// action.getAuthPromptType() is postContent
break;
default:
break;
}
}Once the reader is authenticated the pending vote is cast automatically.
Height
The widget reports its own height through VFLayoutInterface, in dp:
@Override
public void containerHeightUpdated(VFFragment fragment, String containerId, int height) {
ViewGroup.LayoutParams params = pollContainer.getLayoutParams();
params.height = (int) AndroidUtils.convertDpToPixel(height, this);
pollContainer.setLayoutParams(params);
}The height is 0 whenever the widget hides itself, so a container driven by this callback collapses the widget without leaving a gap. It changes again whenever a moderator moves between the prompt, the editor and the poll, and when results replace the options. When the widget was opened by content container UUID, the containerId argument carries that UUID as a string.
Styling
Colours, fonts and theme come from the VFSettings you pass in, and setTheme(VFTheme) switches the theme at runtime.
For finer control, VFCustomUIInterface hands you these views:
VFCustomViewType | View |
|---|---|
pollBackgroundView | The widget's root layout |
pollHeaderLabel | The small header above the question (active with time remaining, or closed) |
pollQuestionLabel | The poll question |
pollMenuButton | The moderator menu button in the header |
pollOptionContainerView | One option row |
pollOptionLabel | An option's text |
pollOptionPercentageLabel | An option's percentage, once results are visible |
pollOptionRadioImage | The radio indicator shown before voting |
pollOptionVotedImage | The check mark on the option the reader chose |
pollOptionFillView | The results bar behind an option |
pollPrimaryButton | The primary button: create, submit, confirm |
pollSecondaryButton | The secondary button: cancel |
pollMessageTitleLabel | The title of the create prompt, login prompt and confirmations |
pollMessageTextLabel | The body text of those messages |
@Override
public void customizeView(VFTheme theme, VFCustomViewType customViewType, View view) {
if (customViewType == VFCustomViewType.pollPrimaryButton) {
view.setBackgroundResource(R.drawable.my_rounded_button);
}
}Updated 4 days ago
