Android Platform Development Introduction
Day 3
I work at Integrity as a Project and Engineering Lead
What is your name?
What do you do?
What kind of programming experience do you have?
What is your favorite app?
By adding an "onClick" handler to our UI we can now programmatically trigger a function whenever a use clicks a button
public void buttonClicked(View view) {
Log.i("LOG","Button was clicked!");
TextView output = (TextView) findViewById(R.id.textView1);
output.setText("You Pressed the button!");
}
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/textView1"
android:layout_below="@id/textView1"
android:src="@drawable/abc_ic_go_search_api_mtrl_alpha"
android:onClick="buttonClicked" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:id="@+id/textView" />
<EditText
android:id="@+id/editText1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView"
android:hint="Enter name here..." />
Setup Exercise 1 to collect a users information and print it to the screen when they hit the "Save" button
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="checkboxClicked"
android:text="Check here"
/>
public void checkboxClicked(View view) {
CheckBox cb = (CheckBox) view;
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Checkbox is " + (cb.isChecked() ? "checked" : "not checked"));
}
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Yes"
android:textOff="No"
/>
public void toggleboxClicked(View view) {
ToggleButton tb = (ToggleButton) view;
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("toggle is " + (tb.isChecked() ? "checked" : "not checked"));
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Which are better, Pirates or Ninjas?"
/>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="favClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="favClicked"/>
</RadioGroup>
public void favClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.radio_pirates:
if (checked)
Log.i("TEST", "Pirates are the best");
break;
case R.id.radio_ninjas:
if (checked)
Log.i("TEST", "Ninjas are the best");
break;
}
}