You are on page 1of 3

LEARNING TASK 7

Legaspi, Mark Anthony BSIT-3A


Reminder. Read Module 7: Event Handling in Android Studio first, before answering this
section.

Instruction: Answer the following questions briefly, your


answer will be evaluated using the following rubric.

Holistic Rubric
Exceeding Meeting Approaching Below None
(10) (8) (6) (2) (0)
Substantial,
Content Clarity
specific, and/or
The presence of ideas Sufficient
illustrative
developed through developed Limited content Superficial
content No content
facts, examples, content with with inadequate and/or
demonstrating or answer
illustrations, details, adequate elaboration or minimal
strong provided.
opinions, statistics, elaboration or explanation content
development and
reasons, and/or explanation.
sophisticated
explanations.
ideas.

1. Provide one example of implementation of LongClick(). When do you think


you can properly use this in your application?

The code below is a simple example implementation of onLongClick(). When we


long click the button, the TextView changes from “Hello World” to “You long
click the Button”. I also tried the onClick() to differentiate the two event
listeners.

I think I can properly use the onLongClick() in a ListView so that when I long click
the one item, I can also select the other items in a ListView. Also, I can apply it on
selecting multiple images in a Gallery. And sometimes I long click the files in my
file manager when I’m deleting multiple files. I can implement the onLongClick()
when I develop a mobile game that needs a long click.

package com.example.LearningAct7;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


Button firstBtn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstBtn = (Button) findViewById(R.id.myBtn);

//This is an implementation of OnClickListener


firstBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView myText = (TextView) findViewById(R.id.myText);
myText.setText("You click the button");
}
});

//This is an implementation of OnLongClickListener


firstBtn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
TextView myText = (TextView) findViewById(R.id.myText);
myText.setText("You long click the button");
return true;
}
});
}
}

2. Provide one example of implementation of onFocusChanged().When do you


think you can properly use this in your application?

The code below is a simple implementation of onFocusChange() in EditText.


When we remove the focus on EdiText 1 and 2, a toast message of the input
from the user will appear. The onFocusChange() happens when the user goes
away from the view item.

I think I can properly use the onFocusChange() when I’m validating a text input
from the user. We can use this mostly in a Log in form or anything that needs a
user input so that the users know that they need to fill up some fields.
onFocusChange() is really important to make our more interactive and user-
friendly.

package com.example.LearningAct7;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

EditText text1, text2;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text1 = findViewById(R.id.text1);
text2 = findViewById(R.id.text2);

//This an implementation of onFocusChange() in EditText 1


text1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus){
if(!hasFocus && text1.getText().toString() != null){
Toast.makeText(getApplicationContext(),
text1.getText().toString(), Toast.LENGTH_LONG).show();
}
}

});
//This an implementation of onFocusChange() in EditText 2
text2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus){
if(!hasFocus && text2.getText().toString() != null){
Toast.makeText(getApplicationContext(),
text2.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
}
}

Score Sheet
Question 1 Question 2 Question 3 Total Score

You might also like