Thursday, June 23, 2011

“Debug certificate expired” Error in Eclipse

When there is no error in the code, but for some reason the project says there is an error and prevents you from running...

When you've torn your hair out trying to delete and re-import your project files to no avail...

When you've "cleaned" and "fixed project properties" so many times your index fingertip is bleeding...

Try looking at your project problems ( Window > Show View > Problems )

You might get something that looks like this:
Debug Certificate Expired...

If the above has happened to you, then I have a fix for you! You can probably solve this problem by deleting the debug certificate file. (Eclipse will then build a new one, and the error will go away.)

Win 7 OS: Go to ( Window > Preferences > Android > Build ) and find out where the debug.keystore file is located on your hard drive. Then using file explorer, find it and destroy it.

Thats it. Import your projects or whatever, do a clean and the error should disappear.

References:
http://stackoverflow.com/questions/2194808/debug-certificate-expired-error-in-eclipse-android-plugins

Wednesday, June 8, 2011

So you need to change the text size of simple_list_item_single_choice...

I feel you.

I tried for a month straight to call the checkedtextview in the android standard layout and resize the text. It never let me. I felt like a idiot. And now that I've figured it out, I know that it is more than just a feeling.

And so I'm posting it here, in the event that I actually forget how to do this later on. Because.. you know... I'm an idiot.

I went and copied the xml from the api demo and pasted it into a new .xml file(step 1). I accessed the text size and adjusted it thusly (step 2). Then I changed the java code to replace the simple_list_item_single_choice with my own custom row layout (step 3). Facepalmed for about two hours (step 4).

my_listitem_singlechoice.xml (i just chaged "textAppearance" attribute but I'm sure you can do other stuff to it too) :


<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
/>



my_java_with_singlechoice.java (this should look somewhat familiar) :


        ListView lv = this.getListView();
MyListAdapter = new ArrayAdapter<String>(this, R.layout.my_listitem_singlechoice, myArrayList);
lv.setAdapter(MyListAdapter );
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Wednesday, April 27, 2011

Getting Your Big Ass Database onto Your Android Phone

Yeah, I effing did it!!!!!!!!!! (Sorry, still pumped from finally getting this to work.)

First off: giving credit where credit is due. Many many many thanks to my sources:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

http://www.mail-archive.com/android-developers@googlegroups.com/msg28194.html

http://www.chriskopec.com/blog/2010/mar/13/deploying-android-apps-with-large-databases/


My project requires a preformed database (SQLite), which in total comes out to a whopping 17mb. Ridiculous I know, but my boss wants it all packaged with the app, and not just to be available online. Hypothetically, getting this to work should be as easy as creating a blank db and then copying the existing one over it from assets or raw. And this totally works-- if your db is smaller than 1mb. So I went and got some free file splitting software (http://www.dekabyte.com/filesplitter/) and split my db file into 1mb pieces. I put the pieces in the raw directory.

I created the DataBaseHelper class and made the necessary tweaks:


public class DataBaseHelper extends SQLiteOpenHelper {


// The Android's default system path of your application database.
private static final String DB_PATH = "/data/data/com.instacare/databases/";


private static final String DB_NAME = "MDAtHand";


private SQLiteDatabase myDataBase;


private final Context myContext;
Resources resources;


/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.

* @param context
*/
public DataBaseHelper(Context context) {


super(context, DB_NAME, null, 1);
this.myContext = context;


// super(context, DB_NAME, null, 1);
resources = context.getResources();
}


/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {


boolean dbExist = checkDataBase();


if (dbExist) {
// do nothing - database already exist


copyDataBase();


} else {


// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();


try {


copyDataBase();


} catch (IOException e) {


throw new Error("Error copying database");


}
}


}


/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.

* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {


SQLiteDatabase checkDB = null;


try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);


} catch (SQLiteException e) {


// database does't exist yet.


}


if (checkDB != null) {


checkDB.close();


}


return checkDB != null ? true : false;
}


/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException {


// Open your local db as the input stream
// InputStream myInput = myContext.getAssets().open(DB_NAME);


// Path to the just created empty db
// String outFileName = DB_PATH + DB_NAME;


// Open the empty db as the output stream
// OutputStream myOutput = new FileOutputStream(outFileName);


// transfer bytes from the inputfile to the outputfile
// byte[] buffer = new byte[1024];
// int length;
// while ((length = myInput.read(buffer))>0){
// myOutput.write(buffer, 0, length);
// }


// Close the streams
// myOutput.flush();
// myOutput.close();
// myInput.close();


InputStream databaseInput = null;
// String outFileName = DB_PATH + DbConsts.DATABASE_NAME;
String outFileName = DB_PATH + DB_NAME;
OutputStream databaseOutput = new FileOutputStream(outFileName);


byte[] buffer = new byte[1024];
int length;


databaseInput = resources.openRawResource(R.raw.datafile01);
while ((length = databaseInput.read(buffer)) > 0) {
databaseOutput.write(buffer, 0, length);
databaseOutput.flush();
}
databaseInput.close();
databaseInput = resources.openRawResource(R.raw.datafile02);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile03);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();


   databaseInput = resources.openRawResource(R.raw.datafile04);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile05);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile06);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile07);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile08);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile09);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile10);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile11);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile12);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile13);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile14);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile15);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile16);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
   databaseInput = resources.openRawResource(R.raw.datafile17);
   while((length = databaseInput.read(buffer)) > 0) {
       databaseOutput.write(buffer, 0, length);
       databaseOutput.flush();
   }
   databaseInput.close();
databaseOutput.flush();
databaseOutput.close();


}


public void openDataBase() throws SQLException {


// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);


}


@Override
public synchronized void close() {


if (myDataBase != null)
myDataBase.close();


super.close();


}


@Override
public void onCreate(SQLiteDatabase db) {


}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}


// Add your public helper methods to access and get content from the
// database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd
// be easy
// to you to create adapters for your views.


}

Then I added this awesomeness to the main.java file


DataBaseHelper myDbHelper = new DataBaseHelper(this);
        //myDbHelper = new DataBaseHelper(this);

        try {

         myDbHelper.createDataBase();

  } catch (IOException ioe) {

  throw new Error("Unable to create database");

  }

  try {

  myDbHelper.openDataBase();

  }catch(SQLException sqle){

  throw sqle;

  }


Thats it :)

Wednesday, February 9, 2011

Deleting Single Choice Listview Items

This post assumes you know how to create a single choice listview and a button in xml.

1) First, define these three variables:


     public static List<String> strings;
     public static ListAdapter adapter;
     ListView lv;

2) Next, set up the listview in onCreate:


     strings = new ArrayList<String>();
     lv = getListView();
     adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, strings);
     lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     lv.setAdapter(adapter);



3) Last, set up the delete button:


     Button btnRemove = (Button) findViewById(R.id.ButtonEditLabOrderForm_Remove);
     btnRemove.setOnClickListener(new View.OnClickListener() {
          @SuppressWarnings("unchecked")
          public void onClick(View v) {
               // Code below only works with ONE CHECKED ITEM at a time
               int len = lv.getCount();
SparseBooleanArray checked = lv.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
String item = strings.get(i);
// do whatever you want with the checked item
strings.remove(item);
((ArrayAdapter<String>) adapter).notifyDataSetChanged();
}
   }
     });

Thats it !

Still trying to figure out how to delete multiple items in a multiple choice list :(

Tuesday, February 1, 2011

Button with Image and Text

This all goes down in the XML file with the button in question. This can also be accomplished dynamically (not shown.) Here is the excerpt from the xml file defining the button. The image should be located in the drawable folder. The image is called from android:drawable. Use drawableLeft, drawableRight, drawableTop, drawableBottom depending on how you want to place the image in relation to the text. Adjust the padding to suit your aesthetic.



<Button
    android:layout_width="wrap_content"
    android:text="Sample Text"
    android:id="@+id/Button01"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/search"
    android:drawablePadding="0px"
    android:padding="0px">
</Button>

Friday, January 28, 2011

Custom Spinner Row Layout & Custom Spinner Adapter

The custom spinner adapter is similar to the custom list row & adapter, because we can use LayoutInflater.

(*edit: This code may not run as-is. Therefore, I have included project files associated with this post available for download at the end of this post.)

1) The spinner layout. This is how each row will look when the spinner is selected. In my example there are three text views as three rows.

<LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" android:orientation="vertical">
      <TextView
          android:layout_height="wrap_content"
          android:id="@+id/TextView01"
          android:textSize="12px"
          android:layout_width="match_parent"
          android:textColor="#000000">
      </TextView>
      <TextView
          android:layout_height="wrap_content"
          android:id="@+id/TextView02"
          android:layout_width="match_parent"
          android:textSize="12px" android:textColor="#000000"></TextView>
      <TextView
          android:layout_height="wrap_content"
          android:id="@+id/TextView03"
          android:layout_width="match_parent"
          android:textSize="12px"
          android:textColor="#000000">
      </TextView>
</LinearLayout>




2) The custom adapter

// CUSTOM SPINNER ADAPTER
public class MyCustomSpinnerAdapter extends ArrayAdapter<String> {

public MyCustomSpinnerAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView,ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);

LayoutInflater inflater = getLayoutInflater();

ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.customspinneritem, null);
holder = new ViewHolder();
holder.txt01 = (TextView) convertView.findViewById(R.id.TextView01);
holder.txt02 = (TextView) convertView.findViewById(R.id.TextView02);
holder.txt03 = (TextView) convertView.findViewById(R.id.TextView03);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();
}

holder.txt01.setText("Route")
holder.txt02.setText("Strength");
holder.txt03.setText("Form");

return convertView;
}

class ViewHolder {
TextView txt01;
TextView txt02;
TextView txt03;
}

} // end custom adapter


3) Calling the spinner & setting the adapter

Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setPrompt("Select One");
spinner.setAdapter(new MyCustomSpinnerAdapter(this, 0, items));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}

@Override
>public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});


4) Setting string items

String[] items = { "Route 1, Strength 1, Form 1", "Route 2, Strength 2, Form 2", "Route 3, Strength 3, Form 3" };

Downloads:
main.xml
customspinneritem.xml
CustomSpinnerActivity.java (select "qui")

Resources:
My previous Spinner post
Android Dev Tutorial - Hello Spinner
Custom List Row Layout w/ Adapter

Simple Spinner w/ Array Adapter

Very basic stuff here, people. If you haven't yet, get acquainted with android spinner using the dev tutorial.


1) Calling the spinner, defining the array adapter.

       Spinner spinner= (Spinner) findViewById(R.id.spinner);
       ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
       this, android.R.layout.simple_spinner_item, items);
       spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinner.setPrompt("Select One");
       spinner.setAdapter(spinnerArrayAdapter );
spnRoute.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// your code here
}


@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

2) The string array


String[] items = { "lorem", "ipsum", "dolor", "sit", "amet"};