Well, so that we are in tune, my idea is to create a logger
I already managed to make it have vertical scroll but how do I make it not fit the text in TextView
it I share the code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="texto" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enviar"
app:layout_constraintTop_toBottomOf="@id/edit"/>
<TextView
android:id="@+id/test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="4"
android:scrollbars="vertical|horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
JAVA:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
final TextView textView = (TextView) findViewById(R.id.test1);
Button but=(Button) findViewById(R.id.send);
final EditText edit=(EditText) findViewById(R.id.edit);
textView.setMovementMethod(new ScrollingMovementMethod());
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text=edit.getText().toString();
textView.append("\n"+text);
}
});
}
}
RESULT:
This is how the vertical scroll works but not the horizontal
MOST ACCURATE ANSWER
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="4"
android:scrollbars="vertical" />
</LinearLayout>
</HorizontalScrollView>
but : being wrap_content the size of the textview is not the best to facilitate vertical movement
By using ScrollingMovementMethod() you can have the ability to enable scrolling,
But in reality if you want a horizontal ScrollView and at the same time a vertical one, this would not be natural since the content of the view is always intended to be displayed within the area that comprises the width of the device.
The only way to add horizontal as well as vertical Scroll is by using the ScrollingMovementMethod() class to enable vertical movement:
and make use of a HorizontalScrollView to enable vertical scrolling.
But there is an important point to consider, when you add the text to your via setText() , it originally does not have line breaks, all the text is on one line, therefore only the horizontal scrollview would be shown:
The secret so that in addition to the horizontal scrollview, the vertical scrollview is activated, is to add line breaks in your text defined in the
EditText
by\n
, so that when this text is added to theTextView
, the vertical scrollview is also activated.Example:
This would be the layout:
I think this would be the simplest option, let the ScrollView adapt and if we require scrolls, it implements them on its own.