Change if any EditText is in focus - java

In my program i use many EditText dynamically created , and i need to call method each time when any of them get changed (lost focus). is it possible to do such thing ?
otherwise how to make on focus lost\change lister for all View ?

you will need to set View.setOnFocusChangeListener for EditText to listen for focus change .
For Example:
View.OnFocusChangeListener editTextFocusChnage=
new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
swich(v.getId()){
case Edittext1_id:
// do your work here..
break;
case Edittext2_id:
// do your work here..
break;
case Edittext3_id:
// do your work here..
break;
.....
}
}
};
where Edittext1_id,Edittext2_id,.... is dynamic EditText id's
EDIT :-
add FocusChangeListener to EditText's as:
editText1.setOnFocusChangeListener(editTextFocusChnage);
editText2.setOnFocusChangeListener(editTextFocusChnage);
editText3.setOnFocusChangeListener(editTextFocusChnage);
.....

Related

One OnClick handler for multiple buttons in a fragment

I have 3 buttons in a fragment that I want to use the same click event. How can this be achieved within a fragment?
XML
<Button
android:id="#+id/btn_1"
android:onClick="btnClick_DoSomething"
android:text="#string/one"/>
<Button
android:id="#+id/btn_2"
android:onClick="btnClick_DoSomething"
android:text="#string/two"/>
<Button
android:id="#+id/btn_3"
android:onClick="btnClick_DoSomething"
android:text="#string/three"/>
Java
#Override
public void btnClick_DoSomething(View v) {
}
Error
#Override (within the fragment Java class) becomes underlined in red and the following error is returned
Annontations are not allowed here
I want the onClick event to be the same for all 3 buttons
You dont need to write #Override annotation as you are not overriding the method. Just use
public void btnClick_DoSomething(View v) {
}
You will get a callback in this method at runtime.
you simply don't do it in XML, do it in Java instead:
<Button
android:id="#+id/btn_1"
android:text="#string/one"/>
<Button
android:id="#+id/btn_2"
android:text="#string/two"/>
<Button
android:id="#+id/btn_3"
android:text="#string/three"/>
then...
private final OnClickListener onClick = new OnClickListener(){
#Override
public void onClick(View view){
switch(view.getId()){
... cases...
}
}
somewhere initialising the views you do:
fragmentView.findViewById(R.id.btn_1).setOnClickListener(onClick);
fragmentView.findViewById(R.id.btn_2).setOnClickListener(onClick);
fragmentView.findViewById(R.id.btn_3).setOnClickListener(onClick);
}
What method are you trying to override? Do any of the parent classes have a method btnClick_DoSomething? You do not need to override anything when setting a click listener from a Layout XML. Just ensure that a method of the same name with a void return type and a View as its only argument exists in the activity class that will use this layout
The general way to distinguish clicks from different Views in the same onClick handler is to identify them by id
public void btnClick_DoSomething(View v){
switch(v.getId()){
case R.id.btn_1:
// handle click from button 1
break;
case R.id.btn_2:
// handle click from button 2
break;
case R.id.btn_3:
// handle click from button 3
break;
}
}
There are some misunderstandings here. To override a Click event, your Fragment need to implement the View.OnClickListener interface like this:
public class YourFragment extends Fragment implements View.OnClickListener{
#Override
public void onClick(View v) {
}
}
Note that the name of the method must be onCLick, must return void and recives a View as a parameter to be overrided from the interface. In this case you need to set a Listener to each button in your fragment:
btn1 = (Button) view.findViewById(R.id.btn_1);
btn1.setOnClickListener(this);
Inside this method you can control which object was clicked by it´s ID
#Override
public void onClick(View v) {
int id = v.getId();
switch(v.getId()){
case R.id.btn_1:
// handle click from button 1
break;
case R.id.btn_2:
// handle click from button 2
break;
case R.id.btn_3:
// handle click from button 3
break;
}
}
Or, you don't need to override the method, so remove the anotation that will work's fine :
public void btnClick_DoSomething(View v) {
switch(v.getId()){
case R.id.btn_1:
// handle click from button 1
break;
case R.id.btn_2:
// handle click from button 2
break;
case R.id.btn_3:
// handle click from button 3
break;
}
}
Instead of declaring the onClick method in the xml layout file, you could have your Fragment implement View.onClickListener interface, override onClick(), and set all 3 buttons onClickListener like this btn.setOnClickListener(this). Put the behavior you want for all 3 buttons in onClick(). All 3 will have the same behavior (unless you check which button the event came from in onClick()).

After click on editText is done when going to another edittext

I'm having a question where I couldn't find the answer online or know how to find it..
I have EditText xml attribute and I made an event listener
to this attribute by changing the color of an underline beneath it. Is there a way when the focus is removed from this EditText (i.e user click on any other element rather than this one) to remove the highlighted color for the line I colored?
On the onclick event listener? It seems weird, but I want the opposite of the onclick like onclickremove or something.
You can use the the setOnFocusChangeListener to your EditText. If lost focus,clear the color filter:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
else{
editText.getBackground().clearColorFilter();
}
}
});
If you want to change your view color, just add the below line in onFocusChange:
view.setBackgroundColor(Color.parseColor("#ffffff"));
Hope this helps.
You need to use setOnFocusChangedListener for this. The hasFocus determines whether the focus is removed or given to a view. It being false indicates that user has left the field.
EditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus == false){
// change the color
}
}
});

Simple edit text?

I am getting the users information from an edit text. I do have a listener that gets their entered information after clicking submit, but I want to also get the entered info after clicking back or clicking somewhere else:
For example, if the users clicks on the black space, I want to get the text they entered. If they type "hello", and click back rather than "enter", I still want to get the text hello. If, however, they don't type anything, I don't care about their input. How can I achieve this?
Thanks,
Ruchir
First add these as a class variables
private String inputText;
private EditText yourEditText;
Get the instance of your EditText View
yourEditText = (EditText)findViewById(R.id.your_editText);
When a button is clicked, you can get the content of the EditText field like this
Button mButton = (Button)findViewById(R.id.m_button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputText = yourEditText.getText().toString();
}
});
If a user press the Back button, you can get the input if any like this
#Override
public void onBackPressed() {
inputText = yourEditText.getText().toString();
super.onBackPressed();
}
Then check if there is any value assigned to your String variable
if(inputText.equals("") || inputText == null){
// there is no value
}else{
// there is value entered.
}
To extend my solution for clicking some where else
add a class variable
private boolean isEditTextHasFocus;
then create a focus listener which will check if the Edittext has focus
private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
isEditTextHasFocus = true;
} else {
isEditTextHasFocus = false;
}
}
}
Add this line in onCreate(Bundle savedInstanceState) method
yourEditText.setOnFocusChangeListener(focusListener);
Then override to onTouchEvent(MotionEvent event) listener and access the Edittext input when the key up action is called
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if(!isEditTextHasFocus){
inputText = yourEditText.getText().toString();
}
}
return true;
}
I hope this will give you a further idea to find your unique solution.
Overriding what happens when the back button is pressed is bad practice and is unnecessary for what you want to do.
You need to use a special listener called onFocusChangedListener. This function is called anytime an element gains or loses focus. In this case for your editText it will be called whenever someone clicks on it or away. Pressing the back button or leaving the editText in any way will call this function. In the following code I check if
if(!username.hasFocus())
which makes it so the value is only saved when focus from the editText is lost rather than everytime focus is changed.
You haven't added any of your own code so I am just going to use obvious placeholder variables in my code example.
Edittext username = (EditText findViewById(R.id.YOUR_EDITTEXTS_ID);
String previousValue = ""; // to keep track of value change
String usernameValue = "";
username.setOnFocusChangeListener(new View.OnFocusChangeListener(
{
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (username.hasFocus()){
//take note of value for comparison when clicking away
previousValue = username.getText().toString();
} else if (!username.hasFocus()){
// check if value has changed
if (!previousValue.equals(username.getText().toString()){
usernameValue = username.getText().toString();
}
}
}
});

How to initialize a view in an Android Method?

I am working on an Android Application that will take the height and weight of a person and calculates its BMI and Required calories based on Gender and Age.
When I press calculate button, the onClick method should be activated and thus there is another method called in the implementation of the 1st method.
The problem is that they are asking me to initialize the view and I dont know what do they mean and how to initialize in this case.
here's my java code for this:
public void calculateCalories()//this is the onClick method
{
View view;
onRadioButtonClicked(view); // here I get the error
}
public void onRadioButtonClicked(View view)
{
boolean checked=((RadioButton) view).isChecked();
switch (view.getId())
{
case R.id.male:
if (checked)
male_calories();
break;
case R.id.female:
if (checked)
female_calories();
break;
}
}
If I understand correctly your Java code doesn't know which of your activity views is your checkbox, that's why you are getting an error.
The common practice to initialise a view is writing this line in onCreate method:
CheckBox cb = (CheckBox) findViewById(R.id.yourcheckboxname);
Then to add listener:
cb.addOnCheckChanged(this);
and implement onCheckChanged within your Java class.
And you don't need to manually call onCheckChanged() - it's fired automatically everytime user checks or unchecks the checkbox

How to clear android textedit field

I am traing to clear textedit field when it is focused. I know about hint option, but I wanna clear textedit everytime when user actives it, also if is filled by user (now user have to clear field manualy everytime he wants change value).
I load fragments in my app so there are a lot of edittext fields which I wanna clear on focus, so is there universal method to do this, or I have to do it to all fields severally?
Should I create another java file to this method or put inside onCreate?
You need to add an onClickListener and set the textfield to an empty string when clicked. You need to add the listener for each textfield but you can reuse the same listener because the functionality is the same. You can set the listener on onCreate, that's fine.
You should you OnFocusChangeListener as is shown in code below:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
((EditText)v).setText("");
}
}
});
Simply set an View.OnFocusChangeListener with the method setOnFocusChangeListener(View.OnFocusChangeListener l).
In the callback onFocusChange(View v, boolean hasFocus) you can do the following:
if (hasFocus && v instanceof EditText) {
((EditText) v).setText("");
}

Categories