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>


Bluetooth Low Energy



Bluetooth Low Energy is an incredibly exciting technology with the promise of unlocking a myriad of use cases. This session will focus on new Bluetooth Low Energy features, including improved scanning and newly added support for peripheral mode, in Android.

Monday, July 28, 2014

Convert String to int to String base on various radix

This example show how to 
  • Convert String (user enter in EditText) to integer base on radix, using parseInt(String string) and parseInt(String string, int radix).
  • Convert from integer back to String base on various radix using  toString(int i), toString(int i, int radix), toBinaryString(int i), toOctalString(int i) and toHexString(int i).


package com.example.androidstringformat;

import java.util.Locale;

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

public class MainActivity extends ActionBarActivity {
 
 RadioButton radioDefault, radioBinary, radioOctal, radio10, radioHex, radio32;
 EditText edit1;
 TextView textOut;
 Button buttonPrint;
 
 Spinner spAvailableLocale;
 Locale[] availableLocales;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        radioDefault = (RadioButton)findViewById(R.id.optDefault);
        radioBinary = (RadioButton)findViewById(R.id.optBinary);
        radioOctal = (RadioButton)findViewById(R.id.optOctal);
        radio10 = (RadioButton)findViewById(R.id.opt10);
        radioHex = (RadioButton)findViewById(R.id.optHex);
        radio32 = (RadioButton)findViewById(R.id.opt32);
        
        edit1 = (EditText)findViewById(R.id.edit1);
     textOut = (TextView)findViewById(R.id.printout);
     buttonPrint = (Button)findViewById(R.id.buttonprint);
     
     buttonPrint.setOnClickListener(onClickListener);

    }
    
    OnClickListener onClickListener =
     new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    String strIn = edit1.getText().toString();

    if(!strIn.equals("")){

     int intIn = 0;
     
     try{
      if(radioDefault.isChecked()){
       intIn = Integer.parseInt(strIn);
      }else if(radioBinary.isChecked()){
       intIn = Integer.parseInt(strIn, 2);
      }else if(radioOctal.isChecked()){
       intIn = Integer.parseInt(strIn, 8);
      }else if(radio10.isChecked()){
       intIn = Integer.parseInt(strIn, 10);
      }else if(radioHex.isChecked()){
       intIn = Integer.parseInt(strIn, 16);
      }else if(radio32.isChecked()){
       intIn = Integer.parseInt(strIn, 32);
      }
      
      String result 
       = "default: " + Integer.toString(intIn) + "\n"
       + "Binary: " + Integer.toBinaryString(intIn) + "\n"
       + "Binary: " + Integer.toOctalString(intIn) + "\n"
       + "Radix 10: " + Integer.toString(intIn, 10) + "\n"
       + "Hex: " + Integer.toHexString(intIn) + "\n"
       + "Radix 32: " + Integer.toString(intIn, 32);
      
      textOut.setText(result);
     }catch(NumberFormatException ex){
      textOut.setText(ex.toString());
     }
     
    }
    
   }};

}


<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" />
        
 <RadioGroup
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" >
     <RadioButton
         android:id="@+id/optDefault"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="default" 
         android:checked="true" />
     <RadioButton
         android:id="@+id/optBinary"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Binary" />
     <RadioButton
         android:id="@+id/optOctal"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Octal" />
     <RadioButton
         android:id="@+id/opt10"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Radix 10" />
     <RadioButton
         android:id="@+id/optHex"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Hex" />
     <RadioButton
         android:id="@+id/opt32"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Radix 32" />
 </RadioGroup>

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/buttonprint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" - Print - " />
    <TextView
        android:id="@+id/printout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

</LinearLayout>


Accept negative number in EditText

In last exercise "Display formated number in decimal, octal and hexadecimal using String.format", EditText defined in XML with android:inputType="number". It will not accept minus sign, so the input number is always positive.

To accept minus sign to enter negative number, change it to android:inputType="numberSigned".


<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" />
    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberSigned" />
    <Button
        android:id="@+id/buttonprint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" - Print - " />
    <TextView
        android:id="@+id/printout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Display formated number in decimal, octal and hexadecimal using String.format

How to display formated number in decimal, octal and hexadecimal using String.format, with %d, %o, %x and %X.


In layout file, add android:inputType="number" to EditText to limit the user input to number only.
<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" />
    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />
    <Button
        android:id="@+id/buttonprint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" - Print - " />
    <TextView
        android:id="@+id/printout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


MainActivity.java
package com.example.androidstringformat;

import java.util.Locale;

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

public class MainActivity extends ActionBarActivity {
 
 EditText edit1;
 TextView textOut;
 Button buttonPrint;
 
 Spinner spAvailableLocale;
 Locale[] availableLocales;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        edit1 = (EditText)findViewById(R.id.edit1);
     textOut = (TextView)findViewById(R.id.printout);
     buttonPrint = (Button)findViewById(R.id.buttonprint);
     
     buttonPrint.setOnClickListener(onClickListener);
    }
    
    OnClickListener onClickListener =
     new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    String strIn = edit1.getText().toString();

    if(!strIn.equals("")){
     //convert String to integer
     int inInt = Integer.parseInt(strIn);

     textOut.setText(
      String.format("parse to Integer\n(Decimal): %d\n", inInt) +
      String.format("(Octal): %o\n", inInt) +
      String.format("(hexadecimal): %x\n(HEXADECIMAL): %X\n", inInt, inInt));
    }
    
   }};

}




Next: Accept negative number in EditText