Question

I would like to create a fill in the blank activity.
Example: "The weather was (EDIT TEXT HERE) yesterday."
"The boys played(EDIT TEXT HERE) at recess." "Lucy drank (EDIT TEXT HERE) at lunch yesterday."

The way I have it set up now, I have a text view before and after the edit text. The problem is the text of the text views are being set by putStringExtra method sent from an intent. I never know how long the text of either view will be. If the text is too long, then the second view's text gets bunched up at the end. That is, the text wraps on the second text view. What I'd like to achieve is the text to wrap around the whole screen. Currently, I'm checking the amount of lines of the second text view. If there's more than one, then I'm placing that text in a textview in a layout below the first layout. It seems like there's an easier way to do this. Here's my code, thanks for your help:

 public void setText(double w, double t) {
    final double HalfScreenWidth = w;
    final double totalScreenWidth = t;
    TextView1.setText(getIntent().getExtras().getString("SentPart1"));
    TextView2.setText(getIntent().getExtras().getString("SentPart2"));
    TextView3.setText("");


    TextView1.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    tVWidth = TextView1.getMeasuredWidth();

    if (totalScreenWidth > 1100) {
        linLay1.removeAllViews();
        linLay2.removeAllViews();

        linLay1.addView(TextView1);
        linLay1.addView(EditText);
        linLay1.addView(TextView2);
        linLay1.addView(TextView3);
    } else if (tVWidth > HalfscreenWidth && totalScreenWidth < 1100) {
        linLay1.removeAllViews();
        linLay2.removeAllViews();

        linLay1.addView(TextView1);
        linLay1.addView(EditText);
        linLay2.addView(TextView2);
    } else if (tVWidth < HalfScreenWidth && totalScreenWidth < 1100) {
        linLay1.removeAllViews();
        linLay2.removeAllViews();
        linLay1.setVisibility(View.INVISIBLE);
        linLay2.setVisibility(View.INVISIBLE);
        linLay1.addView(TextView1);
        linLay1.addView(EditText);
        linLay1.addView(TextView2);
        linLay2.addView(TextView3);
        h.postDelayed(r, 100);

    }

}

final Runnable r = new Runnable() {
    public void run() {

        int lineCount = TextView2.getLineCount();
        if (lineCount == 1) {
            linLay2.removeView(TextView3);
            linLay1.addView(TextView3);
        } else if (lineCount == 2) {
            int lineStart0 = TextView2.getLayout().getLineStart(0);
            int lineEnd0 = TextView2.getLayout().getLineEnd(0);
            int lineStart = TextView2.getLayout().getLineStart(1);
            int lineEnd = TextView2.getLayout().getLineEnd(1);

            CharSequence extraText0 = TextView2.getText().subSequence(
                    lineStart0, lineEnd0);
            CharSequence extraText =  TextView2.getText().subSequence(lineStart,
                    lineEnd);
            TextView2.setText(extraText0);
            TextView3.setText(extraText);

        } else if (lineCount == 3) {
            int lineStart0 = TextView2.getLayout().getLineStart(0);
            int lineEnd0 = TextView2.getLayout().getLineEnd(0);
            int lineStart1 = TextView2.getLayout().getLineStart(1);
            int lineEnd1 = TextView2.getLayout().getLineEnd(1);
            int lineStart2 = TextView2.getLayout().getLineStart(2);
            int lineEnd2 = TextView2.getLayout().getLineEnd(2);

            CharSequence extraText0 = TextView2.getText().subSequence(
                    lineStart0, lineEnd0);
            CharSequence extraText1 = TextView2.getText().subSequence(
                    lineStart1, lineEnd1);
            CharSequence extraText2 = TextView2.getText().subSequence(
                    lineStart2, lineEnd2);

            CharSequence oneTwo = TextUtils.concat(extraText1, extraText2);
            TextView2.setText(extraText0);
            TextView3.setText(oneTwo);

        } else if (lineCount == 4) {
            int lineStart0 = TextView2.getLayout().getLineStart(0);
            int lineEnd0 = TextView2.getLayout().getLineEnd(0);
            int lineStart1 = TextView2.getLayout().getLineStart(1);
            int lineEnd1 = TextView2.getLayout().getLineEnd(1);
            int lineStart2 = TextView2.getLayout().getLineStart(2);
            int lineEnd2 = TextView2.getLayout().getLineEnd(2);
            int lineStart3 = TextView2.getLayout().getLineStart(3);
            int lineEnd3 = TextView2.getLayout().getLineEnd(3);

            CharSequence extraText0 = TextView2.getText().subSequence(
                    lineStart0, lineEnd0);
            CharSequence extraText1 = TextView2.getText().subSequence(
                    lineStart1, lineEnd1);
            CharSequence extraText2 = TextView2.getText().subSequence(
                    lineStart2, lineEnd2);
            CharSequence extraText3 = TextView2.getText().subSequence(
                    lineStart3, lineEnd3);

            CharSequence oneTwo = TextUtils.concat(extraText1, extraText2,
                    extraText3);
            TextView2.setText(extraText0);
            TextView3.setText(oneTwo);
 }
linLay1.setVisibility(View.VISIBLE);
        linLay2.setVisibility(View.VISIBLE);
}

I put in a runnable because it seems like Android needs a little pause in order to draw the text on the screen.

Was it helpful?

Solution

You could use a LinearLayout in horizontal orientation and put TextView on both sides of your EditText. Something like this :

<LinearLayout 
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="The weather was " /> <!-- Just for short answer, should be put elsewhere -->
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" yesterday." /> <!-- Just for short answer, should be put elsewhere -->
</LinearLayout>

After that in your code, you can set the text like this :

// Written here as if it where put in and Activity
public void setYesterdayWeather(final String text) {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            TextView v = (TextView) findViewById(R.id.myEditText);
            v.setText(text);
        }
    });
}

After that, you can listen to changes using a TextWatcher and resize the EditText so that it always wraps its text like this :

// On inflation, likely in Activity.onCreate()
final TextView v = (TextView) findViewById(R.id.myEditText);
v.addTextChangedListener(new TextWatcher() {

    [...]

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        float textSize = v.getPaint().measureText(s, start, start + count);
        // onTextChanged may run on Ui Thread, but if it's not the case, the following statement must be called inside a Runnable posted to the main Looper.
        v.setWidth(textSize); // Eventually, take some extra space for padding ?
    }
});

OTHER TIPS

you can set it in the activity :

String chaine ="The weather was"+getIntent().getExtras().getString("your String name")+"yesterday." ;
        TextView text = (TextView) findViewById(R.id.text);
text.setText(chaine);

In the XML :

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top