This is the minimal code you need to set up a linear layout. The main attributes are layout_width, layout_height, and orientation.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
// Inner layouts and widgets here.
</LinearLayout>
Inner layouts should not repeat the first two attributes.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
// More inner layouts and widgets here.
</LinearLayout>
See also:
http://developer.android.com/guide/topics/ui/layout/linear.html
This example extends ListActivity and uses a default android.R.layout.simple_list_item_1 layout for each ListView row.
The toast is retrieving the user's selection using items[position]. The code is reading the string value from the array data source which is in synch with the data in the ListView adapter.
If you prefer, the toast can read the selected ListView item directly from the adapter using code such as this:
parent.getItemAtPosition(position).toString()
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
String[] items = { "Albacore", "Anchovy", "Angelfish", "Barracuda",
"Cherubfish", "Dragonfish", "Freshwater eel", "Golden trout",
"Lemon shark", "Moonfish", "Redfin perch" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast toast = Toast.makeText(this, items[position], Toast.LENGTH_LONG);
toast.show();
}
}
Putting your data into an XML resource is better than hard-coding your data using Java code. You can localize your data by creating several XML resource files.
Using an XML resource is very similar to the previous example: Java code array as data source
The two main differences are that you need to 1) Create your XML resource file and 2) call the method getResources().getStringArray() to pull the data from your XML string array and assign it to a Java array.
To create an XML string array resource:
Create a new Android XML file under res/values. It is important to note that the name attribute is the name of the array - not the file name.
Note:
If you want, you can create the ArrayAdapter using a convenience factory method instead of the new operator.
public static ArrayAdapter<CharSequence> createFromResource (Context context, int textArrayResId, int textViewResId);
Parameters
context The application's environment.
textArrayResId The identifier of the array to use as the data source.
textViewResId The identifier of the layout used to create views.
You can use the R file position of the XML array without using getResources() to create an array from the XML data in your code. You can read the selected ListView item directly from the adapter using code such as this:
parent.getItemAtPosition(position).toString()
In my opinion, it is easier to work with the XML data if you have it in an array as is demonstrated in the code below. For example, the Toast uses the array position instead of calling this method:
parent.getItemAtPosition(position).toString()
Here is an example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="FishTypes">
<item >Albacore</item>
<item >Anchovy</item>
<item >Angelfish</item>
<item >Barracuda</item>
<item >Cherubfish</item>
<item >Dragonfish</item>
<item >Freshwater eel</item>
<item >Golden trout</item>
<item >Lemon shark</item>
<item >Moonfish</item>
<item >Redfin perch</item>
</string-array>
</resources>
The following is an example of a ListActivity that displays in a ListView UI widget data stored in an XML resource file.
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
String[] fish;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fish = getResources().getStringArray(R.array.FishTypes);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,fish));
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast toast = Toast.makeText(this, fish[position], Toast.LENGTH_LONG);
toast.show();
}
}