Lecture 3
Lecture 3
Android Applications
Development - Events
CSC445, Modern Programming Languages, Dr.
Mubeen Ghafoor, COMSATS
‘OnClick’ Event
• Example:
onClick(View v):
Called when a view has been clicked.
setOnClickListener():
Register a callback to be invoked when this view is clicked.
Button
A clickable widget with a text label
● key attributes:
android:clickable="bool" set to false to disable the button
android:id="@+id/theID" unique ID for use in Java code
android:onClick="function" function to call in activity when clicked
(must be public, void, and take a View arg)
android:text="text" text to put in the button
@Override
protected void onCreate(Bundle savedInstanceState) {
.
.
myTextView = (TextView) findViewById(R.id.textView1);
myButton1 = (Button) findViewById(R.id.button1);
myButton1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == myButton1.getId()){
myTextView.setText("Button 1 Clicked");
}
}
ImageButton
A clickable widget with an image label
● key attributes:
android:clickable="bool" set to false to disable the button
android:id="@+id/theID" unique ID for use in Java code
android:onClick="function" function to call in activity when clicked
(must be public, void, and take a View arg)
android:src="@drawable/img" image to put in the button
(must correspond to an image resource)
● key attributes:
●
to change the visible image, in Java code:
● key attributes:
android:hint="text" gray text to show before user starts to type
android:id="@+id/theID" unique ID for use in Java code
android:inputType="type" what kind of input is being typed;
number,phone,date,time,...
android:lines="int" number of visible lines (rows) of input
android:maxLines="int" max lines to allow user to type in the box
android:text="text" initial text to put in box (default empty)
android:textSize="size" size of font to use (e.g. "20dp")
● key attributes:
android:checked="bool" set to true to make it initially checked
android:clickable="bool" set to false to disable the checkbox
android:id="@+id/theID" unique ID for use in Java code
android:onClick="function" function to call in activity when clicked
(must be public, void, and take a View arg)
android:text="text" text to put next to the checkbox
● In Java code:
CheckBox cb = (CheckBox) findViewById(R.id.theID);
cb.toggle(); //Change the checked state of the view to the
//inverse of its current state
● key attributes:
android:checked="bool" set to true to make it initially checked
android:clickable="bool" set to false to disable the button
android:id="@+id/theID" unique ID for use in Java code
android:onClick="function" function to call in activity when clicked
(must be public, void, and take a View arg)
android:text="text" text to put next to the button
myRadioBtn1 = (RadioButton)findViewById(R.id.radioButton1);
myRadioBtn2 = (RadioButton)findViewById(R.id.radioButton2);
myRadioBtn1.setOnClickListener(this);
myRadioBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == myRadioBtn1.getId()){
myTextView.setText("Radio Button 1 Clicked"); }
else if(v.getId() == myRadioBtn2.getId()){
myTextView.setText("Radio Button 2 Clicked"); }
}
Spinner
A drop-down menu of selectable choices
● key attributes:
android:clickable="bool" set to false to disable the spinner
android:id="@+id/theID" unique ID for use in Java code
android:entries="@array/array" set of options to appear in spinner
(must match an array in strings.xml)
android:prompt="@string/text" title text when dialog of choices pops up
<string-array name="arrayname">
<item>value</item>
<item>value</item>
<item>value</item> <!-- must escape ' as \' in values -->
...
<item>value</item>
</string-array>
</resources>
●
Spinner example
<LinearLayout ...>
<Spinner ... android:id="@+id/tmnt"
android:entries="@array/turtles"
/>
<TextView ... android:id="@+id/result" />
</LinearLayout>
● in res/values/strings.xml:
<resources>
<string-array name="turtles">
<item>Leonardo</item>
<item>Michelangelo</item>
<item>Donatello</item>
<item>Raphael</item>
</string-array>
</resources>
Spinner event example
public class MainActivity extends ActionBarActivity implements OnClickListener,
OnItemSelectedListener{
.
.
Spinner myspinner = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
.
.
myspinner = (Spinner)findViewById(R.id.spinner1);
myspinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
ScrollView
A container with scrollbars around
another widget or container
<LinearLayout ...>
...
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView ... android:id="@+id/turtle_info" />
</ScrollView>
</LinearLayout>