Modifiy text in a button - if statement - android

I am currently creating an android application with different options. One of the option would be to have a button that would show "Activate" as default. When the application would be running, clicking on it would change it to "Disable" and then to "activate" if clicked again. I believe that all I have to do is to .getText with a string variable then use this variable in a if statement but it seems like it is not reacting to any of my conditions...
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText == "#string/Disable") {
button.setText(R.string.Enable);
}
else if (buttonText == "#string/Enable"){
button.setText(R.string.Disable);
}
}
});
Thanks for help
Phyzikk

You shouldn't use the == operator when comparing strings in Java. Source
You should either use the .equals() method of the string, or alternatively you could keep a global boolean state flag to determine which value is set. This way you won't need to do a string compare every time you need to figure out if it's active or disabled.

Use .equals to compare strings. You wont need the #String/ prefix as this is not part of what the button displays.
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText.equals(getResources().getText(R.string.Disable)) {
button.setText(R.string.Enable);
}
else if (buttonText.equals(getResources().getText(R.string.Enable)){
button.setText(R.string.Disable);
}
}
});

Related

I want to perform many tasks in one Edittext

I want to perform many task in one Edittext. Like when I edit the first number in edittext it store in string value and edittext may be null after that when I enter 2nd number in same edittext 2nd number may also save in another string. Then after some action perform answer show in Textview.
Please help me out...
private void save_on_cick_listen(){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(save == 0){
// save first string
editText.getText().clear(); //or you can use editText.setText("");
save+;
} else if (save == 1){
// save second string
editText.getText().clear(); //or you can use editText.setText("");
save+;
}
}
});
}

Dynamic Buttons with two functionalities ANDROID

I have dynamic buttons that are created by a variable that can change.I want that these buttons have two functions. I did one option but I don't know how to implement the other option.
the first time I click the button I call a function that do something and the second time that I click the same button I would like to do another action. And I want to repeat this running with all the dynamic buttons created.
My code is:
LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);
for(int i=0;i<drawView.getNumeroMallas();i++){
Button buttonMalla = new Button(this);
buttonMalla.setText("Malla "+(i+1));
buttonMalla.setId(i+1);
final int index = i;
buttonMalla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Malla malla = drawView.getMalla(index);
drawView.paintMallaSelected(malla);
}
}
});
buttonsLayout.addView(buttonMalla);
}
}
EDIT - Very important:
I readed your code again, you could use the getTag/setTag to remember the last state of the button (i missed the for part, sorry!)
for(int i=0;i<drawView.getNumeroMallas();i++){
Button buttonMalla = new Button(this);
buttonMalla.setText("Malla "+(i+1));
buttonMalla.setId(i+1);
buttonMaila.setTag(Boolean.FALSE);
Then in setOnClickListener
if (((Boolean)v.getTag()) == Boolean.TRUE)
{
// Do first action
v.setTag(Boolean.FALSE);
}
else
{
// Do second action
v.setTag(Boolean.TRUE);
}
An idea could be to use a variable to know which was the last action.
A boolean variable
private boolean action = false;
If action is false do the first thing and set it to true. If it's true do the second action and set it to false.
It should go out any method (global of the class)
Something like
private boolean action;
buttonMalla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (action == true)
{
// Do first action
}
else
{
// Do second action
}
action = !action;
Malla malla = drawView.getMalla(index);
drawView.paintMallaSelected(malla);
}
}
});
Anyway if it does something of important you should manage better the button (example button action is not based on a boolean variable but in a specific state.) time ago i builded a "select all" and "unselect all" function in an app and i checked if the user have unselect manually something to let the button act again like a "select".. I hope i gave to you an idea.
Anyway the boolean variable is the most immediate way to do it.

Script button click behaving not like user click

In my android app, I have a Play button. After I manually clicked the button, everything works fine. but when I use uiautomator to trigger the button click, nothing happend. After debugging, I am pretty sure it is the string comparison step (labeled stuck here) that causes the failure. Very confused why it behaves differently. I do see the button get clicked in both ways (with button color change). If I use button.getText().toString(), both worked. BTW, button.getText() returns CharSequence, not String object.
I defined my string values in res/values/strings.xml as
<string name="play">Play</string>
The java code:
private final static String PLAY = "Play";
//some code in between
Button playButton = new Button(this);
playButton.setText(R.string.play);
playButton.setTextSize(BUTTON_FONT_SIZE);
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().equals(PLAY)) { //stuck here.
startPlay();
} else {
stopPlay();
}
}
});
Could you please share how you tried to click the button using UiAutomator. Below code should work
UiObject buttonToClick = new UiObject(new UiSelector ().text(Play).className("android.widget.Button")); buttonToClick.clickAndWaitForNewWindow();

android toggle button state set texview text

i have a toggle button which when set to the on position sets the hint to one of my textview to
"kg"
.
the initial hint of the text view is
"st"
which should be shown if the toggle is in the off position.
when i first start the app the textview dispalys
"st"
(which at first is ok as the toggle is in the off position) now when i press the toggle it turns to the on position and displays
"kg"
in the textView (this is also ok.)
now comes the problem. if i click on the toggle again (off position) the textView stays as
"kg"
does anyone know how i could set it to always display "st in the off state and "kg" in the on state.
many thanks in advance
addListenerOnButton();
}
public void addListenerOnButton() {
unitToggle = (Button) findViewById(R.id.unitToggle);
final TextView tw1 = (TextView)findViewById(R.id.tw1);
unitToggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
StringBuffer result = new StringBuffer();
tw1.setHint("kg");
unitToggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
StringBuffer result = new StringBuffer();
if(tw1.getHint().toString().equals("kg"))
tw1.setHint("st");
else
tw1.setHint("kg");
The main reason for the said problem is the logic which has not yet been implemented.
When you click the button for the first time it sets the text to "kg" which it will set always on any number of click. since you have written the statement
tw1.setHint("kg");
inside your onClick() method without keeping the state of the button. emphasized text.
In order to make it correct use a boolean flag and change its state on each click and set the text based on the flag value.
The best way to do it is to use ToggleButton which has the inbuilt on/off states so you don't need to have your on boolean flag and set the hint based on the button state.
Try
private boolean on=false;
unitToggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
StringBuffer result = new StringBuffer();
if(on){
tw1.setHint("kg");
on = true;
}else{
tw1.setHint("st");
on = false;
}

ParseInt Exception

I am creating a small calc app with EditText views and Im running into an runtime exception when the user leaves an EditText view empty causing the ParseInt to try and Parse nothing. Ive read that I need to 'Try' and 'Catch' this error before it occurs, but Im unsure of where and how to do this!
Any advice is much appreciated!
Here is my code:
public class HandlerExamples extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v) {
String a,b,t;
double vis;
EditText txtbox1 = (EditText)findViewById(R.id.A);
EditText txtbox2 = (EditText)findViewById(R.id.B);
EditText txtbox3 = (EditText)findViewById(R.id.t);
TextView tv = (TextView) findViewById(R.id.Answer);
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
t = txtbox3.getText().toString();
vis = ((Integer.parseInt(a)*1) + (Integer.parseInt(b)*2)) / (Double.parseDouble(t));
tv.setText(double.toString(vis));
}
}
Thanks so much!
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.xx:
//do things xx click
break;
case R.id.yy:
//do things yy click
break;
}
}
you can get the view id to know whick widget was clicked.
Changwei Yao defined one way you can do this, but here's the way most Android programmers would do this (programmatically), since it's a little easier to read and figure out what your widgets are doing:
But first, remove the implements OnClickListener from your Activity, as it's not needed.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// what you want your button to do when clicked
}
}
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// what you want your EditText to do when clicked
// (such as editText.setText(""))
}
}
Another way to do the same thing is to define android:onClick="insert_method_name_here" for the widgets that you want perform an action when clicked. In your case, in your main.xml (since that's what you're using in your Activity), you could write something like...
<Button android:id="#+id/testButton"
(other attributes you wish to apply to the button)
android:onClick="buttonAction" />
<EditText
(other attributes)
android:onClick="textAction" />
And then, in your Activity, you define the methods buttonAction(View v) and textAction(View v). Note that these methods must be public void, and must take the sole argument View v.
(One advantage of the XML method is that you don't necessarily have to define an android:id attribute for these widgets, unless you need to be able to manipulate them or extract information from them in your code (which means you will need to define an android:id for your EditText since you'll likely want the user's input))
If you only need to exclude the empty text field then hotveryspicy's solution is probably the quickest. For a secure solution: catching the NumberFormatException will filter anything that can not be converted to an integer.
int vis;
try {
vis = Integer.parseInt(a);
}
catch(NumberFormatException nfe) {
Log.e(TAG,"trying to convert:"+a+" to integer failed");
vis = 0;
}

Categories

Resources