Difference between revisions of "First steps"
From MyWiki
| Line 17: | Line 17: | ||
if ( v.equlas(buttonblue)) then do the stuff<br> | if ( v.equlas(buttonblue)) then do the stuff<br> | ||
Toast.makeText(getApplicationcontext(), "asdfadsfa", Toast.LENGTH_SHORT).show();<br> | Toast.makeText(getApplicationcontext(), "asdfadsfa", Toast.LENGTH_SHORT).show();<br> | ||
| + | |||
| + | Below is the code from the coursera site <br> | ||
| + | <source lang="java"> | ||
| + | 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(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </source> | ||
Revision as of 12:03, 5 October 2017
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(); } }