Skip to content

Commit 7e9aeb0

Browse files
Make help FAQ items expandable
1 parent 20b2c0c commit 7e9aeb0

File tree

5 files changed

+65
-11
lines changed

5 files changed

+65
-11
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/help/HelpActivity.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import android.widget.FrameLayout;
1313

1414
import androidx.annotation.Nullable;
15-
import androidx.appcompat.widget.LinearLayoutCompat;
16-
1715
import androidx.annotation.NonNull;
1816
import androidx.annotation.StringRes;
1917
import androidx.appcompat.app.AlertDialog;
18+
import androidx.appcompat.widget.LinearLayoutCompat;
19+
import androidx.core.view.ViewCompat;
2020
import androidx.lifecycle.ViewModelProvider;
2121
import androidx.preference.Preference;
2222
import androidx.preference.PreferenceFragmentCompat;
@@ -218,11 +218,29 @@ private void bindFaqItems(ActivityHelpBinding binding) {
218218
ItemHelpFaqBinding itemBinding = ItemHelpFaqBinding.inflate(inflater, faqList, false);
219219
itemBinding.question.setText(item.questionResId);
220220
itemBinding.answer.setText(item.answerResId);
221+
itemBinding.answer.setVisibility(View.GONE);
222+
itemBinding.toggleIcon.setRotation(0f);
223+
itemBinding.questionContainer.setContentDescription(itemBinding.question.getText());
224+
ViewCompat.setStateDescription(itemBinding.questionContainer, getString(R.string.faq_state_collapsed));
225+
itemBinding.questionContainer.setOnClickListener(v -> toggleFaqItem(itemBinding));
221226
itemBinding.divider.setVisibility(i == FAQ_ITEMS.size() - 1 ? View.GONE : View.VISIBLE);
222227
faqList.addView(itemBinding.getRoot());
223228
}
224229
}
225230

231+
private void toggleFaqItem(ItemHelpFaqBinding binding) {
232+
boolean expand = binding.answer.getVisibility() != View.VISIBLE;
233+
binding.answer.setVisibility(expand ? View.VISIBLE : View.GONE);
234+
float rotation = expand ? 180f : 0f;
235+
binding.toggleIcon.animate().cancel();
236+
binding.toggleIcon.animate()
237+
.rotation(rotation)
238+
.setDuration(200L)
239+
.start();
240+
ViewCompat.setStateDescription(binding.questionContainer,
241+
getString(expand ? R.string.faq_state_expanded : R.string.faq_state_collapsed));
242+
}
243+
226244
private static final class FaqItem {
227245
@StringRes
228246
private final int questionResId;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:width="24dp"
4+
android:height="24dp"
5+
android:viewportWidth="24"
6+
android:viewportHeight="24">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z" />
10+
</vector>

app/src/main/res/layout/item_help_faq.xml

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,51 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent"
55
android:layout_height="wrap_content"
6-
android:orientation="vertical"
7-
android:paddingVertical="16dp">
6+
android:orientation="vertical">
87

9-
<com.google.android.material.textview.MaterialTextView
10-
android:id="@+id/question"
8+
<androidx.appcompat.widget.LinearLayoutCompat
9+
android:id="@+id/question_container"
1110
android:layout_width="match_parent"
1211
android:layout_height="wrap_content"
13-
android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
14-
android:textStyle="bold" />
12+
android:background="?attr/selectableItemBackground"
13+
android:clickable="true"
14+
android:focusable="true"
15+
android:gravity="center_vertical"
16+
android:orientation="horizontal"
17+
android:paddingVertical="12dp">
18+
19+
<com.google.android.material.textview.MaterialTextView
20+
android:id="@+id/question"
21+
android:layout_width="0dp"
22+
android:layout_height="wrap_content"
23+
android:layout_weight="1"
24+
android:paddingEnd="16dp"
25+
android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
26+
android:textStyle="bold" />
27+
28+
<androidx.appcompat.widget.AppCompatImageView
29+
android:id="@+id/toggle_icon"
30+
android:layout_width="24dp"
31+
android:layout_height="24dp"
32+
android:contentDescription="@null"
33+
android:importantForAccessibility="no"
34+
android:tint="?attr/colorOnSurfaceVariant"
35+
app:srcCompat="@drawable/ic_expand_more_24" />
36+
37+
</androidx.appcompat.widget.LinearLayoutCompat>
1538

1639
<com.google.android.material.textview.MaterialTextView
1740
android:id="@+id/answer"
1841
android:layout_width="match_parent"
1942
android:layout_height="wrap_content"
2043
android:layout_marginTop="8dp"
21-
android:textAppearance="@style/TextAppearance.Material3.BodyMedium" />
44+
android:textAppearance="@style/TextAppearance.Material3.BodyMedium"
45+
android:visibility="gone" />
2246

2347
<com.google.android.material.divider.MaterialDivider
2448
android:id="@+id/divider"
2549
android:layout_width="match_parent"
2650
android:layout_height="wrap_content"
27-
android:layout_marginTop="16dp" />
51+
android:layout_marginTop="12dp" />
2852

2953
</androidx.appcompat.widget.LinearLayoutCompat>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@
338338
<string name="summary_preference_faq_7">You can disable Firebase analytics and crashlytics by going to the settings menu in the app and toggle the switch for these features.</string>
339339
<string name="summary_preference_faq_8">You can check for updates to Android Studio Tutorials: Java Edition by going to the settings menu in the app and selecting the \"Check for updates\" option.</string>
340340
<string name="summary_preference_faq_9">You can support the development of Android Studio Tutorials: Java Edition by leaving a positive review on the Google Play Store, sharing the app with friends and colleagues, and supporting the developers through the \"Share\" option in the settings menu.</string>
341+
<string name="faq_state_collapsed">Collapsed</string>
342+
<string name="faq_state_expanded">Expanded</string>
341343
<string name="summary_preference_permissions_ad_id">Allows the app to retrieve and use the advertising identifier associated with the user\'s device, providing personalized ads, measuring ad effectiveness, and showing ads on Android 13 or later devices.</string>
342344
<string name="summary_preference_permissions_internet">Allows the app to establish an internet connection to send error reports or check for updates.</string>
343345
<string name="summary_preference_permissions_post_notifications">Allows the app to display notifications on the devices with Android 13 or later.</string>

docs/screens/help.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The Help screen is implemented as an `Activity` that composes different sections
1111

1212
## Features
1313
The Help screen offers the following functionalities, accessible primarily through an options menu:
14-
- **View FAQs**: Users can browse a list of common questions and answers.
14+
- **View FAQs**: Users can browse a list of common questions and answers, expanding items on demand to reveal the detailed responses.
1515
- **Provide Feedback**: Users can initiate a review flow or be directed to the Google Play Store to leave a review.
1616
- **View in Google Play**: Opens the app's listing on the Google Play Store.
1717
- **Version Info**: Displays a dialog with the app's name, version, and copyright information.

0 commit comments

Comments
 (0)