Skip to content

Commit 645ea14

Browse files
Refactor spinner lesson to use shared scroll scaffold
1 parent 99395b9 commit 645ea14

File tree

4 files changed

+134
-104
lines changed

4 files changed

+134
-104
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/android/lessons/views/spinner/SpinnerActivity.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.d4rk.androidtutorials.java.R;
1515
import com.d4rk.androidtutorials.java.ads.AdUtils;
1616
import com.d4rk.androidtutorials.java.databinding.ActivitySpinnerBinding;
17+
import com.d4rk.androidtutorials.java.databinding.ActivitySpinnerContentBinding;
1718
import com.d4rk.androidtutorials.java.ui.components.navigation.UpNavigationActivity;
1819
import com.d4rk.androidtutorials.java.utils.EdgeToEdgeHelper;
1920

@@ -22,6 +23,7 @@
2223
public class SpinnerActivity extends UpNavigationActivity {
2324
private final Handler handler = new Handler(Looper.getMainLooper());
2425
private ActivitySpinnerBinding binding;
26+
private ActivitySpinnerContentBinding contentBinding;
2527

2628
@Override
2729
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -30,42 +32,53 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3032
binding = ActivitySpinnerBinding.inflate(getLayoutInflater());
3133
setContentView(binding.getRoot());
3234
EdgeToEdgeHelper.applyEdgeToEdge(getWindow(), binding.getRoot());
33-
binding.descriptionSection.descriptionHeader.getRoot().setText(R.string.description);
34-
binding.layoutPreviewHeader.getRoot().setText(R.string.layout_preview);
35-
binding.descriptionSection.descriptionText.setText(R.string.summary_spinner);
36-
AdUtils.loadBanner(binding.descriptionSection.adView);
3735

38-
new FastScrollerBuilder(binding.scrollView).useMd2Style().build();
36+
contentBinding = ActivitySpinnerContentBinding.inflate(
37+
getLayoutInflater(),
38+
binding.lessonScaffold.lessonContentContainer,
39+
true
40+
);
41+
42+
contentBinding.descriptionSection.descriptionHeader.getRoot().setText(R.string.description);
43+
contentBinding.layoutPreviewHeader.getRoot().setText(R.string.layout_preview);
44+
contentBinding.descriptionSection.descriptionText.setText(R.string.summary_spinner);
45+
AdUtils.loadBanner(contentBinding.descriptionSection.adView);
46+
47+
new FastScrollerBuilder(binding.lessonScaffold.lessonScrollView).useMd2Style().build();
3948

4049
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
4150
this,
4251
R.array.spinner_lessons_entries,
4352
android.R.layout.simple_spinner_item
4453
);
4554
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
46-
binding.spinnerOptions.setAdapter(adapter);
55+
contentBinding.spinnerOptions.setAdapter(adapter);
4756
if (adapter.getCount() > 0) {
4857
CharSequence firstItem = adapter.getItem(0);
4958
if (firstItem != null) {
50-
binding.textSelectedItem.setText(getString(R.string.spinner_selected_format, firstItem));
59+
contentBinding.textSelectedItem.setText(
60+
getString(R.string.spinner_selected_format, firstItem)
61+
);
5162
}
5263
}
53-
binding.spinnerOptions.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
64+
contentBinding.spinnerOptions.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
5465
@Override
5566
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
5667
CharSequence item = (CharSequence) parent.getItemAtPosition(position);
57-
binding.textSelectedItem.setText(getString(R.string.spinner_selected_format, item));
68+
contentBinding.textSelectedItem.setText(
69+
getString(R.string.spinner_selected_format, item)
70+
);
5871
}
5972

6073
@Override
6174
public void onNothingSelected(AdapterView<?> parent) {
62-
binding.textSelectedItem.setText(R.string.spinner_selected_none);
75+
contentBinding.textSelectedItem.setText(R.string.spinner_selected_none);
6376
}
6477
});
6578

66-
binding.floatingButtonShowSyntax.setOnClickListener(v ->
79+
binding.lessonScaffold.lessonShowCodeFab.setOnClickListener(v ->
6780
startActivity(new Intent(this, SpinnerCodeActivity.class)));
68-
handler.postDelayed(() -> binding.floatingButtonShowSyntax.shrink(), 5000);
81+
handler.postDelayed(() -> binding.lessonScaffold.lessonShowCodeFab.shrink(), 5000);
6982
}
7083

7184
@Override
Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<merge xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
xmlns:tools="http://schemas.android.com/tools"
5-
android:id="@+id/container"
6-
android:layout_width="match_parent"
7-
android:layout_height="match_parent">
4+
xmlns:tools="http://schemas.android.com/tools">
85

9-
<me.zhanghai.android.fastscroll.FastScrollScrollView
10-
android:id="@+id/scroll_view"
11-
android:layout_width="match_parent"
12-
android:layout_height="match_parent"
13-
android:layout_marginBottom="80dp">
14-
15-
<androidx.appcompat.widget.LinearLayoutCompat
16-
android:layout_width="match_parent"
17-
android:layout_height="wrap_content"
18-
android:orientation="vertical"
19-
android:paddingHorizontal="24dp">
20-
21-
<include
22-
android:id="@+id/description_section"
23-
layout="@layout/lesson_description_section" />
24-
25-
<include
26-
android:id="@+id/layout_preview_header"
27-
layout="@layout/lesson_section_header"
28-
tools:text="@string/layout_preview" />
29-
30-
<com.google.android.material.textview.MaterialTextView
31-
android:id="@+id/text_view_spinner_label"
32-
style="@style/TextAppearance.Material3.HeadlineSmall"
33-
android:layout_width="wrap_content"
34-
android:layout_height="wrap_content"
35-
android:layout_marginTop="24dp"
36-
android:text="@string/spinner_choose_label" />
37-
38-
<com.google.android.material.card.MaterialCardView
39-
android:id="@+id/spinner_card"
40-
style="@style/Widget.Material3.CardView.Outlined"
41-
android:layout_width="match_parent"
42-
android:layout_height="wrap_content"
43-
android:layout_marginTop="16dp"
44-
android:layout_marginBottom="24dp"
45-
app:contentPadding="24dp"
46-
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRoundedOutlined">
47-
48-
<androidx.appcompat.widget.LinearLayoutCompat
49-
android:layout_width="match_parent"
50-
android:layout_height="wrap_content"
51-
android:orientation="vertical">
52-
53-
<com.google.android.material.textview.MaterialTextView
54-
android:id="@+id/text_view_spinner_description"
55-
style="@style/TextAppearance.Material3.BodyLarge"
56-
android:layout_width="match_parent"
57-
android:layout_height="wrap_content"
58-
android:text="@string/spinner_description" />
59-
60-
<Spinner
61-
android:id="@+id/spinner_options"
62-
android:layout_width="match_parent"
63-
android:layout_height="wrap_content"
64-
android:layout_marginTop="16dp"
65-
android:prompt="@string/spinner_prompt" />
66-
67-
<com.google.android.material.textview.MaterialTextView
68-
android:id="@+id/text_selected_item"
69-
style="@style/TextAppearance.Material3.BodyLarge"
70-
android:layout_width="match_parent"
71-
android:layout_height="wrap_content"
72-
android:layout_marginTop="16dp"
73-
android:text="@string/spinner_selected_none"
74-
tools:text="@string/spinner_selected_example" />
75-
</androidx.appcompat.widget.LinearLayoutCompat>
76-
</com.google.android.material.card.MaterialCardView>
77-
</androidx.appcompat.widget.LinearLayoutCompat>
78-
</me.zhanghai.android.fastscroll.FastScrollScrollView>
79-
80-
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
81-
android:id="@+id/floating_button_show_syntax"
82-
style="@style/Widget.Material3.ExtendedFloatingActionButton.Icon.Surface"
83-
android:layout_width="wrap_content"
84-
android:layout_height="wrap_content"
85-
android:layout_marginEnd="24dp"
86-
android:layout_marginBottom="24dp"
87-
android:contentDescription="@string/tooltip_show_java_code_snippet"
88-
android:text="@string/show_code"
89-
android:textSize="14sp"
90-
android:tooltipText="@string/tooltip_show_java_code_snippet"
91-
app:icon="@drawable/ic_code"
92-
app:layout_constraintBottom_toBottomOf="parent"
93-
app:layout_constraintEnd_toEndOf="parent"
94-
tools:targetApi="26" />
95-
</androidx.constraintlayout.widget.ConstraintLayout>
6+
<include
7+
android:id="@+id/lesson_scaffold"
8+
layout="@layout/lesson_scroll_scaffold" />
9+
</merge>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<merge xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools">
5+
6+
<include
7+
android:id="@+id/description_section"
8+
layout="@layout/lesson_description_section" />
9+
10+
<include
11+
android:id="@+id/layout_preview_header"
12+
layout="@layout/lesson_section_header"
13+
tools:text="@string/layout_preview" />
14+
15+
<com.google.android.material.textview.MaterialTextView
16+
android:id="@+id/text_view_spinner_label"
17+
style="@style/TextAppearance.Material3.HeadlineSmall"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:layout_marginTop="24dp"
21+
android:text="@string/spinner_choose_label" />
22+
23+
<com.google.android.material.card.MaterialCardView
24+
android:id="@+id/spinner_card"
25+
style="@style/Widget.Material3.CardView.Outlined"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
android:layout_marginTop="16dp"
29+
android:layout_marginBottom="24dp"
30+
app:contentPadding="24dp"
31+
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewTopRoundedOutlined">
32+
33+
<androidx.appcompat.widget.LinearLayoutCompat
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:orientation="vertical">
37+
38+
<com.google.android.material.textview.MaterialTextView
39+
android:id="@+id/text_view_spinner_description"
40+
style="@style/TextAppearance.Material3.BodyLarge"
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content"
43+
android:text="@string/spinner_description" />
44+
45+
<Spinner
46+
android:id="@+id/spinner_options"
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
android:layout_marginTop="16dp"
50+
android:prompt="@string/spinner_prompt" />
51+
52+
<com.google.android.material.textview.MaterialTextView
53+
android:id="@+id/text_selected_item"
54+
style="@style/TextAppearance.Material3.BodyLarge"
55+
android:layout_width="match_parent"
56+
android:layout_height="wrap_content"
57+
android:layout_marginTop="16dp"
58+
android:text="@string/spinner_selected_none"
59+
tools:text="@string/spinner_selected_example" />
60+
</androidx.appcompat.widget.LinearLayoutCompat>
61+
</com.google.android.material.card.MaterialCardView>
62+
</merge>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<merge xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools">
5+
6+
<androidx.constraintlayout.widget.ConstraintLayout
7+
android:id="@+id/lesson_scaffold_root"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent">
10+
11+
<me.zhanghai.android.fastscroll.FastScrollScrollView
12+
android:id="@+id/lesson_scroll_view"
13+
android:layout_width="match_parent"
14+
android:layout_height="match_parent"
15+
android:layout_marginBottom="80dp">
16+
17+
<androidx.appcompat.widget.LinearLayoutCompat
18+
android:id="@+id/lesson_content_container"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:orientation="vertical"
22+
android:paddingHorizontal="24dp" />
23+
</me.zhanghai.android.fastscroll.FastScrollScrollView>
24+
25+
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
26+
android:id="@+id/lesson_show_code_fab"
27+
style="@style/Widget.Material3.ExtendedFloatingActionButton.Icon.Surface"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_marginEnd="24dp"
31+
android:layout_marginBottom="24dp"
32+
android:contentDescription="@string/tooltip_show_java_code_snippet"
33+
android:text="@string/show_code"
34+
android:textSize="14sp"
35+
android:tooltipText="@string/tooltip_show_java_code_snippet"
36+
app:icon="@drawable/ic_code"
37+
app:layout_constraintBottom_toBottomOf="parent"
38+
app:layout_constraintEnd_toEndOf="parent"
39+
tools:targetApi="26" />
40+
</androidx.constraintlayout.widget.ConstraintLayout>
41+
</merge>

0 commit comments

Comments
 (0)