• Have Any Question?
  • contact@eitworld.com

write an application to print the counting up to 5 with the help of text view and array





In this application, we will use the text view for printing the number which is stored in the string type array. The xml file is as follows:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” ><TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Print Counting on text view using array” />
<TextView
android:id=”@+id/tvCount”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView2″
android:layout_below=”@+id/textView2″
android:layout_marginTop=”26dp”
android:text=”Counting”
android:textAppearance=”?android:attr/textAppearanceMedium” />

</RelativeLayout>

In this xml file we have used the relative layout and use the text view to print the counting. The java file is as follows:

package com.example.arraytextview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

String[] numCount = {“1″,”2″,”3″,”4″,”5”,};
TextView count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

count = (TextView) findViewById(R.id.tvCount);
for(int i=0;i<numCount.length;i++)
{
count.append(“n”);
count.append(numCount[i]);
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

In this java file we have used the following packages:

android

Here we have declared the following variables:
android

Here we have declared the string array named numCount which is initialized with the string values and declared a text view type variable count to display the numbers.
After that we have to register the controls inside the onClick() method as follows:
android

Here we have registered the control using the id defined in the xml file. Here we have used for loop. Here we have used declared and initialized I variable with value 0 and define the condition i<numCount.lenght; means this loop will continue till the end of the array. Inside the loop, we have used append() method to append the string value of array.
The output is as follows:

android



Real Time Web Analytics