I'm trying to enable/disable a seekbar when a checkbox is checked(or not).
I'm using this to create and refer:
CheckBox checkBoxProva = (CheckBox) findViewById(R.id.checkbox_prova);
boolean varCheckBoxProva = checkBoxProva.isChecked();
And this simple if to disable/enable the view:
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}
All this is inside the onCreate.
When the app starts the seekbar is disabled (so the if works), but if I check the CheckBox it doesn't change to enabled.
EDIT: I've succed thank to the reply of #rohan bhatia.
You are doing all of this in your onCreate method that only gets called when the activity is first created. You need to setup a listener that will be fired when the checkbox is clicked. See: https://developer.android.com/guide/topics/ui/controls/checkbox.html
You need a Listener that notifies when a checkbox is selected or deselected..
checkBoxProva.setOnCheckedChangeListener(new new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
seekBarProva.setEnabled(isChecked);
}
}
);
Accordingly, whenever you click the checkbox the method onCheckedChanged() will be executed.. You will still need to specify this in onCreate -
seekBarProva.setEnabled(checkBoxProva.isChecked());
As onCheckedChanged() only listens for changes in checked state, so to specify initial stage you will need that above line too.
Simply insert an else block
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}else{
seekBarProva.setEnabled(true);
}
Alternative solution:
Personally, I'd recommend adding a listener to the checkbox inside your onCreate method.
checkBoxProva.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(varCheckBoxProva){
seekBarProva.setEnabled(true);
}else{
seekBarProva.setEnabled(false);
}
}
});
Related
I added the following lines of Code into my OnCreate method.My goal is to assign a button to two functions and to call them up alternately. With the first click the text of the button should be changed and the EditText should be editable. At the second click, the fields should no longer be editable and the button text should change to the first alternative. I have implemented two OnClickListeners and the program structure seems logical to me. Nevertheless, I get an error message; "Cannot resolve symbol onClickListener". What can I do to get the setup described above up and running? Thanks for all responses!
private Button ProfilUpdate;
ProfilUpdate=findViewById(R.id.buttonProfilUpdate);
.
.
.
.
final ProfilUpdate.OnClickListener listener2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Profil bearbeiten");
profilVorname.setFocusable(false);
}
};
ProfilUpdate.OnClickListener listener1 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Ă„nderungen speichern");
profilVorname.setFocusable(true);
v.setOnClickListener(listener2);
}
};
ProfilUpdate.setOnClickListener(listener1);
why don't you create a boolean isFirstClick = true , and then check it in the same listener
ProfilUpdate.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isFirstClick){
//Do the job for the first click process
isFirstClick= false;
}else {
//Do the job for the second click process
isFirstClick= true;
}
}
};
ProfilUpdate.setOnClickListener(listener);
There can only be one click listener on one view at a time. Use ProfileUpdate.setOnClickListener(listener object). To get the alternate functionality, you can define a Boolean to keep track of the state, for the example, define a class variable at the top Boolean shouldChangeText = true, and in the onClick body in the listener, do something like:
If (shouldChangeText) { // change the text
}
else { // clear the text
}
shouldChangeText = !shouldChangeText
I have created an application which uses the fragment, I am opening a fragment on the click of the first fragment with custom animation, meanwhile the animation is going on I am able to click the button on the first fragment and it creates 2 fragments. how can I not click on my button while moving from one fragment to another, I just don't want double click of the same button.
can anyone help me?
Try below
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.setEnabled(false);
}
});
// on animation complete, enable it
// button.setEnabled(true);
You could try set android:clickable in your XML layout to determine whether a button can be clicked.
You could implement the following method into your code and call it when needed.
public void myMethod(boolean isLoading){
myButton.setEnabled(!isLoading);
}
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {view.setEnabled(false);}
});
Try using myButton.setEnabled(false) in your click callback function.
Try the below kotlin snippet
view.setOnClickListener {
val tag = "my_dialog"
val oldFragment: Fragment? = supportFragmentManager.findFragmentByTag(tag)
if(!(oldFragment?.isAdded == true)) {
val myDialogFragment = MyDialogFragment.newInstance()
myDialogFragment.show(supportFragmentManager, tag)
}
}
In case a fragment(with the tag specified) is already added to the activity then this code prevents new fragment creation and adding it to the activity.
I want to know how can I stop message sending in a loop.
When I press stop, messages are still sent.
I have provided a stop() method.
for (i = 0; i < copy; i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(stop!=1)
sms.sendTextMessage(number,null,message,sentPIn,pdelint);
}
}, 25000);
stop method
int stop=0;
public void st(View view){
Button b=(Button)findViewById(R.id.button2);
b.setText("Stopped");
stop=1;
}
Modify St function to this
public void st() {
Button b = (Button) findViewById(R.id.button);
#Override
b.setOnClickListener(new View.OnClickListener() {
b.setText("Stopped!");
stop=1;
});
}
The problem was u were not using an listener to detect button press. It is not advisable to change the button text use a text view instead!
Most likely he has set onClick attribute in the xml, although only he'll be able to clarify that.
I'm saying that because I see View as a parameter in the method st(View v).
Although if the attribute onClick is not set in xml #Audi 's solution would be good. Though on the contrary if the attribute has been set in the xml then
you should check if view.getId() == R.id.button then apply a button cast to your view and set its text to "Stopped".
I want to execute some code immediately after the form is shown.
I want to check the size of a button on the form and use the same size to create a new button at runtime.
I tried onStart() and onResumed(), but they do not work.
Thanks,
Ashok
You can add a globallayout listener. Add the listener at onActivityCreated.
Check this example:
button1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
button1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
button1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
//here the size is already available. create new button2 here with the size of button1
}
});
you are looking at the onCreate() method, that is the function that will run when your app is first loaded in to memory
When you click button in my app if you are fast enough before the screen/popup loads it loads them multiple times. I know how to disable the click on the button but that's not an option, because when you close the popup or return to the previous screen the button is disabled. I tried with Handler and Runnable to wait for 1s before the button is active again but this solution is not optimal in case if the OS needs more time to open the next screen. So I am searching for the most optimal solution. Any ideas?
Edit: setClickable(false) and then setting it back to true doesn't work because it loads my screen/popup slower than expected the button is enabled again and it opens the screen/popup multiple times again.
You can disable the multiple click at the same time using the following code
private boolean isClicked;
#Override
public void onClick(final View v) {
if(isClicked) {
return;
}
isClicked = true;
v.postDelayed(new Runnable() {
#Override
public void run() {
isClicked = false;
}
}, 1000);
}
Implement logic in your onClick to determine whether you want to ignore the click.
You can disable the button. When you close the popup enable the button and when the popup is visible make it disable. Keep listening the actions for popup and when the user get back to the previous screen.
Maintain one variable on button onClick listener and change the value to determine when you want to click button..
You can stop multiple operations by this way.
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick()
{
performOperation();
}
});
public void performOperation()
{
static boolean working = true;
if(working)
{
return;
}
working = true;
//Do you work here;
working = false;
}