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; } }
<?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>
<?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>
<?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>