// ******************************************************************* // // ** Chapter 4 Code Listings ** // // ** Professional Android 2 Application Development ** // // ** Reto Meier ** // // ** (c)2010 Wrox ** // // ******************************************************************* // // ** Creating Layouts ******************************************** // // ******************************************************************* // ** Listing 4-1: Inflating an Activity layout @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); TextView myTextView = (TextView)findViewById(R.id.myTextView); } // ******************************************************************* // ** Listing 4-2: Creating a UI layout in code @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TextView myTextView = new TextView(this); setContentView(myTextView); myTextView.setText("Hello, Android"); } // ******************************************************************* // ** Listing 4-3: Simple Linear Layout in XML // ******************************************************************* // ** Listing 4-4: Simple LinearLayout in code LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); TextView myTextView = new TextView(this); EditText myEditText = new EditText(this); myTextView.setText("Enter Text Below"); myEditText.setText("Text Goes Here!"); int lHeight = LinearLayout.LayoutParams.FILL_PARENT; int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT; ll.addView(myTextView, new LinearLayout.LayoutParams(lHeight, lWidth)); ll.addView(myEditText, new LinearLayout.LayoutParams(lHeight, lWidth)); setContentView(ll); // ** Extending Views ********************************************* // // ******************************************************************* // ** Listing 4-5: Extending TextView (Extended) public class MyTextView extends TextView { public MyTextView (Context context, AttributeSet ats, int defStyle) { super(context, ats, defStyle); } public MyTextView (Context context) { super(context); } public MyTextView (Context context, AttributeSet attrs) { super(context, attrs); } @Override public void onDraw(Canvas canvas) { [ ... Draw things on the canvas under the text ... ] // Render the text as usual using the TextView base class. super.onDraw(canvas); [ ... Draw things on the canvas over the text ... ] } @Override public boolean onKeyDown(int keyCode, KeyEvent keyEvent) { [ ... Perform some special processing ... ] [ ... based on a particular key press ... ] // Use the existing functionality implemented by // the base class to respond to a key press event. return super.onKeyDown(keyCode, keyEvent); } } // ******************************************************************* // ** Listing 4-6: Creating a compound control public class MyCompoundView extends LinearLayout { public MyCompoundView(Context context) { super(context); } public MyCompoundView(Context context, AttributeSet attrs) { super(context, attrs); } } // ******************************************************************* // ** Listing 4-7: A compound View layout resource