Srikanth Technologies

Android Application - Vehicle Registration Number Finder

We often see owners of new vehicle getting very choosy about their vehicle registration number. They want the number to meet certain criteria. For example, total for digits must be 9.

So, here is an Android application that can be used to get the list of vehicle registration numbers according to your criteria, provided you have an Android device - an Android mobile phone or Tablet.

The following are the options you can select to get list of numbers.

Activity - VehicleNumberFinderActivity.java

package com.st;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

public class VehicleNumberFinderActivity extends Activity {
	private static final double COLUMNS_PER_ROW = 5.0;
	Spinner spinnerTotal, spinnerDigits;
	CheckBox checkAsc;
	ListView listNumbers;
	TextView textNumbers;
	TableLayout tableNumbers;
	ArrayList<String> numbers;
	ProgressDialog pd;
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
   	     super.onSaveInstanceState(outState);
   	     if  (numbers != null) {
   	         outState.putStringArrayList("numbers",numbers);
   	     }
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (savedInstanceState != null) {
			Message msg = numberHandler.obtainMessage();
			msg.obj = savedInstanceState.getStringArrayList("numbers");
			numberHandler.sendMessage(msg);  	   		
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vehicle_number_finder);

		spinnerTotal = (Spinner) this.findViewById(R.id.spinnerTotal);
		spinnerDigits = (Spinner) this.findViewById(R.id.spinnerDigits);
		checkAsc = (CheckBox) this.findViewById(R.id.checkAscending);
		textNumbers = (TextView) this.findViewById(R.id.textNumbers);
		tableNumbers = (TableLayout) this.findViewById(R.id.tableNumbers);
		textNumbers.setVisibility(View.INVISIBLE);
	}
	
	public TextView createTextView(String value) {
		TextView text = new TextView(this);
		text.setBackgroundResource(R.drawable.cell);
		text.setGravity(Gravity.CENTER);
		text.setText(value);
		return text;
	}
	
	final Handler numberHandler = new Handler() {
		public void handleMessage(Message msg) {
			numbers = (ArrayList<String>) msg.obj;
            // clear table
			tableNumbers.removeAllViews();
			// create a table with the data
			int count = numbers.size();
			int rows = (int) Math.ceil(count / COLUMNS_PER_ROW);
			textNumbers.setVisibility(View.VISIBLE);
			TableRow tr = null;
			int pos = 0;
			for (int i = 1; i <= rows; i++) {
				// add a row
				tr = new TableRow( VehicleNumberFinderActivity.this);
				for (int j = 1; j <= 5; j++) {
					if (  pos < count) {
					  tr.addView(createTextView(numbers.get(pos).toString()));
					  pos++;
					}
					else
						tr.addView(createTextView(""));
					
				}
				tableNumbers.addView(tr, new TableLayout.LayoutParams(
						LayoutParams.MATCH_PARENT,
						LayoutParams.WRAP_CONTENT));
			}
			textNumbers.setText(String.format(
            "List of Vehicle registration numbers with minimum of [%s] digits and with digits total [%s] are:", 
            spinnerDigits.getSelectedItem().toString(), spinnerTotal.getSelectedItem().toString()));
		
     } // end of handleMessage
	};

	public void getNumbers(View v) {
		
		pd = ProgressDialog.show(this,"","Processing....");
		class NumbersThread extends Thread {
			public void run() {
				int total = Integer.parseInt(spinnerTotal.getSelectedItem()
						.toString());
				int digits = Integer.parseInt(spinnerDigits.getSelectedItem()
						.toString());
				int startNumber;
				ArrayList<String> numbers = new ArrayList<String>();

				switch (digits) {
				case 1:
					startNumber = 1;
					break;
				case 2:
					startNumber = 10;
					break;
				case 3:
					startNumber = 100;
					break;
				default:
					startNumber = 1000;
					break;
				}

				for (; startNumber <= 9999; startNumber++) {
					if (checkAsc.isChecked())
						if (!isNumberAscending(startNumber))
							continue;
 
					if (getTotal(startNumber) == total) {
						numbers.add(String.valueOf(startNumber));
					}
				} // for
				
				Message msg = numberHandler.obtainMessage();
				msg.obj = numbers;
				numberHandler.sendMessage(msg);
  		        pd.dismiss();
			}
		}
		
		
  	    NumbersThread thread = new NumbersThread();
	    thread.start();

	} // getNumbers()

	private int getTotal(int number) {
		int sum;
		while (true) {
			sum = 0;
			while (number > 0) {
				int digit = number % 10;
				sum += digit;
				number /= 10;
			}
			if (sum >= 10)
				number = sum;
			else
				break;
		}
		return sum;
	}

	private boolean isNumberAscending(int number) {
		int digit, previousDigit = 9;

		while (number > 0) {
			digit = number % 10;
			number /= 10;
			if (digit > previousDigit)
				return false;
			previousDigit = digit;
		}
		return true;
	}
}

Layout - activity_vehicle_number_finder.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    
    <TableLayout android:layout_width="fill_parent"
       android:layout_height="wrap_content">
        
        <TableRow >
            <TextView android:text="@string/nodigts" />
            <Spinner  android:id="@+id/spinnerDigits"  android:entries="@array/digits" />
        </TableRow>
        
        <TableRow >
            <TextView android:text="@string/total" />
            <Spinner  android:id="@+id/spinnerTotal" android:entries="@array/total" />
        </TableRow>
     </TableLayout>
     
     <TableLayout android:layout_width="fill_parent"
       android:layout_height="wrap_content">
       <TableRow>
        <CheckBox android:id="@+id/checkAscending"/>
        <TextView android:text="@string/order" />
       </TableRow>
     </TableLayout>
     
     <Button android:id="@+id/buttonNumber" 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
         android:onClick="getNumbers" 
         android:text="@string/getnumber" />
     
    
     <TextView android:text="@string/numbers" android:id="@+id/textNumbers"
          android:textColor="#ff0000"  android:textStyle="bold"
          android:layout_width="match_parent" android:layout_height="wrap_content" />
     <ScrollView android:layout_width="fill_parent"  android:fillViewport="true"
        android:layout_height="wrap_content"  android:layout_weight="1" >
        <TableLayout android:id="@+id/tableNumbers"  android:layout_width="fill_parent" android:layout_height="match_parent"
              android:stretchColumns="*" >
        </TableLayout>
     </ScrollView>
</LinearLayout>

String resource - res/values/string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Find Vehicle Registration Numbers</string>
    <string name="nodigts">Minimum Number Of Digits </string>
    <string name="total">Total For Digits :</string>
    <string name="order">Should Digits Be In Ascending Order</string>
    <string name="getnumber">Find Numbers</string>
    <string name="numbers">List Of Numbers</string>
    <string-array name="digits">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>

    <string-array name="total">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
        <item>7</item>
        <item>8</item>
        <item>9</item>
    </string-array>
</resources>

Drawable Resoure - res/drawable/cell.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape= "rectangle"  >
        <solid android:color="#000000"/>
        <stroke android:width="1dp"  android:color="#ffffff"/>
</shape>

Here are the steps to download and install this application.

  1. Your Android device must be enabled to install applications from source other than Android Market. For this you have to take the following steps.
    • Go to Settings in your Android device
    • Select Applications
    • Turn on checkbox for Unknown Sources
  2. Go to VehicleNumberFinder.apk from your Android device and to download .APK file into your device. Once it is downloaded, Android can install the application. Once download is completed, you can click on the notification to start installation.
  3. Alternatively you can download .apk file into any system. Then copy .apk file into USB device. Then connect USB device to your Android device. When Android lists the application (.apk) in the file system of USB device, selecting .apk file will install the application in your Android device.
  4. When prompted whether to Open the application after it is installed, select Open and it will start the application.
  5. This application needs Android API Level 8, so it runs on any device with Android 2.2 (Froyo) or above.