12

In my information area of my app I want to show a brief description of my app. For that I need to create a view with a lot of text. What is the best practice for that ? Actually I created a Linear Layout with a Scrollview, in this i will add my text, which also should contain some variable values e.g. my app version. But I do not know if there are better ways to do that?

Here is my current approach:

 <LinearLayout
     android:id="@+id/information_content"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_above="@id/bottom_footer"
     android:layout_below="@id/top_header"
     android:layout_marginLeft="@dimen/marginLeft"
     android:layout_alignParentRight="true"
     android:layout_weight="1"
     android:orientation="vertical" >

     <ScrollView
         android:id="@+id/scrollView1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent" 
             android:orientation="vertical"
             >

             <EditText
                 android:id="@+id/info_text"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:ems="10"
                 android:inputType="textMultiLine"
                 android:text="Hallo das ist ein test!!!
                 dsf" />

         </LinearLayout>
     </ScrollView>

 </LinearLayout>

Any recommendations?

Thanks

Al Phaba
  • 6,545
  • 12
  • 51
  • 83

6 Answers6

25

You are probably looking for a typical "About Screen" with simple scrollable text that may be formatted using HTML.

Firstly, here is the layout /layout/about_layout.xml. You don't need to wrap your view in an extra LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="12dp" >

    <TextView
      android:id="@+id/about_text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

  </RelativeLayout>

</ScrollView>

Afterwards, add the following to your /values/strings.xml:

<string name="about_text">
  <![CDATA[ 
    Author: Your name<br/><br/>Your description text, changelog, licences etc... ]]>
</string>

Finally, use the layout in your AboutActivity.java, display the string HTML-formatted and enrich it with some auto-updated information, such as your version number.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.about_layout);

    String versionName = "";
    try {
        PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        versionName = pinfo.versionName;
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    TextView aboutTextView = (TextView) findViewById(R.id.about_text_view);

    aboutText = Html.fromHtml("<h1>Your App Name, Version " + versionName + "</h1>"
            + getString(R.string.about_text));
    aboutTextView.setText(aboutText);
}

That's how I deal with the About Screen in my app. Sorry for this answer might be a bit over the top, but I think it's a commonly requested pattern.

saschoar
  • 8,150
  • 5
  • 43
  • 46
  • Is it possible to load the aboutText instead from the strings.xml directly from a file, it would be easier to maintain. – Al Phaba Jan 15 '13 at 14:49
  • 3
    To keep the HTML separate, put the HTML file into the `/assets` or `/raw` folder (described [here](http://stackoverflow.com/a/5771369/1140682)), read the `InputStream` to a string (described [here](http://stackoverflow.com/a/2549222/1140682)) and replace `getString(R.string.about_text)` in my solution with that string. – saschoar Jan 15 '13 at 15:17
  • Hey I did the same for my app. Well the difference is my file size is 1.5 mb and see that my phone hangs tryna load that into a textview directly? What's a better way to do that? – Nobody Apr 29 '17 at 09:26
1

There is no standardised way to display a lot of text. It depends on how you want it to look. However a lot of developers will use the scrollview like you have. Tho if you are only displaying text you should use a textview rather than the edittext you have.

There are other way to display lots of text such as a viewpager

jiduvah
  • 5,108
  • 6
  • 39
  • 55
1

LinearLayouts serve no purpose when there is only one child View. When I wanted to show a lot of text, I used this XML:

<!-- Display the record details in a scrolling text view. -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/[my drawable]"
    android:fillViewport="true"
    android:scrollbars="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/defaultDetail" />

</ScrollView>
Sparky
  • 8,437
  • 1
  • 29
  • 41
1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent">

     <ScrollView 
        android:id="@+id/ScrollView01"
      android:layout_height="150px" 
      android:layout_width="fill_parent">

       <TextView 
       android:id="@+id/TEXT_VIEW" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
     </ScrollView>
    <EditText
        android:id="@+id/EDIT_TEXT"
        android:layout_height="70px"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true"
        android:maxHeight="70px"
    />

</RelativeLayout>   
Nipun Gogia
  • 1,846
  • 1
  • 11
  • 17
1

use this

 <TextView
   android:id="@+id/text_view"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:singleLine="false"
   android:scrollbars="vertical"
   android:textColor="#000"
   >
  </TextView>


 TextView textView = (TextView)findViewById(R.id.text_view);
 textView.setMovementMethod(ScrollingMovementMethod.getInstance());

this is auto scroll textview.

Pankaj Singh
  • 2,241
  • 2
  • 16
  • 16
0

What you're doing is perfect. There are no changes required. But your outermost LinearLayout doesn't make sense if that has only one ScrollView as its child. Except for that, everything is exactly how it should be done.

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53