BAB 5
Android Event Handling
Mahardeka Tri Ananta
deka.kelas@gmail.com
Konten
• Event pada Widget
• Event Handler
• Event Listener
Event
• Event adalah suatu cara untuk mengumpulkan data tentang interaksi pengguna dengan komponen interaktif dari Aplikasi.
• Contoh: Aksi (action) yang terjadi ketika user berinteraksi dengan widget (clicks, typing, scrolling)
Setting an Event Listener (SoftCoding)
1. Interacting with widgets /views
• in Layout, give that view a unique ID property value
• in Java code, call findViewById to access its View object
• pass it a parameter of R.id.your_unique_ID
• cast the returned value to the appropriate type (Button, TextView, etc.)
• In Layout XML file:
<TextView android:id="@+id/textViewName"/>
• In Activity onCreate() method:
TextView nameView = (TextView)findViewById(R.id.textViewName);
Setting an Event Listener (SoftCoding)
2. Modify onClick property in layout file.
• Scroll down its Properties until you find onClick. or edit .XML file manually via text.
• Type the name of a method you'll write to handle the click event
Setting an Event Listener (SoftCoding)
3. Add method in onclick Event in JavaCode
• In your Activity, create method that have been registered in layout.
• Add View object in method parameter.
<LinearLayout 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:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call 0888111222"
android:id="@+id/button"
android:layout_gravity="center"
android:onClick="button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:id="@+id/button2"
android:layout_gravity="center"
android:onClick="button2" />
</LinearLayout>
activity_main.xml
public class MainActivity extends AppCompatActivity { private Button button1, button2;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button);
button2=(Button)findViewById(R.id.button2);
}
public void button1(View view){
Intent dial = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0888111222"));
startActivity(dial);
}
public void button2 (View view){
Toast.makeText(this,"you have pressed: " +button2.getText(), Toast.LENGTH_LONG).show();
} }
MainActivity.java
Setting an Event Listener (Hard Coding)
• An event is handled by an event listener object.
• Define an object that implements event listener interface and register it with the designated View.
• View.OnClickListener (for handling "clicks" on a View),
• View.OnTouchListener (for handling screen touch events),
• View.OnKeyListener (for handling device key presses).
• etc..
Setting an Event Listener (Hard Coding)
Illustration
View Widget Button
Event Handler Object
setOnClickListener() onClick() User Click
Event
View.OnClickListener
Setting an Event Listener (Hard Coding)
• Step 1: Add button to Activity
• Activity.findViewById( buttonViewID )
• Step 2: Implement Event Handler…
• for a Button click event means the handler object should implements the View.OnClickListener interface consists of TWO OPTIONS in the following:
1. using a separate (specific) object to handle event(s) that implements the interface, OR
2. have the Activity containing the button do the event handling and letting the Activity implements the interface
• Step 3: Register Event Handler to the button
• button.setOnClickListener( clickEventHandlerObject )
Setting an Event Listener (Hard Coding) – Option 1
• EVENT HANDLING CODE in separate object named mCorkyListener
// Create an anonymous implementation of OnClickListener STEP 2 private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked }
};
//Now inside your Activity class onCreate event method protected void onCreate(Bundle savedValues) {
...
// STEP 1: Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// STEP 3: Register the onClick listener // with the implementation above
button.setOnClickListener(mCorkyListener);
Setting an Event Listener (Hard Coding) - Option 2
Here's the code to handle Button's click event using the Activity itself
public class ExampleActivity extends Activity implements OnClickListener { protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky); // STEP 1 button.setOnClickListener(this); //STEP 2 – registration }
// Implement the OnClickListener callback method // STEP 3 – event handler
public void onClick(View v) {
// do something when the button is clicked }...
}
activity_main.xml
<LinearLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Penjumlahan 2 bilangan"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="masukkan angka ke-1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="masukkan angka ke-2"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_gravity="center"
android:text="hasil: "
android:editable="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hitung"
android:id="@+id/button"
android:layout_gravity="center"/>
</LinearLayout>
activity_main.xml
public class MainActivity extends AppCompatActivity { private EditText txtAngka1, txtAngka2, txtHasil;
private Button buttonHitung;
private View.OnClickListener click;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtAngka1= (EditText)findViewById(R.id.editText);
txtAngka2= (EditText)findViewById(R.id.editText2);
txtHasil = (EditText)findViewById(R.id.editText3);
buttonHitung = (Button)findViewById(R.id.button);
click = new View.OnClickListener() {
@Override
public void onClick(View v) { try {
int angka1 = Integer.parseInt(txtAngka1.getText().toString());
int angka2 = Integer.parseInt(txtAngka2.getText().toString());
int jumlah = angka1 + angka2;
txtHasil.setText(String.valueOf(jumlah));
}
catch (Exception e){
e.printStackTrace();
} } };
buttonHitung.setOnClickListener(click);
MainActivity.java
STEP 1
// Create an anonymous implementation of OnClickListener STEP 2
// STEP 3: Register the onClick listener
TUGAS KELOMPOK
• Buat sebuah aplikasi Android pada platform Google Android yang teridiri dari hanya 1
Activity, dengan menggunakan 4 event handler yang ada di bawah ini:
1. onClick()
2. onLongClick() 3. onFocusChange() 4. onKey()
5. onTouch()
6. onCreateContextMenu()
• Jalankan aplikasi, kemudian amati perbedaan dari 4 event handler yang digunakan
• Buat dokumentasi (.pdf) yang berisi:
1. Langkah2 implementasi Event Handling pada aplikasi yang Anda buat (dilengkapi dengan screenshot)
2. Jelaskan perbedaan dari 6 event handler yang ada di atas, dan berikan contoh kasus penggunaannya
TUGAS KELOMPOK
• Nama File: PAPB-G-T3-Kelompok99
• Deadline: 6 OKTOBER 2015
• Dikumpulkan secara kolektif di koordinator kelas.
KUIS 1 PAPB 13 OKTOBER 2016 closed book (Tulis tangan [Teori & Code Completion])
Materi:
1. Perkembangan Aplikasi Mobile 2. Android Activities
3. User Interface 4. Event Handling