Use flexbox for radical selection and improve interface

Julien LepillerMon Apr 27 14:57:10+0200 2020

cf2b22c

Use flexbox for radical selection and improve interface

app/build.gradle

2424
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
2525
    implementation 'com.google.android.material:material:1.1.0'
2626
    implementation 'com.andree-surya:moji4j:1.0.0'
27+
    implementation 'com.google.android:flexbox:2.0.1'
2728
    implementation project(path: ':furiganatextview')
2829
    testImplementation 'junit:junit:4.12'
2930
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'

app/src/main/java/eu/lepiller/nani/RadicalSelectorView.java

22
33
import android.content.Context;
44
import android.os.AsyncTask;
5+
import android.os.Build;
56
import android.util.AttributeSet;
67
import android.util.Log;
78
import android.util.Pair;

910
import android.view.View;
1011
import android.widget.Button;
1112
import android.widget.LinearLayout;
12-
import android.widget.TableLayout;
13-
import android.widget.TableRow;
1413
import android.widget.TextView;
1514
import android.widget.ToggleButton;
1615
16+
import com.google.android.flexbox.FlexWrap;
17+
import com.google.android.flexbox.FlexboxLayout;
18+
1719
import java.util.ArrayList;
18-
import java.util.HashSet;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Set;

2728
        OnOtherTaskCompleted<Pair<Map<Integer, List<String>>, Set<String>>> {
2829
    private Button close_button;
2930
    private LinearLayout kanji_row1, kanji_row2;
30-
    private TableLayout radical_table;
31+
    private LinearLayout radical_table;
3132
    final List<String> selected = new ArrayList<>();
3233
    View.OnClickListener kanjiSelectionListener;
3334
    RadicalDict dictionary;

6869
            return;
6970
7071
        for (int stroke : radicals.keySet()) {
71-
            TableRow row = new TableRow(getContext());
7272
            TextView strokeView = new TextView(getContext());
7373
            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);
7578
7679
            List<String> lst = radicals.get(stroke);
7780
            for (final String radical : lst) {

7982
                radicalButton.setTextOff(radical);
8083
                radicalButton.setTextOn(radical);
8184
                radicalButton.setText(radical);
82-
                radicalButton.setLayoutParams(new TableRow.LayoutParams(122, 122));
85+
                radicalButton.setLayoutParams(new FlexboxLayout.LayoutParams(122, 122));
8386
8487
                radicalButton.setOnClickListener(new OnClickListener() {
8588
                    @Override

98101
                        new DictionarySearchTask(RadicalSelectorView.this, dictionary).execute(set);
99102
                    }
100103
                });
101-
                row.addView(radicalButton);
104+
                box.addView(radicalButton);
102105
            }
103-
            radical_table.addView(row);
106+
107+
            box.setFlexWrap(FlexWrap.WRAP);
108+
            box.invalidate();
104109
        }
105110
    }
106111

191196
        for (int stroke : kanji.keySet()) {
192197
            TextView strokeView = new TextView(getContext());
193198
            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+
            }
194206
            row.addView(strokeView);
195207
196208
            for (String k : kanji.get(stroke)) {

198210
                Button kanji_button = new Button(getContext());
199211
                kanji_button.setText(k);
200212
                kanji_button.setOnClickListener(kanjiSelectionListener);
213+
                kanji_button.setLayoutParams(new LayoutParams(122, 122));
201214
                row.addView(kanji_button);
202215
203216
                if (number > total / 2)

209222
    private void updateRadicals(Set<String> available) {
210223
        Log.v("SELECTOR", Integer.toString(radical_table.getChildCount()));
211224
        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+
                    }
217232
                }
218233
            }
219234
        }

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

3030
        android:layout_width="match_parent"
3131
        android:layout_height="wrap_content"
3232
        android:layout_weight="1">
33-
34-
        <HorizontalScrollView
35-
            android:layout_width="match_parent"
36-
            android:layout_height="wrap_content">
37-
38-
            <TableLayout
33+
            <LinearLayout
3934
                android:id="@+id/radical_table"
40-
                android:layout_width="wrap_content"
35+
                android:layout_width="match_parent"
4136
                android:layout_height="wrap_content"
42-
                android:orientation="vertical">
43-
44-
            </TableLayout>
37+
                android:orientation="vertical"
38+
                android:padding="8dp">
4539
46-
        </HorizontalScrollView>
40+
            </LinearLayout>
4741
    </ScrollView>
4842
4943
    <LinearLayout