How to disable button runtime in click event? - java

I searched on Google and StackOverflow but I did not find really what I need. I want to disable a button runtime in a click event but it does not work. I have 2 buttons whose names are ButtonA and ButtonB
My code:
ButtonA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonA.setEnabled(false);
ButtonA.invalidate();
ButtonB.setEnabled(true);
ButtonB.invalidate();
}
});
ButtonB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonB.setEnabled(false);
ButtonB.invalidate();
ButtonA.setEnabled(true);
ButtonA.invalidate();
}
});
I use onclick listener events in my list adapter class
If I refresh the screen it works but I want to do it runtime. When I click the button I want to see it will be disabled.
How can I fix this?

You can use clickable() method for this.
ButtonA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonA.setClickable(false);
ButtonA.invalidate();
ButtonB.setClickable(true);
ButtonB.invalidate();
}
});
ButtonB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonB.setClickable(false);
ButtonB.invalidate();
ButtonA.setClickable(true);
ButtonA.invalidate();
}
});

Or you can try this:
Button b=(Button) findViewById(R.id.btnB);
b.setEnabled(true);

The thing is :
it should be defined by final,
like this;
final Button ButtonA,ButtonB;

Related

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

Buttons outside the onCreate

Always in my apps I added buttons in void onCreate, but now I'm trying to do app with more buttons (about 10). I would like to all buttons active on start app.
In my opinion it is too much buttons to add in this onCreate and app will be starting to long.
I tried to put this:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
})
out of onCreate
but AndroidStudio underlines setOnClickListener and view
I don't have ideas, how and where can i add button out of onCreate.
If you don't want to overcrowd your oncreate method, then create a clicklistener outside onCreate anywhere in activity and in onCreate just set it.
onCreate :
edit_a_member = (Button) findViewById(R.id.edit_member);
delete_a_member = (Button) findViewById(R.id.delete_member);
edit_a_member.setOnClickListener(handleClick);
delete_a_member.setOnClickListener(handleClick);
clickListener:
private View.OnClickListener handleClick = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.edit_member:
member_selected = EDIT_MEMBER_SELECTED;
callDialog();
break;
case R.id.delete_member:
callDeleteAlert();
break;
}
}
};
You can simply add a separate method for your buttons in the same class, e.g.:
public void onCreate(...){
//Standard setup of views or whatever you want to do here
this.addButtons();
}
private void addButtons(){
Button b1 = new Button("Hi");
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
Button b2 = new Button("Hi to you too");
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
}
This is an example. You can do this in soooo many ways. I feel like you should thoroughly learn Java's fundamental Object Oriented programming, because that's really what your question suggests you don't understand. Go follow a youtube tutorial. I always like "The New Boston"'s Java tutorial series on youtube.
PS: You can make code like this beautiful under the 'Words of wisdom': Don't repeat yourself
If you have to do a lot of work in your onCreate but you are worried that the UI will take too long to load you can always post a delayed runnable to a handler so in the onCreate method put :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add your code here
}
},10);
what this will do is your UI will load then the code in your Runnable will be executed 10 milliseconds after your UI loads thus your app will not take too long to load the UI, even though in your case I doubt it would be necessary.
If you are declaring the buttons in xml file :
Add these properties in each button Declaration in your Xml :
android:clickable="true"
android:onClick="onClick"
And now in Activity Class create a method like this :
public void onClick(View v){
switch(v.getId){
case R.id.{buttons_id_in_xml}
(Your Code)
break;
(Like for others)
}
}
If you want to add buttons dynamically :
Create a method to add the button like this:
void addButton(String buttonName, int button id){
Button button = new Button(this);
button.setText("Push Me");
(add it to parent Layout of xml)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(id){
case id1:
(handle )
break;
(like for others)
}
}
});
}
The best way to do this is:
add implements View.OnClickListener to
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// declare variables
private Button mBtn1;
private Button mBtn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// make an instance to the btns
mBtn1 = findViewById(R.id.btn1);
mBtn2 = findViewById(R.id.btn2);
// set onClickListener
mBtn1.setOnClickListener(this); // with "this" you are passing the view
mBtn2.setOnClickListener(this);
}
// implement onClick
#Override
public void onClick(View view) {
// check which btn was clicked by id
switch (view.getId()) {
case R.id.btn1:
btn1Clicked();
break;
case R.id.btn2:
btn2Clicked();
break;
}
}
private void btn1Clicked() {
// your code btn1 clicked
}
private void btn2Clicked() {
// your code btn2 clicked
}
Hope this helped. Cheers!

how to play different audio on button click android java

I want to play music when a button is clicked, but I want to play different music with different button then the first music stopped and the second music would play.
code:
charge = MediaPlayer.create(this, R.raw.charge);
santai1 = MediaPlayer.create(this, R.raw.santai1);
}
public void charge(View view) {
charge.start();
}
public void santai1(View view) {
santai1.start();
}
Say, I clicked charge button and then played the charge.mp3. Then I want to play santai1.mp3 but when I clicked the santai1 button the charge.mp3 will stop playing and the santai1.mp3 will play.
I want this to be able the opposite and it can be done continuously.
When I clicked santai1 button then santai1.mp3 will be played, while the santai.mp3 still playing I clicked the charge button. I want the santai1.mp3 stopped and then play the chage.mp3.
When I clicked on charge button, then I clicked on santai1 button, both music started together.
if you need it for many
create a mediaplayer object and a method.
private MediaPlayer media;
private void stopMusic() {
if (media != null) {
media.stop();
media.release();
media= null;
}
}
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopMusic();
media= MediaPlayer.create("you acivityname".this, "mp3file directory");
media.start();
}
});
try this:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(charge.isPlaying()){
charge.stop();
}
santail.start();
}
});
and do the same for other button
well it's simple you need 2 if statments for the buttons to check if one of them is playing.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(santail.isPlaying()){
santail.stop();
}
charge.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(charge.isPlaying()){
charge.stop();
}
santail.start();
}
});

How attach two Click Listener in a button in android?

I want to attach two click listener in a button and both onclicklistener should be different. Is there any way to do this.? Can't use function to call from first to second. When I use this then I got output from second one only. I want both output.
I am working on some screening task, so whenever a use click on a button it tell me that user clicked on this in Logcat and also button do its normal task.
First is :
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("First Click" , "Clicked on button 1");
}
});
Second is :
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v2) {
if (v2 instanceof Button) {
Log.i("User Clicked a checkbox with value ", " : " + ((Button) v2).getText().toString());
}
}
});
You can't do that. Setting your second OnClickListener removes the first one.
That's why the function is called setOnClickListener() instead of addOnClickListener()
As you say, you can call two functions from the onClick()
Edit:
On your edit, you say that you want to show a message when your button is clicked.
You can add the loggging functionality that you need before doing anything else on the event, simply doing
#Override
public void onClick(View v) {
// Log something
// Your functionality
}
Or you can create a class implementing View.OnClickListener
public class MyOnClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
// Log something
}
}
And then, on your Activity:
btn.setOnClickListener(new MyOnClickListener() {
#Override
public void onClick(View v) {
super.onClick(v);
// Your functionality
}
});
You can use function in OnClickListener like-
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
run1();
run2();
}
});
private void run1(){
//Your first functionality
}
private void run2(){
//Your second functionality
}
If I understood you correctly, you could do this:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dothings(); //what you are trying to achieve with this button click
showViewInLogCat(v); //to show you the view in the logcat
}
}
where showViewInLogCat() is a function that show you which view was clicked in your logcat:
public void showViewInLogCat(View view) {
if (view instanceof Button) {
Log.i("User Clicked a checkbox with value ", " : " + ((Button) view).getText().toString());
}
//and add the other if
}
You can call this function in every OnClick event on other views
Probably if you want to do something like this..!
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Log.i("First Click" , "Clicked on button 1");
// add a boolean here to check if you want to do the task or not.
doTask = true;
doGeneralTask(doTask); //to show you the view in the logcat
}
}
and in doGeneralTask(doTask) do something like this.
public void doGeneralTask(boolean doTask) {
if (doTask) {
// do whatever generalized tasks you need.
}
}

onClickListener is called only after soft-keyboard press

I write an android app
I set an editText.setOnClickListener(...)
but i see that when the user clicks, the soft-keyboard is opened and only
when the user clicks on a keyboard key - the onClick() is called.
how to catch the click before the soft keyboard is opened?
I want to avoid the keyboard opening.
here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_login);
initMembers();
setOnClickListeners();
initFieldsTexts();
setKeyboardVisibilityListener();
}
private void setOnClickListeners() {
mPhoneNumberField.setInputTextOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
}
and:
public class PhoneLoginFillInField extends LinearLayout {
..
public void setInputTextOnClickListener(OnClickListener onClickListener)
{
mInputText.setOnClickListener(onClickListener);
}
during debugging i see this line is called twice
mPhoneNumberField.setInputTextOnClickListener(new OnClickListener() {
though it's called only from onCreate and there setOnClickListeners(); is called once
You mean EditText should not gain focus; it needs to respond only to click events. Do this:
editText.setFocusableInTouchMode(false);
And then carry on:
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Your code...
}
}
Set an onTouchListener to the EditView. Remember to return true to avoid the event propagation.

Categories

Resources