I'm coding an App for learning tones of Chinese characters. The test words contain anything from 1 to 12 Chinese characters, which I want to be evenly distributed over the width of the page, with shorter words having larger characters than longer words. In order to achieve this, I'm filling a horizontal linear layout with programmatically created textviews. Each of these textviews contains one single Chinese character and gets assigned a weight through layout parameters. The width of the textviews gets applied correctly, however, I fail to auto-size the containing characters (again: only one Chinese character per textview) .
I've tried different versions of auto-size, but the text size doesn't change, as it only seems to take the height of the textview into account, not the width. This results in characters being cropped in width for longer words.
This is the Container in which I create single text views: XML
<LinearLayout
android:id="@+id/hanziframe"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100"
>
Here I iterate through the single characters of the test word, giving each the same weight in the above layout:
public void proceedToQuestion(String han) {
int nhanzi = han.length(); //number of characters
float widthperhanzi = 100 / nhanzi;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
0, LayoutParams.MATCH_PARENT,widthperhanzi);
for (int i = 0; i < nhanzi; i++) {
TextView h = new TextView(getApplicationContext());
h.setLayoutParams(lp);
h.setText(han.substring(i, i + 1));
TextViewCompat.setAutoSizeTextTypeWithDefaults(h, AUTO_SIZE_TEXT_TYPE_UNIFORM);
hanziframe.addView(h);
hanzis.add(h);
}
A word with three characters gets displayed ok
A longer word - here the single characters get cropped in width.
How can I make the characters assume the correct size depending on how many there are? Is there any way to calculate the text size directly from the screen size? Or have I just set the layout parameters incorrectly?
Any help is greatly appreciated!