Live Q&A
This guide shows how to add Viafoura Live Q&A to an Android app using VFLiveQuestionsFragment.
Requirements
- The app includes the Viafoura Android SDK (
com.viafoura:android). - The SDK is initialized before presenting any Viafoura UI.
- You have a Live Q&A
containerIdfor the page, event, or content surface. - The host Activity (or Fragment) implements
VFActionsInterface.
1. Implement Action Handling
Live Q&A can be viewed anonymously, but posting, replying, liking, and moderation actions require authentication. The SDK fires onNewAction with VFActionType.authPressed when the user attempts an authenticated action while logged out. Profile taps are delivered as VFActionType.openProfilePressed.
import com.viafourasdk.src.interfaces.VFActionsInterface;
import com.viafourasdk.src.model.local.VFActionData;
import com.viafourasdk.src.model.local.VFActionType;
public class ArticleActivity extends AppCompatActivity implements VFActionsInterface {
@Override
public void onNewAction(VFActionType actionType, VFActionData action) {
if (actionType == VFActionType.authPressed) {
startActivity(new Intent(this, LoginActivity.class));
} else if (actionType == VFActionType.openProfilePressed) {
String userUUID = action.getOpenProfileAction().userUUID.toString();
Intent intent = new Intent(this, ProfileActivity.class);
intent.putExtra("userUUID", userUUID);
startActivity(intent);
}
}
}After your login flow completes, authenticate the user with the Viafoura auth service. Use the auth method that matches your integration.
ViafouraSDK.auth().cookieLogin(viafouraCookieToken, new CookieLoginCallback() {
@Override
public void onSuccess(CookieLoginResponse response) {
// User is now authenticated.
}
@Override
public void onError(NetworkError error) {
// Show your login error state.
}
});Other supported auth methods include login(email, password, callback), openIdLogin(token, callback), socialLogin(token, callback), and loginRadiusLogin(token, callback).
2. Build Article Metadata
Live Q&A posts include article metadata for analytics, moderation context, and profile/history surfaces.
VFArticleMetadata articleMetadata = new VFArticleMetadata(
"https://example.com/articles/live-qa",
"Live Q&A",
"Ask questions during the live event",
"https://example.com/images/live-qa.jpg"
);3. Create the Live Q&A Fragment
Use VFLiveQuestionsFragment.newInstance(...).
VFColors colors = new VFColors(
ContextCompat.getColor(this, R.color.colorPrimary),
ContextCompat.getColor(this, R.color.colorPrimaryLight)
);
VFSettings settings = new VFSettings(colors);
VFLiveQuestionsFragment fragment = VFLiveQuestionsFragment.newInstance(
"YOUR_LIVE_QA_CONTAINER_ID",
articleMetadata,
settings
);Optional parameters:
VFLiveQuestionsFragment fragment = VFLiveQuestionsFragment.newInstance(
"YOUR_LIVE_QA_CONTAINER_ID",
articleMetadata,
settings,
20, // limit
2, // replyLimit
null // sectionUUID
);containerId: your external Live Q&A container id.articleMetadata: metadata for the page or event.settings: Viafoura colors, fonts, and theme settings.limit: number of top-level questions loaded per page. Defaults to20.replyLimit: number of replies loaded with each question. Defaults to2.sectionUUID: optional. Ifnull, the SDK uses the default site UUID.
4. Set the Actions Interface
Wire up the actions interface so the fragment can request login and open profiles.
fragment.setActionsInterface(this);Optionally, set a custom UI interface to further customize views:
fragment.customUIInterface = new VFCustomUIInterface() {
@Override
public void customizeView(VFTheme theme, VFCustomViewType customViewType, View view) {
// Apply custom styling to SDK-provided views.
}
};5. Apply Theme
Set the theme on VFColors before creating the fragment to match your app's light or dark mode.
VFColors colors = new VFColors(
ContextCompat.getColor(this, R.color.colorPrimary),
ContextCompat.getColor(this, R.color.colorPrimaryLight)
);
int nightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
colors.setTheme(nightMode == Configuration.UI_MODE_NIGHT_YES ? VFTheme.dark : VFTheme.light);
VFSettings settings = new VFSettings(colors);6. Add the Fragment to Your Layout
Add a container view in your Activity layout:
<FrameLayout
android:id="@+id/live_questions_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />Then commit the fragment in onCreate:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.live_questions_container, fragment);
ft.commit();You can also push it onto a back stack:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.live_questions_container, fragment);
ft.addToBackStack(null);
ft.commit();Opening a User Profile
To open a profile when VFActionType.openProfilePressed fires, create VFProfileFragment with the user UUID from the action payload.
import com.viafourasdk.src.fragments.profile.VFProfileFragment;
import com.viafourasdk.src.model.local.VFProfilePresentationType;
// Inside onNewAction:
if (actionType == VFActionType.openProfilePressed) {
UUID userUUID = action.getOpenProfileAction().userUUID;
VFProfileFragment profileFragment = VFProfileFragment.newInstance(
userUUID,
VFProfilePresentationType.presentation,
settings
);
getSupportFragmentManager().beginTransaction()
.replace(R.id.live_questions_container, profileFragment)
.addToBackStack(null)
.commit();
}Complete Example
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;
import com.viafourasdk.src.fragments.livequestions.VFLiveQuestionsFragment;
import com.viafourasdk.src.interfaces.VFActionsInterface;
import com.viafourasdk.src.model.local.VFActionData;
import com.viafourasdk.src.model.local.VFActionType;
import com.viafourasdk.src.model.local.VFArticleMetadata;
import com.viafourasdk.src.model.local.VFColors;
import com.viafourasdk.src.model.local.VFSettings;
public class ArticleActivity extends AppCompatActivity implements VFActionsInterface {
private static final String LIVE_QA_CONTAINER_ID = "YOUR_LIVE_QA_CONTAINER_ID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
VFColors colors = new VFColors(
ContextCompat.getColor(this, R.color.colorPrimary),
ContextCompat.getColor(this, R.color.colorPrimaryLight)
);
VFSettings settings = new VFSettings(colors);
VFArticleMetadata metadata = new VFArticleMetadata(
"https://example.com/articles/live-qa",
"Live Q&A",
"Ask questions during the live event",
"https://example.com/images/live-qa.jpg"
);
VFLiveQuestionsFragment fragment = VFLiveQuestionsFragment.newInstance(
LIVE_QA_CONTAINER_ID,
metadata,
settings,
20,
2,
null
);
fragment.setActionsInterface(this);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.live_questions_container, fragment);
ft.commit();
if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onNewAction(VFActionType actionType, VFActionData action) {
if (actionType == VFActionType.authPressed) {
startActivity(new Intent(this, LoginActivity.class));
} else if (actionType == VFActionType.openProfilePressed) {
String userUUID = action.getOpenProfileAction().userUUID.toString();
Intent intent = new Intent(this, ProfileActivity.class);
intent.putExtra("userUUID", userUUID);
startActivity(intent);
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}Updated 3 days ago
