First steps
From MyWiki
Simple first program
1. choose empty activity
2. we will edit activity_main.xml and MainActivity.java
3. use api level 16
4. Get rid of "hello world"
5. Drag button
6 width = match parent, change background colour
7 changethe id of the button and also the text
8 onclick property, speccify the name of method
9. public void toDo(View v){
}
in oncreate
Button buttonblue;
buttonBlue = (Button) findViewById(R.id.button_blueinvisible);
define button in main activity
if ( v.equlas(buttonblue)) then do the stuff
Toast.makeText(getApplicationcontext(), "asdfadsfa", Toast.LENGTH_SHORT).show();
Below is the code from the coursera site
package fr.centralesupelec.galtier.block01; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { // the 2 buttons defined in the activity_main.xml file Button buttonBlue, buttonPink; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // display the GUI defined in the activity_main.xml file setContentView(R.layout.activity_main); // retrieve references to the Views defined in the activity_main.xml buttonBlue = (Button) findViewById(R.id.button_blueInvisible); buttonPink = (Button) findViewById(R.id.button_pinkPanther); } /** * called when a button from activity_main.xml is clicked * @param v the View which triggered the method call: should refer to buttonBlue or buttonPink */ public void toDo(View v) { if (v.equals(buttonBlue)) // buttonBlue was clicked // turn the button invisible v.setVisibility(View.INVISIBLE); if (v.equals(buttonPink)) // buttonPink was clicked // display a pop-up message for a short duration Toast.makeText(getApplicationContext(), "to do to do to do...", Toast.LENGTH_SHORT).show(); } }
Below is the XML code
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="fr.centralesupelec.galtier.block01.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="The Invisible Man's To Do List" android:id="@+id/button_blueInvisible" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="#AED2FF" android:onClick="toDo" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="The Pink Panther's To Do List" android:id="@+id/button_pinkPanther" android:layout_below="@id/button_blueInvisible" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="#FD9BF3" android:onClick="toDo" /> </RelativeLayout>