Monthly Archives: April 2015

App Project File Locations

When you begin developing apps there are 3 main locations you will be working with.
Screen Shot 2015-06-13 at 7.39.37 AM
I use Android Console, notice in the top left corner in the example above I am using the Android layout. When using this layout the files you would be working with are the AndroidManifest.xml, MainActivity, and activity_main.xml

MainActivity – where your logic code goes, where you spend most of your time.

activity_main.xml – where your screen layout code goes/

AndroidManifest.xml – which is where you define special permissions your app needs to work (reading from storage, accessing the internet) and if your app will display more than one layout, you must also define them here.

Another view that I find useful some times is the Project View:

Screen Shot 2015-06-13 at 7.55.14 AM

Sometimes when working with API’s or 3rd party libraries they will ask you to create folders or place files in the libs directory, and this is where you would find it.

Other files you may also use:

strings.xml file – which you should use to place Label and Button text etc. so that if your app ever goes global you can have these words translated into another languages much easier and then you will have one .xml file that supports several languages instead of creating a .xml files for each language.

Bonus file locations:

App_name/App/src/main/res/drawable-xxxxx is where you place your icons for different screen resolutions.

App_name/App/src/main/res/xml – is where you put special configuration files.

App_name/App/src/main/res/raw – is where you can put large files such as .mp3’s and .wav’s

App_name/App/src/main/res/menu – is where you put menu layout’s that look very similar to regular layouts.

Android – ListActivity

There are also subclasses that override functionality of the parent class to perform specific functions for you.

For instance the ListActivity. This subclass of the Activity Class does a lot of work behind the scenes to give you a list view to work with.

Take the code I provided in the MainActivity example (which is actually just a default app that you will get when you create one anyway) and extend a ListActivity instead of a Activity, fix your imports which replaces

import android.app.Activity;
with
import android.app.ListActivity;

at the top of your code.

Change

public class MainActivity extends Activity {
to
public class MainActivity extends ListActivity {

and remark out the setContentView() method like this:

//setContentView(R.layout.main);

because it will supply one for us and run the code. You will now have a list view, even though it has no items in the list to display. Go ahead and take a look at your main.xml or activity_main.xml layout in the App_name/res/layout folder and you will see nothing has changed with the layout and no other .xml files were added to the directory because we are using the ListActivity Class and it does all the work for us. As you will see later when we create a list the manual way this hides a lot of work behind the scenes.

Now let’s add some items to an array so we can show you that there is really a list view.

Above the onCreate method, but below the beginning of the MainActivity definition add the following:

String[] listItems = {"Item 1","Item 2","Item 3","Item 4"};

And in the onCreate() method below super.onCreate():

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems));

This set’s the list adapter(which is created automatically for you, but you will have to point to it yourself when we do it manually).

The ListAdapter is used to populate each “cell” or row of your list you want to display. In other words it takes the first item from the listItems array and puts it in the text location in the first “cell” or row of the list where text goes (which you can’t see because it is hidden in the android.R.layout.simple_list_item_1 layout file which you can’t see) then it gets the second item from listItems and puts it in the second “cell” or row, and so on and so on until it gets to the end of the listItems array.

“this” is context which you pass on so the ListAdapter has access to the screen to display the list. (see context for more info).

You can put all kinds of objects in arrays and you can even put different kinds of objects in the same array, but that is just asking for trouble so by putting

ArrayAdapter<String>

you are telling Android to warn you (not crash) if you try to put something other than a string into the array.

String[] - is how you define an array of Strings.

Full Code:

package com.damageinc.bigdogweather;

import android.os.Bundle;
import android.app.ListActivity;

public class MainActivity extends ListActivity {

String[] listItems = {"Item 1","Item 2","Item 3","Item 4"};

@Override
    public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems));
}
}

Now let’s see what Android is hiding from us:

You can change

import android.app.ListActivity;
back to
import android.app.Activity;

Change

public class MainActivity extends ListActivity {
back to
public class MainActivity extends Activity {

and un-remark

setContentView(R.layout.main);

leave everything else the same for now.

Now change your App_name/res/layout/main.xml or activity_main.xml file

<?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" >

    <ListView
        android:id"@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Now back to MainActivity to add some code

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, listItems);
        ListView myListview = (ListView)findViewById(R.id.listview);
        myListview.setAdapter(adapter);

Since we are doing it ourselves we have to add one more argument to our ArrayAdapter definition

android.R.id.text1

this is where each item in listItems is placed in each “cell” or row before it is written to the screen.

Each “cell” or row basically looks like the default layout when you first create your app

<?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="wrap_content" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

The only thing that would be different is the layout height would be wrap_content and the TextView width would be fill_parent and it would have an id of text1.

After adding those three lines you can run the code and you should get the same result.

If you want to look extra smart you can combine the three lines into two.

        ListView myListview = (ListView)findViewById(R.id.listview);
        myListview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, listItems));

The next step is creating your own custom “cell” or row, if you want to display a picture for instance. You use the same code as above, but you replace

android.R.layout.simple_list_item_1

There are many ways to create adapters.

For lists of items you can extend ArrayAdapter or BaseAdapter.

For database items you can use CursorAdapter or SimpleCursorAdapter.