Perform action only if two buttons are clicked - android

This is the first time I'm ever dabbling in Android development so please bear with me.
My requirement is this:
I have two buttons on screen, A and B. If the user presses both buttons (order doesn't matter), I need another page to be displayed. Pressing either A or B should do nothing.
Is this possible? If so, how would I achieve this?
Thank you.

This is possible if you take a flag. (boolean)
You should set a flag in your button listeners.
public class Mtest extends Activity {
Button b1;
Button b2;
boolean flag_1 = false;
boolean flag_2 = false;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
flag_1 = true;
doSomething();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
flag_2 = true;
doSomething();
}
};
}
public void doSomething(){
if(flag_1 && flag_2)
{
//DO SOMETHING
}
}
}

Create two boolean's like button1isClickedand button2isClicked,then set an onClickListener for each Button. When the the Button is clicked set the value of these two boolean's to true, then simply create an if() statement that will chekc to see if both buttons have been clicked, like this:
if(button1isClicked == true && button2isClicked == true){
//display your new page
}

Related

How to achieve on first click listener in android?

I have a customView , i want to set onClick which will only be called on the very first click. In which i want to start a thread which will start a counter on other TextView , with simple onClickListener with each click a new threads starts which is a problem . How can i achieve such task ?
Another option is in your onClick() method do set a null listener, i.e.
#Override
public void onClick(View view) {
// disable any other clicks from now on
customView.setOnClickListener(null);
...
}
I think this is only logic problem, So I solve this problem by using a boolean variable for the first click:
boolean isFristClick = true;
#Override
public void onClick(View v) {
if (isFristClick) {
// Start your counter Thread here
isFristClick = false;
} else {
// Do nothing
}
}
What about making a workaround for that?!! like assigning a boolean value to tell if it's the first click:
private boolean first_click = true;
your_view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(first_click){
first_click = false;
// Do something on first click
}else{
// Do another thing on later clicks
}
}
});

Use a button to add other buttons, editText etc.

I need to be able to add buttons to a layout using an "add" button. The problem is that I need each button to have an OnClickListener()/onClick method. I was thinking every time the "add" button is pressed then i would add a new button to an array but im not sure add the listener and implement an onClick method for each button I create.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SmartChronometer chrono = (SmartChronometer) findViewById(R.id.chrono);
final Button start = (Button) findViewById(R.id.button2);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (chrono.isRunning())
chrono.pause();
else {
chrono.chronoStart();
}
}
});
}
I need to add chronomoter,button and listeners every time I click an "Add" button.
set all the clicklisteners as you wich!
call findViewById(R.id.btnSecond).setVisibility(View.GONE); on creat, then when clickin the first button
Button btnSecond;
...
public void onClick(View v) {
findViewById(R.id.btnSecond).setVisibility(View.VISIBLE);
if (btnSecond.getVisibility() == View.VISIBLE); {
findViewById(R.id.btnThird).setVisibility(View.VISIBLE);}
}
this way you can put all your information in the java file and all buttons in xml, but they will be hidden until click.
This is one way, other answer my come. Good Luck :)
implements OnClickListener
Button add = (Button) findViewById (R.id.addButton);
add.setOnClickListener (this);
List<Button> buttons = new ArrayList <Button>();
for (int i = 0; i < buttons.size (); i++){
buttons.get (i).setOnClickListener (this);
}
#Override
public void onClick (View v){
for (int i = 0; i < buttons.size (); i++){
if (v.getId () == buttons.get (i).getId ()){
// do stuff you want
}else if (v.getId() == R.id.addButton){
//add button
}
}
}
Hope this will work, didnt test it.

programatically change previous button selected state

I am using this code to create buttons and added this buttons in layout. When I click one of this button selected changed successfully. But what should I do here for clicking in button the previous button's selected state will be unselected.
In one word only one button has to be selected.
private void createButtons(String categoryTitle){
final Button myButton = new Button(mActivity);
myButton.setBackgroundResource(R.drawable.btn_default_holo_dark_trans1);
myButton.setText(categoryTitle);
if (mFirstButtonSeleted){ //this is for first run only first button will be selected
myButton.setSelected(true);
mFirstButtonSeleted = false;
}
mButtonsLayout.addView(myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myButton.setSelected(true);
}
});
}
Create a reference to the previously selected button. Then in your on click method you could do something like this :
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(previousButton !=null)
previousButton.setSelected(false);
myButton.setSelected(true);
previousButton = myButton;
}
});

Android - How to increase EditText value?

I'm new to android development..
I have this code in my main class:
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
When I click "Previous", the text field value becomes 19.
When I click "Next", the text field value becomes 21.
But it only displays these two values, nothing else, no matter if i click again. I want to subtract or add 1 whenever i click the appropriate buttons.
I think this happens because the event Listeners are inside onCreate() method? Any idea on how to make it update each time I click?
You need to move your parseInt inside your onClick:
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t+1));
}
});
In both cases, t is defined as a member variable of the listener, and never changed. move it inside the onClick method instead, like this (in both cases):
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t-1));
}

One button used for two Action

Can any one tell me? how can i use the same button for two Action?
Like em using one button in my activity that calculate some values and after calculating the when i again press same button then reset the all fields. Like in this Application
http://www.craziness.com/games/play-love-tester/ when i test the love by pressing the button
then i again press the same button then all fields reset.
what should i use in my activity for the above problem?
You can create a global variable which indicates the state of the program and then change this value when needed. In the OnClickListener of your Button you create an if statement which checks this variable and does the needed things for the associated value.
Example:
public class MainActivity extends Activity {
private int state = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (state == 0) {
// State 1
}
else if (state == 1) {
// State 2
}
else {
// Default state
}
}
});
// Rest of your code including state changing part
}
}
You can do it by changing the button text.
If you do not want that someone sees the text change. Test (example "click" and the other state "click " (1 blank at the end) or another solution.
....
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// todo: check for expected instance (Button)
Button btc = (Button)v;
String bText = btc.getText().toString();
if (bText == "open") {
btc.setText("close");
}
else if (bText == "close") {
btc.setText("open");
}
}
[...]
You can do so by using a toggle button.
In your xml file add a toggle button
<ToggleButton
android:id="#+id/tbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textOff="Reset"
android:textOn="Calculation"
android:background="#drawable/icon"/>
Java file
public class Reviews extends Activity implements OnClickListener {
private ToggleButton tbtn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tbtn = (ToggleButton) findViewById(R.id.tbtn);
tbtn.setOnClickListener(this);
public void onClick(View view) {
if (tbtn.isChecked()) {
//calculate the result
}
else {
//Reset your global calculation variable;
}
}
}
}

Categories

Resources