Android - toggleButton delay with own OnClickListener - java

I have problem with unwanted delay after click on toggleButton using own OnClickListener.
I make my listener by this advice on stackoverflow, like below:
public class ToggleButtonOnClickListener implements OnClickListener{
private String _name;
public ToggleButtonOnClickListener(String name) {
_name = name;
}
#Override
public void onClick(View v) {
Log.i("toggle button clicked",_name);
}
}
and using this:
toggle.setOnClickListener(new ToggleButtonOnClickListener(device.GetName()));
But it not fire onClick method after first click, but the next one.
And because I have group of toggleButtons is this very unhappy, when I click on first, and onClick method fire after click again or even after click to second (or any) from the group.
The OnCheckChangeListener behaves the same.

Please refer developer's example.
You can implement something like below:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable vibrate
} else {
// Disable vibrate
}
}

After looking for errors and testing other options, I found that delay caused not Listener, but the log statement.
So, the code above working well except for
Log.i("toggle button clicked",_name);
and an alternative without needs have own class(parametrs) using OnCheckedChangeListener is:
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(buttonView.getContext(), "test", Toast.LENGTH_LONG).show();
}
});
I don't know why this Log do, but I used them only for debug, so problem solved!

Related

how to show one time toast messages in android studio

I wanted to add a `toast message` when clicked on a button and I added it. Now I want show it only **one time**, that means the toast message will **not show again** if clicked on the button after the first time **also after a restart of app**.
Please help me!
You can do it with SharedPreferences that you store in another class like Utility.java:
public class Utility {
public static SharedPreferences preferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
public static Boolean hasSendToast(Context context) {
return preferences(context).getBoolean("Toast", false);
}
public static void setSendToast(Context context, Boolean bool) {
preferences(context).edit()
.putBoolean("Toast", bool).apply();
}
}
And use it with your Toast inside the onClickListener in your MainActivity.java like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
if (!Utility.hasSendToast(getApplicationContext())) {
Toast.makeText(getApplicationContext(), "My Toast", Toast.LENGTH_SHORT)
.show();
Utility.setSendToast(getApplicationContext(), true);
}
});
}
}
You might want to look into https://developer.android.com/training/data-storage/shared-preferences. After the first click write a boolean into shared preferences to indicate the first click has happened. Next time the user clicks the button make sure to check if that boolean has been set and don't show the toast if that's the case.
You can use a boolean flag and store the value using Shared Preference.
btn.onClickListener {
if(!getToastShownStatusFromSharedPreference()) {
showToast()
changeToastShowsStatusToSharedPreference()
}
/* other operations */
}
Yes you can do that with SharedPreferences which is a pref manager in android. You can follow the below process:
Declare a boolean as like isFirstTime = true.
On the click of the button check boolean status, and if its true then fire the toast message and make isFirstTime = false.
That's it.

Android - Cannot setClickable(false) a button after click

I have a button like a switch where I am trying to setClickable(false) after I click it so that only the first click will be handled
(additional clicks are ignored in the case of accidental double-clicks/multiple-clicks).
Here is a similar code:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
//do other things
}
});
Then eventually, I have a code somewhere where I will reset the clickable to true, depending on a state variable, so I can switch-off.
The problem is when I click the button very quickly, it seems the succeeding clicks are still handled.
Is there a delay to the effects of setClickable()?
Also, I have read about using setEnabled(false) instead, but I cannot use it in my case. I need the button to still be enabled but not clickable.
Judging from your comment you probably need something like this.
Boolean SWITCH_ON = false;
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!SWITCH_ON ){
SWITCH_ON = true;
}
}
});
Button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
if(SWITCH_ON ){
// do your task for long click here ...SWITCH_ON
}
return true;
}
});
You can use button.setEnabled(false); within your onClick method to disable the button.
Disabled buttons don't trigger the onClick method, and you can easily re-enable it with button.setEnabled(true); when needed.
You could add another variable named buttonEnabled or so and initialize it with true. Then in the onclick do this:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
if(buttonEnabled) {
//do other things
}
buttonEnabled = false;
}
});
Note that you need to change the variable to if you want to reactivate it

How to trigger an event when user clicks away from EditText?

I am building an app, and I want the app to trigger an event when the user has entered text into the editText and clicks away from it or closes the keyboard? How can I do this?
I am not very skilled in Java, so I would be grateful if you provided some description or code.
You can use onFocusChange listener on your edit text to overcome this problem
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b==true){
//entered in the edit text
}
else {
//left edit text
}
}
});
Simply set an onFocusChangedListener on your EditText and configure what happens when the EditText loses focus. Here's an illustration:
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean bool) {
if(bool){
// here, the EditText is in focus
}
else {
//here, the EditText is no longer in focus.
// do what you want to do
}
}
});
I hope this helps.

Android: Wait for button click

I have to implement a return method of a class. So the structure is as simple as
public boolean yesNo(String message){
//return something
}
The problem here is that there is an EditText object and a Button and I want return a boolean depending on the answer entered by the User in the EditText. So naturally I have to wait for the user to write and click the Button when he finishes. I already tried setting up an onClicklistener inside the method but then I would not be able to pass anything to the outer method.
I know that in android everything is asynchronous so I you are not supposed to wait for the user to do something but in this case I have no other idea. Also FYI the method that I'm implementing is of the UserInfo class of the Jsch library. From what i understood this UserInfo is designed for user interaction so I figured it would somehow manage "prompt" the user by calling the yesNo method and then "wait" for the user to react. Any ideas? Also I would be happy if anyone would explain to me if there is some error in my logic. Im not quite confident in using AsynTask or runnables but I think its got something to do with this.
The only available documentation of UserInfo class
Thanks a lot!
EDIT: code so far:
public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
public Button goButton = (Button) MainActivity.contentView.findViewById(R.id.startSSH);
public EditText editText = (EditText) MainActivity.contentView.findViewById(R.id.commandSSH);
public boolean yesNo = false;
public boolean promptYesNo(String str) {
goButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String answer = editText.getText().toString();
if(answer.equals("yes")){yesNo = true;}
else {yesNo = false;}
}
});
return yesNo;
}
}
Put an OnClickListener on the Button when the screen is initialized, for example at onCreate lifecycle method. At the listener's onClick method you can call the yesNo method with the EditText content.
Try this way at the screen's onCreate method:
mButton = (Button)findViewById(R.id.my_button);
mEditText = (EditText) findViewById(R.id.my_edittext);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
yesNo(mEditText.getText().toString());
}
});
I already tried setting up an onClicklistener inside the method but
then i would not be able to pass anything to the outer method.
What do you want to pass to the outer method? You can create class variables to make it available inside the whole class.
The promptYesNo method:
public void promptYesNo(String s) {
if(s.equals("Yes") //do something
else if(s.equals("No") //do something
}
Change your code this way:
public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
public Button goButton = (Button) MainActivity.contentView.findViewById(R.id.startSSH);
public EditText editText = (EditText) MainActivity.contentView.findViewById(R.id.commandSSH);
goButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (prompt) {
promptYesNo(editText.getText().toString());
}
}
});
}

OnclickListener in a checkbox (Android Studio)

here is part of my code which includes the CheckBox(ChckBoxNo):
final CheckBox ChckBoxNo = (CheckBox)promptsView.findViewById(R.id.ChkBoxNo);
ChckBoxNo.setChecked(true);
ChckBoxNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ChckBoxNo.isChecked()) {
ChckBoxNo.setChecked(false);
}
else if (!ChckBoxNo.isChecked())
{
ChckBoxNo.setChecked(true);
}
}
});
At the begining I set true for the isChecked() method on my checkbox , then I implement the onclicklistener on the checkbox.
When I run the app the checkbox is Checked as I defined eralier , but when I click on the checkbox ,It's unchecked and then immidately checked again(I didnt clciked again on the checkbox! )
What should I do in order to fix that,what wrong in my code ?
Thanks!
try this:
checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setChecked(checkPasswordExist());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox.isChecked()) {
// your code to checked checkbox
}
} else {
// your code to no checked checkbox
}
}
});
A Checkbox will handle the "checking" process automatically - you do not have to manage this yourself for the standard usage.
Check out this example from the docs. Here, when a click event is caught, they are doing operations based on the isChecked() state.
You're battling the CheckBox. It's standart behavior that checkbox change states, you dont need to do that by yourself.
Remove setChecked true and false and paste something usefull there instead )
You dont need to set the checked state on the same that you have klicked.
You shold handle some other Operations on specific state.

Categories

Resources