Having a button perform 2 jobs - java

Is there a way of having 1 single button perform more then 1 job? Lets say the user clicks on a button called 4 that displays a textview showing the number 4, can the user then press the button 4 again and display the string "four". Can you some how use switch() cases to do it?
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
add = add +4;
display.setText("2 + 2 =" + add);
}
});

Have a Boolean variable to keep track of states (please make this global to an activity so that function may access it). i.e.:
#Override
public void onClick(View v) {
if(flag){
add = add +4;
display.setText("2 + 2 =" + add);
}
else display.setText("Something else");
flag=!flag;
}

Related

Make new onclick listeners not overwrite previous ones

I want certain buttons to set text on certain textviews when clicked depending on their position in the array, I want the first four cases to make the first textview say "yeah", and the second four to make the second textview say "no". The first four cases worked until I added the second four, at which time all 8 of the buttons i'd created listeners for made the second textview say "no".
So it seems like the most recent listener is replacing all previous ones
public void applyClick(Button[] button, final TextView[] texty) {
for (int i = 0; i < 14; i++) {
switch(i) {
case 0:case 1:case 2:case 3: {
button[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
texty[0].setText("yeah");
}
});
}
case 4:case 5:case 6:case 7: {
button[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
texty[1].setText("no");
}
});
}
}
}
}
You forgot to put a break after each of the 2 cases.

Android: Limit the number of choices from the list of checkboxes

In my app, I have multiple checkboxes from which I have to limit the user to 5 choices.
Afterwards - if he chooses another option from the checkboxes, it is not to been marked.
This is my code sketch -
Veg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (Veg.isChecked()) {
if (result.length() == 10) {
finalresult = result.substring(0, result.length() - 1);
Veg.setEnabled(false);
} else {
result.append("1,");
System.out.println(result.length());
}
}
}
});
Why dont you use OnCheckChangedListener.
Keep all your checked checkbox ids in a arraylist, whenever a checkbox is checked, add it to arraylist, when unchecked remove, inside onCheckChanged event add a check for arralist.size() if its greater than 5 uncheck the checkbox by setChecked(false).
Same process for all checkboxes and use ArrayList for disable and enable checkboxes.
and below code is not good practice for programming.
Veg1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (Veg1.isChecked())
{
counter++;
if (counter <= 10) {
do your process
call one of the method for ENABLE all uncheck checkboxes
}
else
{
counter--
Veg1.setChecked(false);
call one of the method for DISABLE all uncheck checkboxes
}
// Veg.setEnabled(false); remove this line
} else {
counter--;
//result.append("1,"); remove this line
}
}
}
});

app crashes java.lang.NumberFormatException: Invalid int:

I use this code to get a number from an EditText and pass it into a Count Down Timer
final int mytime;
mytime = Integer.parseInt(textIn.getText().toString());
btnStartTimer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCountDownTimer(mytime);
}
});
However the app crashed and I get a:
java.lang.NumberFormatException: Invalid int:
However when I write it like this it works:
btnStartTimer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCountDownTimer(Integer.parseInt(textIn.getText().toString()));
}
});
Can please someone explain me difference ? Can I use the first code somehow ?
I really don't understand the problem
In you first example the value of the text has not been set and will be null. You want to do the action after the use has filled in the form and clicked on the button don't you.

Android button comparing

I've got very very basic problem in Java (Android) writing. Here it is, I've got code:
public void WriteValue (View sender){
Button bt=(Button)sender;
}
WriteValue is performed, when user click button. And now I want to compare button which user clicks, with button that Id I know. Something like
if(UserButton==ClearButton) Display.setText("0");
Thanks for help
Tux:)
You could just compare ids.-
public void WriteValue (View sender) {
Button bt = (Button)sender;
if(bt.getId() == R.id.clearButtonId) {
Display.setText("0");
}
}
step 1 : make your Class implement OnClickListener
step 2 :
// global variable
Button btOne ,bTwo;
step 3 :
// in onCreate() method
btOne = (Button)findViewById(R.id.btOne);
btTwo = (Button)findViewById(R.id.btTwo);
btOne.setOnClickListener(this);
btTwo.setOnClickListener(this);
step 4 : implement method from interface
puvlic void onClick(View v)
{
if(v==btOne)
{
// do work for btone
}
if(v==btTwo)
{
// do work for btTwo
}
}
You don't need to cast the View to a Button here. You can simply get the id of the View clicked which is the param sent to the function (assuming this is an onClick() function as defined in xml or by setting the listener in Java). So it could be something like this
public void WriteValue (View sender)
{
int id = sender.getId(); // get the id of the View clicked
swith (id) // switch logic on that id
{
case (R.id clearButtonId): // if the clearButton was clicked do this
Display.setText("0");
break;
default:
Display.setText("Some other String);
break;
}
}
You could also use if/else instead of a switch but this is more readable, IMHO. This easily allows you to add more logic for other Buttons.
Also consider using Java naming conventions. According to this code, I assume Display is a TextView or EditText so it should start with a lower-case letter (ex. display).

Android Notification Area Customization

I don't know whether this question get minus points, but I searched every where and my last resort is stackoverflow.
I need to add five buttons to notification area in horizontally. And each button I need to add even listener. I know it is possible to do with RemoteViews. But I never seen anyone adding event listener to each element.
These are the references if anyone need to refer.
Notifications Documentation
How to create a custom notification on android
SlidingDrawer API
You can add 5 anonymous listeners, or a single named listener.
Anonymous:
Button b1 = new Button(...);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// first listener's code goes here
}
});
Button b2 = new Button(...);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// second listener's code goes here
}
});
...
named is much the same, but contains a switch statement to differentiate what happens:
View.OnClickListener myListener = new View.OnClickListener() {
public void onClick(View v) {
String buttonTitle = ((Button)v).getText();
if ("title1".equals(buttonTitle)) {
// do things for the first button's click
} else if ("title2".equals(buttonTitle)) {
// do things for the second button's click
}
...
}
});
...

Categories

Resources