Tuesday, July 29, 2014

Generate random number and display in various radix

This example generate random number, in the range between 0 and user selected progress value of SeekBar (If progress of 0 is selected, the upper limit is open). Then display the value in variou radix, same as the method used in last exercise "Convert String to int to String base on various radix".


package com.example.androidstringformat;

import java.util.Locale;
import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
 
 SeekBar seekbarRange;
 TextView textOut;
 Button buttonGen;
 
 Spinner spAvailableLocale;
 Locale[] availableLocales;
 
 Random random;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        seekbarRange = (SeekBar)findViewById(R.id.seekbarrange);
     textOut = (TextView)findViewById(R.id.printout);
     buttonGen = (Button)findViewById(R.id.buttongen);
     
     buttonGen.setOnClickListener(onClickListener);

     random = new Random();
    }
    
    OnClickListener onClickListener =
     new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    
    int range = seekbarRange.getProgress();
    int randomNum;
    
    if(range==0){
     randomNum = random.nextInt();
    }else{
     randomNum = random.nextInt(range+1);
    }
    
    String result 
    = "default: " + Integer.toString(randomNum) + "\n"
    + "Binary: " + Integer.toBinaryString(randomNum) + "\n"
    + "Binary: " + Integer.toOctalString(randomNum) + "\n"
    + "Radix 10: " + Integer.toString(randomNum, 10) + "\n"
    + "Hex: " + Integer.toHexString(randomNum) + "\n"
    + "Radix 32: " + Integer.toString(randomNum, 32);
   
   textOut.setText(result);

   }};

}


<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.androidstringformat.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <SeekBar
        android:id="@+id/seekbarrange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="0"/>

    <Button
        android:id="@+id/buttongen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" - Generate Random number - " />
    <TextView
        android:id="@+id/printout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

</LinearLayout>


No comments:

Post a Comment