Getting the State of a Toggle Button - java

I may be going about this all wrong, but I have a view that contains a few ToggleButtons and a Save button. When the Save button is pressed, I want to collect the states of the various toggles as boolean values. I have tried the following in the onClickHandler for the Save button:
ToggleButton tb = (ToggleButton) findViewById(R.id.button1);
boolean pol = tb.isChecked();
and I would expect pol to be set to the state of button1, but it keeps being set to true. Of course I have tried this with the button in both states.
Thanks

you can try this
ToggleButton tb = (ToggleButton) findViewById(R.id.button1);
boolean pol = false;
tb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(tb.isChecked()) {
pol = true;
} else {
pol = false;
}
}
});
}

Related

changing text on textview with button's onClickListener

Hi I'm working at my first bigger app in android studio "FlashCards". I would like it to work so after you click on the button the flashcard's textview changes its text to next random flashcard untill you see all of the them how can i do something like 'continue' to my loop from inside onClick method.
here's the loop's code:
while(i < mTestDeck.size()) {
// generates random number which will represent position in deck.
int random = randomGenerator.nextInt() % mTestDeck.size();
// if random flashcard was already shown create random number again
if (mTestDeck.get(random).wasShown())
continue;
//text view that we will operate on
TextView deckTextView = (TextView) findViewById(R.id.flashcard_text_view);
// set text
deckTextView.setText(mTestDeck.get(random).getFront());
// set mWasShown to true
mTestDeck.get(random).flashcardShown();
Button myButton = (Button) findViewById(R.id.know_answer);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mTestDeck.correctAnswer();
}
});
myButton = (Button) findViewById(R.id.dont_know_answer);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
First, this way, you have a potential infinite loop. And if it can happens, it will happens! It's not a good idea to "get random item and check if it's ok or try again".
I think that it's better to keep a list with all items in a random order. You just have to iterate over it.
Something like:
int currentPosition = 0;
List<Card> items = new ArrayList<Card>(mTestDeck).shuffle();
// Call this method once in onCreate or anywhere you initialize the UI
private void function setCurrentCard() {
Card currentItem = items.get(currentPosition);
[...] // Set all UI views here
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (currentPosition > items.size) {
// TODO? End?
return;
}
currentPosition++;
setCurrentCard();
}
});
}

How to declare boolean inside button click?

I want to set something like this on button click:
boolean isLightOn = false;
if(!isLightOn)
{
FlashTask.flash.on();
isLightOn=true;
return;
} else {
FlashTask.flash.off();
isLightOn=false;
return;
}
But it's not working...
Thanks advance.
Hard to know without seeing all your code. But you could declare a variable as field member, then change it on every button click.
That's the simpliest way:
public class MyActivity extends Activity {
//boolean field member initialized as false by default
private boolean isLightOn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isLightOn){
FlashTask.flash.off();
} else{
FlashTask.flash.on();
}
isLightOn = !isLightOn;
}
});
}
}
Every time the button is clicked, boolean isLightOn = false; is called and isLightOn will always be false when the button is clicked. You are not preserving the previous state.
To preserve the state, make isLightOn as a class variable and remove boolean isLightOn = false; from your listener method.
You need somthing like this.
private boolean isLightOn = false; //defined as class variable;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isLightOn){
FlashTask.flash.on();
isLightOn=true;
}else {
FlashTask.flash.off();
isLightOn=false;
}
}
});
But make sure isLightOn is defined outside the listener scope.

Event get String from 2 buttons

I am using an Adapter to generate buttons in a GridView.
Using an OnClickListener, whenever I click a button from the GridView, it is possible to get the String of the button in a variable. However, I want to click a button, store its text in a variable, then click a different button, store its text in another variable so later I can compare the two texts. How could I do that?
gI'm using OnClickListener, therefore my previous implementation for one button was the following:
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText.toString();
With this approach, I can only get and store text from first button clicked, unless I store all the values in an array.
Ok so from what I can understand, you want to compare the strings of the two items in a grid view. If this is what you want, heres how I would do it(just a psuedocode):
String mValue1, mValue2;
boolean isOneClicked = false;
onItemClickedListener(item, position){
if(!isOneClicked){
mValue1 = item.stringValue;
isOneClicked = true;
}
else{
mValue2 = item.stringValue;
//do whatever you want here
//Reset when done
mValue1 = "";
mValue2 = "";
isOneClicked = false;
}
}
Please you can share your code to turn the things more comprehensible? Edit your question please... Anyways:
MainActivity.java
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private String butString, butString2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
butString = (String) button1.getText();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
butString2 = (String) button1.getText();
}
});
}
}
After that you can compare the two strings.

Pass method as parameter in Java to assign Listeners to multiple Buttons

I'm new to Java (coming from Python) and I'm trying to pass a method as a parameter in order to convert this code:
Button button1;
Button button2;
...
Button buttonN;
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick_button1(v);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick_button2(v);
}
});
(...)
buttonN = (...)
To something like:
public AssignListener( integer tButton, Method tMethod )
{
button_view = (Button) findViewById(tButton);
button_view.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
tMethod(view);
}
}
}
(...)
AssignListener( R.id.button1, onClick_button1 );
AssignListener( R.id.button2, onClick_button2 );
(...)
AssignListener( R.id.buttonN, onClick_buttonN );
I've read that you can't pass methods to functions, and some advice to wrap my function using Runnable to achieve this.
I have not clear idea about how to do it. Any idea on how do it easily? :?
Thanks.
EDIT: Should I wrap "AssignListener" in its own class and pass the class itself? :?
You can call findViewById() and seOnClickListener into onCreate method, and OnClickListenet outside the onCreate.
Button button1 = (Button)findViewById(R.id.button1);
or
findViewById(R.id.button1).seOnClickListener(mClickListener);
findViewById(R.id.button2).seOnClickListener(mClickListener);
findViewById(R.id.button3).seOnClickListener(mClickListener);
findViewById(R.id.button4).seOnClickListener(mClickListener);
private OnClickListener mClickListener = new View.OnClickListener(
public void onClick(View view){
switch(view.getId()){
case R.id.button1:
//button1 click handle here...
break;
case R.id.button2:
//button2 click handle here...
break;
case R.id.button3:
//button3 click handle here...
break;
case R.id.button4:
//button4 click handle here...
break;
}
});
Try it, hope it will helpful to you.
You could define and pass an onClickListener like this:
public AssignListener( integer tButton, OnClickListener listener ){
tButton.setOnClickListener(listener);
}
The simplest way is to create listener implementation for each button, as is written in your first snippet.
I don't see any reasonable benefit of having universal method like AssignListener(). Don't reinvent bicycle, use existing setOnClickListener() method and pass listener specific for button.
An easy solution would be to create a function called tMethod that takes a button id, and has a switch statement that allows for a certain action to be performed for the given button id. This would then be called inside the onClick function like tMethod(view.getId()).
Btw, Python has methods. Java has functions. Pedant mode turned off!
public class MyClass implements OnClickListener, OnLongClickListener {
Button button1;
Button button2;
...
Button buttonN;
. . .
setButtonsListener(findViewById(<your top view id>));
. . .
void setButtonsListener(ViewGroup vg) {
int count = vg.getChildCount();
Button btn;
View v;
for(int i=0; i < count ; i++) {
v = vg.getChildAt(i);
if( v instanceof Button ) {
btn = (Button)v;
btn.setOnClickListener(this);
btn.setOnLongClickListener(this);
} else if( v instanceof ViewGroup ) {
setButtonsListener((ViewGroup) v);
}
}
}
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}

Reset Button for a 'Click Counter' Android App

I am creating a small 'Click Counter' app, which basically has a button function, which when pressed, a textview field displays the number of clicks made.
I am trying to setup a 'Reset' button, which changes the value of the textview back to 0. This is the code I have so far:
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
txtCount.setText(String.valueOf(0));
}
This changes the textview value to 0, however when the button is clicked again it starts back from the count which it was on when the reset button was clicked.
For example:
a. the current count = 10
b. reset button is selected
c. current count = 0
d. clicker button pressed
e. current count = 11
Am I using the wrong statement or intent?
Your not resetting whatever counter you are using:
#Override
public void onClick(View arg0) {
yourCounter = 0;
txtCount.setText("0");
}
You need to save the counter state in an instance field or a static field. Then set that field to zero on reset. For example:
private int counter = 0;
// ...
wreset = (Button) findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
counter = 0;
txtCount.setText(String.valueOf(counter));
}
}
increment = (Button) findViewById(R.id.increment);
increment.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
counter++;
txtCount.setText(String.valueOf(counter));
}
}
public void onClick(View view) {
int count = 0;
count++;//increment the count
TextView text = (TextView) findViewById(R.id.counttxt);// resorce location
text.setText("No.of Clicks " + count);// view in the text

Categories