I know many edit text wraping questions have been asked, but i failed to find one that can solve my problem(obviously). I am making a texteditor app, and I have createdbconfigurations for customizing the editing options.(font size,type...) Though..one option is not exactly making it up, i need to enable and disable the edit text word wrap At runtime. I achieve this when the setting is changed and app restarted, but thats not ideal, I thoought of replacing the edittext fragment in my code dynamically, when user changes settings, but that wont work as the fragment stores alot of information(current editing details) I was wondering if i could achieve this without restarting the app, or replacing the fragment, so when the user changes to wrapping, text wraps, and when they change to no wrap, it un wraps text.
This is edittext xml fragment_texteditor_layout
I am not wrapping text here as i do it programmatically at runtime depending on users screen width...
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
<HorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
<EditText android:id="@+id/fragment_texteditor_layout_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|start"
android:padding="5dp"
android:inputType="textAutoComplete|textCapSentences|textMultiLine"/>
</HorizontalScrollView>
</ScrollView>
</android.support.v4.widget.NestedScrollView>
I wrap text at runtime by adding onLayoutChangeListener
to the Viewgroup container
parameter, then adjust settings acoordingly.
TextEditorFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_texteditor_layout, container, false);
this.editText = view.findViewById(editTextId);
this.editText.addTextChangedListener(this);
container.addOnLayoutChangeListener(this);
return view;
}
...
//Instance variables
int editorWidth,editorHeight
boolean editorWrap
...
@Override
public void onLayoutChange(View view, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9) {
int width = view.getWidth(), height = view.getHeight();
if (editorWidth != width || editorHeight != height) {
editorWidth = width;
editorHeight = height;
if (editorWrap) editText.setWidth(width); else this.editText.setMinWidth(editorWidth);
this.editText.setMinHeight(editorHeight);
}
}
This wrapping technique is working well, but when user changes settings, they need to restart app for wraping to unwrap. Any help offered is welcome. Thanks in advance.
This is wrapped text...
And this is un wrapped text(requires restart or rotate to take effect).
question from:https://stackoverflow.com/questions/65623492/dynamically-enabling-and-disabling-editext-word-wrap-at-runtime