Thursday, June 12, 2014

[Java Android] Array of Pseudo Random Integer on TextView (just for self documentation)

Here the code, the random numbers  generated at the creation and at "Start" button click

package com.nugnux.array;
 

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity{
     
	private static String text = "heheh...";//for use as the tag when logging
    	
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        
        proses();
                         
        Button buttonStart = (Button)findViewById(R.id.buttonStart);       
        buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above
       
        Button buttonStop = (Button)findViewById(R.id.buttonStop);       
        buttonStop.setOnClickListener(stopListener); // Register the onClick listener with the implementation above
         	
    }
    
    private void proses(){

        TextView tulisan = (TextView)findViewById(R.id.tulisan);
        tulisan.setText("tadaa...\n");
        tulisan.append("this is array\n");
        
        Random r = new Random();
        int[]arr = new int[5];

        for(int i=0;i&ltarr.length;i++)
        {
            arr[i] = Math.abs(r.nextInt()%255) +1;
            tulisan.append(arr[i] + "\t\t");
        }

    }
    
    //Create an anonymous implementation of OnClickListener
    private OnClickListener startListener = new OnClickListener() {
        public void onClick(View v) {
        	proses();
        	Toast.makeText(MainActivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
        }
    };
    // Create an anonymous implementation of OnClickListener
    private OnClickListener stopListener = new OnClickListener() {
        public void onClick(View v) {
         Toast.makeText(MainActivity.this, "The Stop button was clicked.", Toast.LENGTH_LONG).show();
         TextView tulisan = (TextView)findViewById(R.id.tulisan);
         tulisan.setText("tadaa...");
        }
    };     
}