Android Programming Tutorials

(Romina) #1
Look Inside Yourself

public void onCreate(Bundle icicle) {
super.onCreate(icicle);

if (CONTENT_URI==null) {
Toast
.makeText(this, "We are experiencing technical difficulties...",
Toast.LENGTH_LONG)
.show();
finish();

return;
}

setContentView(R.layout.main);

Button btn=(Button)findViewById(R.id.pick);

btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i=new Intent(Intent.ACTION_PICK, CONTENT_URI);

startActivityForResult(i, PICK_REQUEST);
}
});
}

We first check to ensure our derived CONTENT_URI value is found; if not, we


shut down the application, since there is nothing we can do. Otherwise, we


load the layout, get the Button, and have it startActivityForResult() on the


ACTION_PICK Intent on a button click.


We also need an implementation of PICK_REQUEST:


private static final int PICK_REQUEST= 1337 ;

Step #5: Populate the List


We are not doing anything presently after the user makes their selection.


What we want to do is display a roster of possible actions in the ListView in


our layout.


First, create a layout to use with our rows, as Contacter/res/layout/row.xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

308
Free download pdf