How to display a code fragment in text form?

I am trying to display a piece of code in a textview. But I can not make a long text line automatically scrolling horizontally in text form.

the code:

    TextView code = new TextView(getActivity());
    TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
    code.setBackgroundColor(getResources().getColor(android.R.color.black));
    code.setPadding(5, 5, 5, 5);
    code.setTextColor(getResources().getColor(android.R.color.white));
    code.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
    code.setClickable(true);
    code.setLayoutParams(paramsExample);
    setCode(code, R.raw.codesnippet);
    ll.addView(code);

I create this text view in fragment.fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_ll"
    android:orientation="vertical">

</LinearLayout>

and I start this fragment in coordinatorlayout> nestedscrollview> linearlayout (fragment container)

each size of each width is a "match parent"

this is the setcode method:

   public void setCode(TextView tv, int rawId) {
    Resources res = getResources();
    BufferedReader input = new BufferedReader(new InputStreamReader(
            res.openRawResource(rawId)));
    StringBuffer sb = new StringBuffer();
    try {
        String line;
        while ((line = input.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Spanned spannedCode = Html.fromHtml(sb.toString());
    tv.setText(spannedCode);
}

I got this piece of code (codesnippet.txt) from vim: toHtml.

but it does not scroll horizontally. What to do to make it automatically scroll horizontally, like web browsing?

output with code above:
enter image description here

HorizontalScrollView code.setHorizontallyScrolling(true):
enter image description here
. .

+4
1

TextView HorizontalScrollView, :

<HorizontalScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <TextView android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:scrollHorizontally="true"
        android:text="Horizontal scroll view"/>

</HorizontalScrollView>
0

Source: https://habr.com/ru/post/1660395/


All Articles