Pages

Sunday, 15 September 2013

Display numbers from 1 to 100 in ScrollView in Android

This is second android application and in this application, you will learn how to use scroll view and how to print numbers from 1 to 100. Create new project and drag linear layout after that drag scroll view layout into linear layout. Now take text view from widget and drop into scroll view and give id textview1. The code of android XML file is given below:

How to print numbers from 1 to 100 in ScrollView in Android


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="#391" >
<ScrollView
  android:id="@+id/scrollView1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
<TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="30sp"
  android:textColor="#000"
/>
</ScrollView>
</LinearLayout>

Now open android Java file and initialize text view object and use append method in for loop to display numbers from 1 to 100. If we use setText() method in for loop then it will display only last value 100, So we used append method which will keep previous string and add next string in the last. The code of android Java file is given below:


package com.valtest.first; //your package name

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initialize text view object
        TextView tv=(TextView)findViewById(R.id.textView1);
        //set text color
        tv.setTextColor(Color.RED);
        //print 1 to 100 numbers using for loop
        //use append method to print all numbers
        for(int a=0;a<=100;a++)
        {
              tv.append(a+"\n");
        }
    }     
}

Now runs your project on emulator and mobile and if you have any doubt please comment.

Related Tutorials:-

Change image when we click on buttons

Change image when we click on a single button

Take numbers from EditText and add them and display on TextView

Make Temperature conversion application

Create Menu using XML

9 comments:

  1. tv.append means ?

    ReplyDelete
    Replies
    1. It adds the text in the last of TextView.

      Delete
  2. In this example why you using Linear Layout

    ReplyDelete
    Replies
    1. you can use other layout or simply use only scrollview layout.

      Delete
  3. why i can't see 0 to 100 value.

    ReplyDelete
    Replies
    1. check your height and width of textView, both should be wrap-content, and scrollView height should be wrap-content, may this display values for you.

      Delete
  4. so it's going next row after adding text view right

    ReplyDelete

Back to Top