is there any other way instead of using isPressed() method? - android

I have 2 activities, A and B.
In activity A I have radiogroup, a CHECK button and NEXT button .
before I can press the NEXT button , I want to choose first one of the radio button in the radio group and CHECK it first so that I can know if its correct or incorrect before I can press the next button.
here's my code:
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if (btnCheck.isPressed()) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
}
} else {
return;
}
}
});
I can't press the NEXT button even I have chosen one of the radio button and check it.
Please help.
Your answers will be appreciated.
Thank You.

I'm not sure what the problem is, but you could just create a boolean to check if you're concerned that isPressed() isn't working as you'd expect.
boolean hasChecked = false;
btnCheck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// put conditions here to check if it was pressed at the right time
hasChecked = true;
}
});
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if (hasChecked) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
}
}
}
});

if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
//Do something here.
}
The above if you want to check if any one of the radio buttons is checked.
if (radio1.isChecked() && radio2.isChecked() && radio3.isChecked()) {
//Do something here.
}
The above if you want to check if all the radio buttons are checked.
If I understand you correctly, you also want to check some btnChk's click, before you press NEXT. So put a FLAG (Global Variable) in your btnChk's onClick method, if you want to log that if that particular button was presed.
If you want anyone of the radio button checks to be true, use the || condition in the if statement. Then to check if btnChk was pressed do the following:
Declare a Global FLAG at just below the class declaration:
public int FLAG = 0;
Set a buttonclick event on your button in onCreate():
Button btnChk = (Button)findViewById(R.id.YourId);
btnChk.setOnClickListener(btn);
private View.OnClickListener btn = new View.OnClickListener() {
#Override
public void onClick(View v) {
FLAG =1;
}
};
Then in your NEXT button check the following:
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if(FLAG ==1){
//Do something here.
}
}
What the FLAG does: FLAG is init to zero initially, as soon as you press btnChk, FLAG becomes 1, if btnChk is not pressed, FLAG remains 0. so in the button click of Next button you check the value of FLAG and do your stuff accordingly.

Related

Android Studio, interrogate if button was clicked

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
}

How can i make 1 method act differently based on button clicked?

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

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