Tuesday, July 5, 2011

Custom Tabs that Scroll Horizontally

So I designed horizontally scrolling tabs that use separate activities to fill the tab content. TI rate this a 1/5 on the fun scale: there are so many different files you have to create and different selection states you have to consider. (Click here for my update to this tutorial: Changing Tab with Horizontal Swipe Gesture)

Faded edges indicate additional content
tabhostlayout.xml - Pretty basic stuff here, if you followed the tab widget tutorial on the android dev site.[1] The only thing to note here is that I surrounded the tab widget with Horizontal Scroll View element to achieve a horizontal scroll for the tabs. (Note: Sorry for the formatting here. I've tried to fix it only to make it much much worse. Blame blogger?)
<TabHost
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@android:id/tabhost"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
<LinearLayout
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<HorizontalScrollView
               android:layout_width="fill_parent"
               android:layout_height="wrap_content">
                <TabWidget
                    android:id="@android:id/tabs"
                   android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
               android:id="@android:id/tabcontent"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:padding="5dp" />
</LinearLayout>
</TabHost>

TabHostActivity.java - This activity must extend TabActivity. You will need to define the following variables and also set the divider drawable BEFORE setting up tabs

TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
Intent intent; // Reusable Intent for each tab

For each tab you would like to add, use the following code in onCreate:
intent = new Intent().setClass(TabHostActivity.this, PageOne.class);
setupTab(new TextView(this), "Tab One", intent);

After onCreate, you need to add two new classes. "setupTab" creates the tabs and tab content. "createTabView" handles all tab events, like setting the views for selection events and stuff like that.
private void setupTab(final View view, final String tag, final Intent myIntent) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(myIntent);
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}

tabs_bg.xml - In the code above, "createTabView" calls a layout called tabs_bg. Create this file in the layout folder. This layout creates the layout for each tab element. I have only added a single textview but an image view could also be added (but will need some adjustments to the java code not shown above). This is where some of the layout personalization comes in. Text size and style, and padding elements. Note the background and text color call drawable files that handle the color changes for selection events.
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/tabsLayout"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@drawable/tab_bg_selector"
     android:padding="3dip"
     android:gravity="center"
     android:orientation="vertical">
<TextView
          android:layout_width="wrap_content"
          android:textColor="@drawable/tab_text_selector"
          android:gravity="center"
          android:textSize="12dip"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:id="@+id/tabsText"
          android:text="Title"
          android:padding="10px">
</TextView>
</LinearLayout>

tab_bg_selector.xml - both bg and text selector drawable files require the same four states to be defined: active, pressed, selected (using arrows), and inactive. I don't really know why the text and bg have to be separate files, but when I tried to combine them, it wasn't displaying properly. This code resembles custom button selection.
<selector
     xmlns:android="http://schemas.android.com/apk/res/android">
     <!--  Active tab -->
   
<item
          android:state_selected="true"
          android:state_focused="false"
          android:state_pressed="false">
          <shape
               android:shape="rectangle">
               <gradient
                    android:startColor="#A8A8A8"
                    android:centerColor="#7F7F7F"
                    android:endColor="#696969"
                    android:angle="-90" />
          </shape>
     </item>
   
<!--  Inactive tab -->
   
<item
          android:state_selected="false"
          android:state_focused="false"
          android:state_pressed="false">
          <shape
               android:shape="rectangle">
               <gradient
                    android:startColor="#5C5C5C"
                    android:centerColor="#424242"
                    android:endColor="#222222"
                    android:angle="-90" />
          </shape>
     </item>
<!--  Pressed tab -->
<item
          android:state_pressed="true"
          android:drawable="@android:color/transparent" />
<!--  Selected tab (using d-pad) -->
 <item
          android:state_selected="true"
          android:state_focused="true"
          android:state_pressed="false"
          android:drawable="@android:color/transparent" />
</selector>

tab_text_selector.xml
<selector
     xmlns:android="http://schemas.android.com/apk/res/android">
     <item
          android:state_selected="true"
          android:color="@android:color/white" />
<item
          android:state_focused="true"
          android:color="@android:color/white" />
<item
          android:state_pressed="true"
          android:color="@android:color/white" />
   
<item
          android:color="#f8f8f8" />
</selector>

References:
1) http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
2)http://joshclemm.com/blog/?p=136
For additional help, click here to download a zip containing all the files needed to create the above project.

Friday, July 1, 2011

Solution to "Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE"

Source: http://stackoverflow.com/questions/4709137/solution-android-install-failed-insufficient-storage-error


Getting this error: 

[2011-07-01 10:36:22 - MDatHand] Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
[2011-07-01 10:36:22 - MDatHand] Please check logcat output for more details.
[2011-07-01 10:36:22 - MDatHand] Launch canceled!


I used this solution:


RIGHT CLICK the root of your Android Project
SELECT "Run As" then go to "Run Configurations..." 
LOCATE the "Android Application" node in the tree at the left then SELECT your project
SELECT "Target" tab on the right side of the window look down for the "Additional Emulator Command Line Options" field (sometimes you'll need to make the window larger) 
PASTE "-partition-size 1024" there
CLICK "Apply" and then Run to use your emulator
Like so.



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

Friday, January 14, 2011

Seriously, this is how I made a multi-column list item populated by strings!!

Untitled Document 1) The item layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/TextView_Title"
android:text="Title"
android:textSize="15px"
android:layout_width="fill_parent"></TextView>

<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:id="@+id/TextView_Item1"
android:text="Item1"
android:layout_width="wrap_content"
android:textSize="10px" android:layout_weight="1"/>
    <TextView
android:layout_height="wrap_content"
android:id="@+id/TextView_Item2"
android:text="Item2"
android:layout_width="wrap_content"
android:textSize="10px" android:layout_weight="1"/>
<TextView
android:layout_height="wrap_content"
android:id="@+id/TextView_Item3"
android:text="Item3"
android:layout_width="wrap_content"
android:textSize="10px" android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>




2) Build the custom adapter:

// CUSTOM TWO ROW LIST ADAPTER
static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}

public int getCount() {
return title.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem_addrx, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.TextView_Title);
holder.txtItem1 = (TextView) convertView.findViewById(R.id.TextView_Item1);
holder.txtItem2 = (TextView) convertView.findViewById(R.id.TextView_Item2);
holder.txtItem3 = (TextView) convertView.findViewById(R.id.TextView_Item3);

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

holder.txtTitle.setText(title.get(position));
holder.txtItem1.setText(one.get(position));
holder.txtItem2.setText(two.get(position));
holder.txtItem3.setText(three.get(position));

return convertView;
}

static class ViewHolder {
TextView txtTitle;
TextView txtItem1;
TextView txtItem2;
TextView txtItem3;
}
} // end custom adapter


3) Call the following items:

static ListView lv;
public static ListAdapter adapter;
String selected;
static final ArrayList<String> title = new ArrayList<String>();
static final ArrayList<String> one = new ArrayList<String>();
static final ArrayList<String> two = new ArrayList<String>();
static final ArrayList<String> three = new ArrayList<String>();


4) Set the list adapter:

lv = (ListView) getListView();
        lv.setAdapter(new EfficientAdapter(this));


5) Add entries to list:

Either define a string array when you call the variables in step 3 or add items dynamically:


title.add(selected);
route.add(selected);
strength.add(selected);
form.add(selected);

lv.setAdapter(new EfficientAdapter(getBaseContext()));



References (& many, many thanks to):

Custom list row & adapter: http://www.androidpeople.com/android-custom-dynamic-listview/


Custom spinner row & adapter: http://android-er.blogspot.com/2010/12/custom-arrayadapter-for-spinner-with.html


Update with my version later...

Custom Button Using XML

1. Create a custombuttonlayout.xml file in the drawable folder


Selector: this element surrounds the entire xml. It defines this drawable as a selecter (e.g. buttons, list rows, or anything that can be "selected")


Items: There are three buttons states that must be defined: pressed, focused, normal. However, there are a total of seven states that can be defined(pressed, focused, selected, checkable, checked, enabled, and window_focused).These are defined as three separate "items". Due to the nature of the way java programs are read (basically from the top down) you need to define items in this specific order, with pressed first, focused next, and normal last to get the buttons to display correctly.


Gradient: Android buttons typically have a highlight/lowlight gradient, so you should define two colors, and the angle of the "shadow".


Stroke: I think this defines the outline style of the button? Not too sure...


Corners: creates a rounded button corner. The default android radius value is "5dp".


Padding: This defines the blank space between the button text and the button borders.


custombuttonlayout.xml



<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:state_pressed="true">
    <shape>
      <gradient
        android:startColor="#FFFF40"
        android:endColor="#FFFFC0"
        android:angle="270" />
      <stroke
        android:width="1dp"
        android:color="#000000" />
      <corners
        android:radius="5dp" />
      <padding
        android:left="10dp"
        android:top="15dp"
        android:right="10dp"
        android:bottom="15dp" />
    </shape>
  </item>
  <item
    android:state_focused="true">
    <shape>
      <gradient
        android:endColor="#8080FF"
        android:startColor="#C0C0C0"
        android:angle="270" />
      <stroke
        android:width="1dp"
        android:color="#4A494A" />
      <corners
        android:radius="5dp" />
      <padding
        android:left="10dp"
        android:top="15dp"
        android:right="10dp"
        android:bottom="15dp" />
    </shape>
  </item>
  <item>
    <shape>
      <gradient
        android:endColor="#8080FF"
        android:startColor="#C0C0C0"
        android:angle="270" />
      <stroke
        android:width="1dp"
        android:color="#000000" />
      <corners
        android:radius="5dp" />
      <padding
        android:left="10dp"
        android:top="15dp"
        android:right="10dp"
        android:bottom="15dp" />
    </shape>
  </item>
</selector>




2. In the main.xml layout when you insert a button that you want to apply the layout to:


Define the button background source as the drawable/custombuttonlayout.xml from above.


excerpt from main.xml:



<Button
          android:layout_width="wrap_content"
          android:text="Sample Text"
          android:id="@+id/Button01"
          android:layout_height="wrap_content"
          android:background="@drawable/custombuttonlayout"></Button>






Resources:


1) http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList


2) http://developer.android.com/guide/topics/resources/color-list-resource.html


3) http://developer.android.com/reference/android/widget/Button.html


4) http://androiddrawableexplorer.appspot.com/


5) http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape