Button button1 = findViewById(R.id.btn_1);Each of the code fragments a, b, c, and d adds an event listener to a button. Match these code fragments to the methods:
// a.
button1.setOnClickListener(new View.OnClickListener( ) {
public void onClick(View view) {
// body of click event listener
}
}
// b.
// In the layout file activity_main.xml:
android:onClick="onClick"
// In the main activity file MainActivity.java:
public void onClick(View view) {
// body of click event listener
}
// c.
button1.setOnClickListener(new MyListener( ));
// The inner MyListener class is defined in the MainActivity
// class, but outside of the onCreate method like this:
private class MyListener implements View.OnClickListener {
public void onClick(View view) {
// body of click event listener
}
}
// d.
button1.setOnClickListener( view -> {
// body of click event listener
});
<View
layout_width="match_parent"
layout_height="25dp" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In nibh lectus, accumsan at ligula sagittis, bibendum suscipit enim.When the textview widget is clicked, concatenate 100 copies of this paragraph together and display the new repeated text in the textview. Add scrollbars so the user can see the entire textview.
public class RollDice {
public static ArrayList<Integer> roll(int numDiceFaces) {
ArrayList<Integer> rolls = new ArrayList<Integer>( );
Random r = new Random( );
int die1 = r.nextInt(numDieFaces) + 1;
int die2 = r.nextInt(numDieFaces) + 1;
rolls.add(die1);
rolls.add(die2);
return rolls;
}
}
Use android:orientation="horizontal" for the linear layout.
Toast t = Toast.makeText(getApplicationContext( ),
"Testing the toast.", Toast.LENGTH_LONG);
t.show( );
Toast.makeText(getApplicationContext( ),
"Testing the toast.", Toast.LENGTH_LONG).show( );
| Event | Event Handler | Description | Required |
|---|---|---|---|
| ON_CREATE | onCreate | Fires immediately after the activity is first created. | Yes |
| ON_START | onStart | Fires immediately after the activity becomes visible. It prepares the activity to move into the foreground and become interactive. | No |
| ON_RESUME | onResume | Fires when the activity moves into the foreground after going into the background. | No |
| ON_RESTART | onRestart | Fires when a covered activity moves into the foreground after being stopped, but not destroyed. | No |
| ON_PAUSE | onPause | Fires immediately before the activity moves into the background. It may still be visible. | No |
| ON_STOP | onStop | Fires when the activity has finished running and is no longer visible to the user, but may still be in memory. | No |
| ON_DESTROY | onDestroy | Fires immediately before an app has terminated and is about to be unloaded from memory. | No |
@Override
public void onCreate(...) {
super.onCreate(...);
openGarageDoor( );
unlockCar( );
getInCar( );
closeCarDoor( );
putOnSeatBelt( );
putKeyInIgnition( );
}
@Override
public void onStart( ) {
super.onStart( );
startEngine( );
changeRadioStation( );
switchOnLightsIfNeeded( );
switchOnWipersIfNeeded( );
}
@Override
public void onResume( ) {
super.onResume( );
applyFootbrake( );
releaseHandbrake( );
putCarInGear( );
drive( );
}
@Override
public void onPause( ) {
super.onPause( );
putCarInNeutral( );
applyHandbrake( );
}
@Override
public void onStop( ) {
super.onStop( );
switchOffLights( );
switchOffRadio( );
switchOffHeater( );
turnOffEngine( );
removeSeatBelt( );
getOutOfCar( );
lockCar( );
}
@Override
public void onDestroy( ) {
super.onDestroy( );
goToOfficeBuilding( );
}
@Override
protected void onRestart() {
unlockCar( );
getInCar( );
closeCarDoor( );
putOnSeatBelt( );
putKeyInIgnition( );
}
Reference:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("OO1", "In onCreate method");
}
@Override
public void onStart() {
super.onStart( );
Log.i("OO1", "In onStart method");
}