Friday, June 3, 2011

Beginning Android Tutorial #1 (Part 3 of 3)- Adding a Button with an onClick XML Attribute

 
So once again picking up where I left off and I am going to head to the main.xml file and add a Button widget so that it now looks like this.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/display_text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"/>
    
<EditText
    android:id="@+id/entered_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine|textAutoCorrect"/>
    
<Button
    android:id="@+id/submit_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:onClick="submit"/>    
        
</LinearLayout>


     One thing to note here is the XML attribute for the button onClick="submit".  I have found that in declaring this here you can save yourself a few lines of code.  The actual implementation of this will take place within the HelloWorldActivity.java which we have not touched until this point.

     The HelloWorldActivity.java will end up being pretty simplistic in set up as well.  The end goal of the program is to have the user enter text into the EditText widget.  From there the user can then press the Submit button and the text that was entered will then replace the so called title text that was previously there.

     I am going to add about 8 lines of new code to handle what the goal of the program is designed to do.

HelloWorldActivity.java

public class HelloWorldActivity extends Activity {
    
    //Variables
    private EditText newText;
    private TextView displayText;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Finding the widgets by their XML id
        newText = (EditText)findViewById(R.id.entered_text);
        displayText = (TextView)findViewById(R.id.display_text);
               
        
    }//End of onCreate
    
    //this is the implementation of the onClick XML attribute 'submit' 
    public void submit(View target) {
        displayText.setText(newText.getText().toString());
        newText.setText("");
    }//End of submit
    
    
}//End of activity

     I have added some comments to help guide you through what the program is doing. The important things to note are finding the widgets by their XML id attributes and the actual implementation of the 'Submit' onClick event that I declared within the main.xml file.



You can get the source code here:  BasicHelloWorld3.zip


      This will pretty much wrap up this tutorial which was broken down into 3 parts.  This was a beginning point of view with some minor enhancements.  As I progress and topics are addressed I might revisit some of these programs to make modifications and add more functionality.  As usual question, comments and suggestions are always welcomed.

No comments: