Use flexbox for radical selection and improve interface
app/build.gradle
24 | 24 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | |
25 | 25 | implementation 'com.google.android.material:material:1.1.0' | |
26 | 26 | implementation 'com.andree-surya:moji4j:1.0.0' | |
27 | + | implementation 'com.google.android:flexbox:2.0.1' | |
27 | 28 | implementation project(path: ':furiganatextview') | |
28 | 29 | testImplementation 'junit:junit:4.12' | |
29 | 30 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' |
app/src/main/java/eu/lepiller/nani/RadicalSelectorView.java
2 | 2 | ||
3 | 3 | import android.content.Context; | |
4 | 4 | import android.os.AsyncTask; | |
5 | + | import android.os.Build; | |
5 | 6 | import android.util.AttributeSet; | |
6 | 7 | import android.util.Log; | |
7 | 8 | import android.util.Pair; | |
… | |||
9 | 10 | import android.view.View; | |
10 | 11 | import android.widget.Button; | |
11 | 12 | import android.widget.LinearLayout; | |
12 | - | import android.widget.TableLayout; | |
13 | - | import android.widget.TableRow; | |
14 | 13 | import android.widget.TextView; | |
15 | 14 | import android.widget.ToggleButton; | |
16 | 15 | ||
16 | + | import com.google.android.flexbox.FlexWrap; | |
17 | + | import com.google.android.flexbox.FlexboxLayout; | |
18 | + | ||
17 | 19 | import java.util.ArrayList; | |
18 | - | import java.util.HashSet; | |
19 | 20 | import java.util.List; | |
20 | 21 | import java.util.Map; | |
21 | 22 | import java.util.Set; | |
… | |||
27 | 28 | OnOtherTaskCompleted<Pair<Map<Integer, List<String>>, Set<String>>> { | |
28 | 29 | private Button close_button; | |
29 | 30 | private LinearLayout kanji_row1, kanji_row2; | |
30 | - | private TableLayout radical_table; | |
31 | + | private LinearLayout radical_table; | |
31 | 32 | final List<String> selected = new ArrayList<>(); | |
32 | 33 | View.OnClickListener kanjiSelectionListener; | |
33 | 34 | RadicalDict dictionary; | |
… | |||
68 | 69 | return; | |
69 | 70 | ||
70 | 71 | for (int stroke : radicals.keySet()) { | |
71 | - | TableRow row = new TableRow(getContext()); | |
72 | 72 | TextView strokeView = new TextView(getContext()); | |
73 | 73 | strokeView.setText(Integer.toString(stroke)); | |
74 | - | row.addView(strokeView); | |
74 | + | radical_table.addView(strokeView); | |
75 | + | ||
76 | + | FlexboxLayout box = new FlexboxLayout(getContext()); | |
77 | + | radical_table.addView(box); | |
75 | 78 | ||
76 | 79 | List<String> lst = radicals.get(stroke); | |
77 | 80 | for (final String radical : lst) { | |
… | |||
79 | 82 | radicalButton.setTextOff(radical); | |
80 | 83 | radicalButton.setTextOn(radical); | |
81 | 84 | radicalButton.setText(radical); | |
82 | - | radicalButton.setLayoutParams(new TableRow.LayoutParams(122, 122)); | |
85 | + | radicalButton.setLayoutParams(new FlexboxLayout.LayoutParams(122, 122)); | |
83 | 86 | ||
84 | 87 | radicalButton.setOnClickListener(new OnClickListener() { | |
85 | 88 | @Override | |
… | |||
98 | 101 | new DictionarySearchTask(RadicalSelectorView.this, dictionary).execute(set); | |
99 | 102 | } | |
100 | 103 | }); | |
101 | - | row.addView(radicalButton); | |
104 | + | box.addView(radicalButton); | |
102 | 105 | } | |
103 | - | radical_table.addView(row); | |
106 | + | ||
107 | + | box.setFlexWrap(FlexWrap.WRAP); | |
108 | + | box.invalidate(); | |
104 | 109 | } | |
105 | 110 | } | |
106 | 111 | ||
… | |||
191 | 196 | for (int stroke : kanji.keySet()) { | |
192 | 197 | TextView strokeView = new TextView(getContext()); | |
193 | 198 | strokeView.setText(Integer.toString(stroke)); | |
199 | + | strokeView.setLayoutParams(new LayoutParams(66, 66)); | |
200 | + | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
201 | + | strokeView.setTextColor(getContext().getColor(R.color.colorAccent)); | |
202 | + | } | |
203 | + | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
204 | + | strokeView.setTextAlignment(TEXT_ALIGNMENT_CENTER); | |
205 | + | } | |
194 | 206 | row.addView(strokeView); | |
195 | 207 | ||
196 | 208 | for (String k : kanji.get(stroke)) { | |
… | |||
198 | 210 | Button kanji_button = new Button(getContext()); | |
199 | 211 | kanji_button.setText(k); | |
200 | 212 | kanji_button.setOnClickListener(kanjiSelectionListener); | |
213 | + | kanji_button.setLayoutParams(new LayoutParams(122, 122)); | |
201 | 214 | row.addView(kanji_button); | |
202 | 215 | ||
203 | 216 | if (number > total / 2) | |
… | |||
209 | 222 | private void updateRadicals(Set<String> available) { | |
210 | 223 | Log.v("SELECTOR", Integer.toString(radical_table.getChildCount())); | |
211 | 224 | for(int row = 0; row < radical_table.getChildCount(); row++) { | |
212 | - | TableRow r = (TableRow) radical_table.getChildAt(row); | |
213 | - | for(int col = 0; col < r.getChildCount(); col++) { | |
214 | - | View child = r.getChildAt(col); | |
215 | - | if(child instanceof ToggleButton) { | |
216 | - | child.setEnabled(available == null || available.contains(((ToggleButton) child).getText().toString())); | |
225 | + | View r = radical_table.getChildAt(row); | |
226 | + | if(r instanceof FlexboxLayout) { | |
227 | + | for (int col = 0; col < ((FlexboxLayout) r).getChildCount(); col++) { | |
228 | + | View child = ((FlexboxLayout) r).getChildAt(col); | |
229 | + | if (child instanceof ToggleButton) { | |
230 | + | child.setEnabled(available == null || available.contains(((ToggleButton) child).getText().toString())); | |
231 | + | } | |
217 | 232 | } | |
218 | 233 | } | |
219 | 234 | } |
app/src/main/res/layout/content_radicals.xml
30 | 30 | android:layout_width="match_parent" | |
31 | 31 | android:layout_height="wrap_content" | |
32 | 32 | android:layout_weight="1"> | |
33 | - | ||
34 | - | <HorizontalScrollView | |
35 | - | android:layout_width="match_parent" | |
36 | - | android:layout_height="wrap_content"> | |
37 | - | ||
38 | - | <TableLayout | |
33 | + | <LinearLayout | |
39 | 34 | android:id="@+id/radical_table" | |
40 | - | android:layout_width="wrap_content" | |
35 | + | android:layout_width="match_parent" | |
41 | 36 | android:layout_height="wrap_content" | |
42 | - | android:orientation="vertical"> | |
43 | - | ||
44 | - | </TableLayout> | |
37 | + | android:orientation="vertical" | |
38 | + | android:padding="8dp"> | |
45 | 39 | ||
46 | - | </HorizontalScrollView> | |
40 | + | </LinearLayout> | |
47 | 41 | </ScrollView> | |
48 | 42 | ||
49 | 43 | <LinearLayout |