Al Phaba
Al Phaba

Reputation: 6755

Best practice to show big text data in an android view?

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

Upvotes: 12

Views: 25129

Answers (6)

saschoar
saschoar

Reputation: 8230

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.

Upvotes: 25

Pankaj Singh
Pankaj Singh

Reputation: 2311

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.

Upvotes: 1

Nipun Gogia
Nipun Gogia

Reputation: 1856

<?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>   

Upvotes: 1

Sudarshan Bhat
Sudarshan Bhat

Reputation: 3772

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.

Upvotes: 0

Sparky
Sparky

Reputation: 8477

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>

Upvotes: 1

jiduvah
jiduvah

Reputation: 5168

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

Upvotes: 1

Related Questions