Android Studio, interrogate if button was clicked - android

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
}

Related

How to code a button with differents effects when pressed multiple times

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.
}
}
});

button that chages backgorund image according to onclick

I need to change some visible properties of a button once it has been clicked. The button is always the same one, it doesn't change. I need a button that is at the beging green and when i click on it, it should change to grey. if i click on it again it should go back to green and so on. Its one button that just changes color and text according to if it beeing clicked (even manytimes).
Do i need to use a normal Button or a RadioButton or a RadioGroup?
could you please show me also how to implement its function by code? thanks
check out this
boolean flag;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(flag){
button.setbackgroundColor(Color.GREEN)
flag=false;
}else{
button.setbackgroundColor(Color.GREY)
flag=true;
}
}
}
Make a global boolean variable
boolean green = true;
Then use the following code to change color.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(green){
button.setBackgroundColor(Color.GRAY);
green =false;
}
else {
button.setBackgroundColor(Color.GREEN);
green = true;
}
}
});

How to create button that behaves as toggle button?

I want functionality of toggle button in simple button.like
Button b=(Button)findViewById(R.id.x);
if(b.isChecked())
{
//do somthing
}
else
{
//do somthing
}
any one have any logic in mind ?i dont want toggle button so please help.
You can make use of the setTag(Object o) and getTag() attributes for button..
By default in xml set the tag as "on"(according to your need):
And then in JAVA:
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(b.getTag().toString().trim().equals("on"))
{
b.setTag("off");
//And your neceaasary code
}
else if(b.getTag().toString().trim().equals("off"))
{
b.setTag("on");
//And your neceaasary code
}
}
});
You need to apply onClickListener combined with a boolean to remember the state on your button this way:
Button button = (Button)findViewById(R.id.button);
boolean state = false;
button.setText(state?"state true":"state false");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (state)
state = false;
else
state = true;
button.setText(state?"state true":"state false");
}
});
Its is better to use CheckBox and you can make CheckBox look like a button by changing it Background. See this link, it might help.

Android single button multiple functions

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;
}
}

How to determine which button pressed on android

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
}

Categories

Resources