I just want to implement one button to do multiple actions like on first click make Textview1 visible and on second click make Textview2 visible and so on.
here is my code it works but for 2 actions only i want to set more visible component in one button i hope its clear and Thanks for any help
final TextView textView_r4 = findViewById(R.id.tv_r4);
final EditText editText_r4 = findViewById(R.id.input_R4);
final TextView textView_r5 = findViewById(R.id.tv_r5);
final EditText editText_r5 = findViewById(R.id.input_R5);
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView_r4.setVisibility(View.VISIBLE);
editText_r4.setVisibility(View.VISIBLE);
}
});
findViewById(R.id.Addbtn).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView_r5.setVisibility(View.VISIBLE);
editText_r5.setVisibility(View.VISIBLE);
return true;
}
});
You can add an enum State to keep track of which state your button is in. Create a class field in the same class (activity) that these methods are in, and change the state every time you click. Then in the .setOnClickListener method you can check which state the button is in, and depending on that do different actions.
private State state = INITIAL;
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (state) {
case INITIAL:
// do first action
state = State.CLICKED_ONCE;
break;
case CLICKED_ONCE:
// do second action
state = State.CLICKED_TWICE;
break;
default:
// clicked too many times, no action taken
break;
}
}
});
private enum State { INITIAL, CLICKED_ONCE, CLICKED_TWICE }
Related
I'm trying to figure out, how I could interrogate if one of two buttons were clicked.
Thanks in forward.
When one of the buttons is clicked, set a variable into true, for example
boolean aButtonWasClicked = false;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
aButtonWasClicked = true;
}
});
So now, you have stored in aButtonWasClicked the result of the "interrogation". So you can do different things depending on it:
if(aButtonWasClicked){
//yes
} else {
//no
}
I may be asking a basic question, but to be honest, I have no real developement or code knowledge. I've been requested to make a prototybe of some basic app, that is supposed mainly to be buttons on screens, activable or desactivable. I've written some kind of TL;DR in case my explanations are bad
I've been coding this on Android Studio 3.0, I (hardly) managed to place PNGs files on the screen, making it looking like a button.
Thing is, while some parts of the app are mainly constituted with independants togglable button. There a part where pressing a button must deselect the others. AND, if this button is pressed a second time open another activities.
Here's part of my code I'm using.
This one for independants buttons
indbutton1.setOnTouchListener(new View.OnTouchListener(){
// track if the image is selected or not
boolean isSelected = true;
public boolean onTouch(View v, MotionEvent event) {
if(isSelected) {
indbutton1.setImageResource(R.drawable.indbutton1slct);
} else {
indbutton1.setImageResource(R.drawable.indbutton1nosl);
}
// toggle the boolean
isSelected = !isSelected;
return false;
}
});
And this one for going into other activities
movements.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent gestesActivity = new Intent (getApplicationContext(), movements.class);
startActivity(movementsActivity);
finish();
}
});
TL;DR
How should I proceed to have a mix of pressing button, disabling others enabled, then, when pressed a second time, I go to another activity.
Thank you for any help :) -Pliskin
Here is how I would do that. Let's say you have 4 buttons.
// your class fields
boolean [] alreadyTouched = new boolean[4];
For each of the 4 buttons :
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!alreadyTouched[0]){
setAlreadyTouched(0);
// one click actions here
}else{
// second click's action
}
}
});
Now you make a private method in your class :
private void setAlreadyTouched(int index){
for (int i = 0; i< alreadyTouched.length; i++)
alreadyTouched[i] = false;
if(index != -1)
alreadyTouched[index] = true;
}
And to reset your boolean array when the button looses the focus :
button0.setOnFocusChangeListener(new View.OnFocusChangeListener(){
#Override
public void onFocusChange(View view, boolean hasFocus){
if(!hasFocus)
setAlreadyTouched(-1);
}
});
You can do exactly the same thing but with an array of int if you want more than two clicks with some slight modifications. For example :
// your class fields
int[] alreadyTouched = new int[4];
Your privat method :
private void setAlreadyTouched(int index){
if(index == -1){
for (int i = 0; i< alreadyTouched.length; i++)
alreadyTouched[i]=0;
}else
alreadyTouched[index] = alreadyTouched[index] +1 ;
}
Then just add some if in your onClickListeners :
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setAlreadyTouched(0);
switch(alreadyTouched[0]){
case 1:
// one click actions here
break:
case 2:
// second click's action
break:
// ... and so on
default:
// action for max number of clicks here.
}
}
});
I have a method which on button click needs to perform in a specific way and if no button is clicked, it should alternatively act differently.
Basically on clicking on the button, it should destroy my service. And if I don't click on it, it will eventually be destroyed. The thing is that I want the destroy method act differently based on these 2 scenarios.
You could add the clicked View as method argument:
myOnClick(View view){
if(view != null){
//Button was clicked
....
}else{
//No Button was clicked
.....
}
}
Is the method performing periodically? Then just add a bool to see if the button was clicked before.
like:
Main:
boolean clicked = false;
onCreate:
Button b = (Button)findViewById(R.id.buttonName);
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if(clicked){
clicked = false;
}else{
clicked = true;
}
});
}
I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}
i need to know, how to recognize, which button is pressed.
Like if i have two buttons ,say button 1 and button2,and both of them performing the same method, say method(),how to determine which button pressed ?
Regards
Most ellegant pattern to follow:
public void onClick(View v) {
switch(v.getId())
{
case R.id.button_a_id:
// handle button A click;
break;
case R.id.button_b_id:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}
This way it's much simplier to debug it and makes sure you don't miss to handle any click.
OR... you can just put a android:onClick="foo" in the xml code of the
button, and define a method on java with the signature. Inside the
method foo, get the id and compare it with the one you need
public void foo(View v){
if (v.getId() == R.id.yourButton){
}
else if (v.getId() == R.id.nextButton){
}
}
I have 10 buttons performing the same method updateText(), I used this code to get the clicked button's text:
public void updateText(View v){
Button btn = (Button) findViewById(v.getId());
String text = btn.getText().toString();
}
If by "performing the same method" you mean theirs OnClickListener then you have to reference the parameter being passed to it.
public void onClick(View v) {
if(v==btnA) {
doA();
} else if(v==btnB) {
doB();
}
}
Ok got the solution
if (yesButton.getId() == ((Button) v).getId()){
// remainingNumber
}
else if (noButton.getId() == ((Button) v).getId())
{
// it was the second button
}