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 ?
}
});