13
Handling View Events

Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

Embed Size (px)

Citation preview

Page 1: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

Handling View Events

Page 2: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside
Page 3: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

• Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml".

• The setContentView method inside the onCreate method will bind the "activity_main.xml" layout to the *MainActivity.java*.

Page 4: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

How It Works

• To handle the events fired by each view, you first have to programmatically locate the view that you created during the onCreate() event. • You do so using the findViewById() method (belonging to the Activity base class), supplying it with the ID of the view:

Page 5: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

Botton

//---Button view---Button btnOpen = (Button) findViewById(R.id.btnOpen);The unique id of the botton is accessed as parameter.

• The setOnClickListener() method registers a callback to be invoked later when the view is clicked:

btnOpen.setOnClickListener(new view.OnClickListener() {public void onClick(View v) {DisplayToast(“You have clicked the Open button”);}});

• The onClick() method is called when the view is clicked.

Page 6: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

Check Box

• To determine the state of the CheckBox, you have to typecast the argument of the onClick() method to a CheckBox and then check its isChecked() method to see if it is checked:

CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave);checkBox.setOnClickListener(new OnClickListener(){public void onClick(View v) {if (((CheckBox)v).isChecked())DisplayToast(“CheckBox is checked”);elseDisplayToast(“CheckBox is unchecked”);}});

Page 7: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

Radio Button

• For the RadioButton, you need to use the setOnCheckedChangeListener() method on the RadioGroup to register a callback to be invoked when the checked RadioButton changes in this group://---RadioButton---RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){public void onCheckedChanged(RadioGroup group, int checkedId) {RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1);if (rb1.isChecked()) {DisplayToast(“Option 1 checked!”);} else {DisplayToast(“Option 2 checked!”);}}});

Page 8: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

• When a RadioButton is selected, the onCheckedChanged() method is fired. • Within it, you locate individual RadioButtons and then call their

isChecked() method to determine which RadioButton is selected. • Alternatively, the onCheckedChanged() method contains a second

argument that contains a unique identifier of the RadioButton selected.• The ToggleButton works just like the CheckBox.

Page 9: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

Edit Text

• findViewById(int) access the edit text. EditText edtText=(EditText)findViewById(R.id.editText1);• Use the getText() method to get the text entered by the user. String enteredText=editText1.getText().toString();• Can set listener for various action on EditText.• Example:- To change focus away from view.• The event,listener and handler associated with this

event are• FocusChange,OnfocusChangeListener &

onFocusChange().

Page 10: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

Reminder for various event, listener & handler of views

Event Object Event Listener Event HandlerFocusChange OnFocusChangeListener onFocusChange()CheckedChange OnCheckedChangeListener onCheckedChange()ItemClick OnItemClickLictener onItemClick()

Focus away from view

Checkbox and radio group

List view

Page 11: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

• So far, to handle the events on the views, you first had to get a reference to the view and then register a callback to handle the event. • There is another way to handle view events. Using the Button as an example, you can add an attribute called onClick to it:<Button android:id=”@+id/btnSave”android:layout_width=”fill_parent”android:layout_height=”wrap_content”android:text=”@string/save”android:onClick=”btnSaved_clicked”/>

Page 12: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside

• The onClick attribute specifies the click event of the button. • The value of this attribute is the name of the event handler.

• Therefore, to handle the click event of the button, you simply need to create a method called btnSaved_clicked, as shown in the example.

Page 13: Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside