<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
tv.append means ?
ReplyDeleteIt adds the text in the last of TextView.
DeleteIn this example why you using Linear Layout
ReplyDeleteyou can use other layout or simply use only scrollview layout.
Deletewhy i can't see 0 to 100 value.
ReplyDeletecheck your height and width of textView, both should be wrap-content, and scrollView height should be wrap-content, may this display values for you.
Deleteso it's going next row after adding text view right
ReplyDeletethanks...
ReplyDeleteThnx
ReplyDelete