Display 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.
No comments:
Post a Comment