Why strings are not equal - android

In my android app, I have a Play button. but after I clicked the button, nothing happend. Seems when comparing the text on the button, they are not equal. if I use indexOf(PLAY), it works. Cannot figure out why it behaves like this. I defined my string values in res/values/strings.xml as
<string name="play">Play</string>
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();
}
}
});
Thanks for the help.

You can use an intermediate variable, like this
String buttonText = b.getText().toString();
if (buttonText.equals(PLAY)) {
.....
Or just
if (b.getText().toString().equals(PLAY)) {

Related

Perform action only if two buttons are clicked

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
}

Non-final variable on a click listener

I am making an android application. I have a TextField and a button. Based on the value of the textfield, as soon as the user clicks the button I want to make something. I have the code:
EditText et = (EditText)findViewById(R.id.editText1);
String value = et.getText().toString();
ImageButton ib = (ImageButton)findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (value = "a" ) {
//do something }
}
});
This however doesn't compile, saying "Cannot refer to a non-final variable value inside an inner class defined in a different method". Is there any way to fix this? Thanks a lot
Use
final String value = et.getText().toString();
and then use,
if(value.equals("a") {
}
If you want to compare the value of a string you should use the "equals" method instead of "=".
The code will look like this:
EditText et = (EditText)findViewById(R.id.editText1);
final String value = et.getText().toString();
ImageButton ib = (ImageButton)findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (value.equals("a") ) {
//do something }
}
});
final String value = et.getText().toString();

compare string stored in string resource in android?

I have a button on which (like) is written from string resource, on click I want to toggle like to unlike & vice verse.
How to compare button contain like or unlike from string resource?
My string.xml contains
<string name="like">Like</string>
<string name="unlike>unlike</string>
Try this..
bt.setText(getString(R.string.txt1));
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myStr = bt.getText().toString();
if((myStr.equals(getString(R.string.txt1)))) {
bt.setText(getString(R.string.txt2));
} else if ((myStr.equals(getString(R.string.txt2)))) {
bt.setText(getString(R.string.txt1));
}
}
});
In string.xml
<string name="txt1">11111111111</string>
<string name="txt2">2222222222222222</string>
You can use getString method
if(btn1.getText().toString().compareTo(getResources().getString(R.string.app_name)) == 0)
{
// do some code here
}
use below code
Button button = (Button)findViewById(R.id.btn1);
if(button.getText().toString().equals(getString(R.string.like)))
{
button.setText(getString(R.string.unlike));
}
else
{
button.setText(getString(R.string.like));
}
try as:
String text = getResources().getString(R.string.texttest);
Button button = (Button)this.findViewById(R.id.btntest);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(button.getText().toString().equals(text))
{
// do some code here
}
}
});
You can retrieve the label (Text on Button) in this manner.
Button button=(Button) findViewById(R.id.button1);
Log.d("Text",""+button.getText());
You can do this::
String value=buttonname.getText().toString
//this Will Give the Text On the Button
Now compare the value like this Way and change the Name Of the Button::
if(value.equals("Like"))
{
buttonname.setText("UnLike");
}
else
{
buttonname.setText("Like");
}

How to check which ImageButton was prssed?

I have lots of image buttons. User can press any of those button and i need to know which button is pressed. These buttons appears dynamically so i don't know how much of them will be.
For one image button i would write this listener:
ImageButton ib = new ImageButton(this);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(TravelBite.this, "test", Toast.LENGTH_SHORT).show();
}
});
But how to do one listener for all my Images Buttons? Can i recognize which button was pressed by it's tag ? Like tag would be ID from sqlite.
Also i put image to button with this code:
button.setImageDrawable( testPic );
button is ImageButton and testPict is drawable (image)
But when i press this button it don't show that it is pressed if i do this:
button.setBackgroundColor(R.color.transparent_background);
I had to do this because i just want to see Buuton image which i could press and recognize what i pressed.
Thanks.
ok what you can do is that you can write a single callback function and then set it to each and every button it will allow you to handle each button with a sing function like :
View.OnClickListener btn_listener = View.OnClickListener() {
#Override
public void onClick(View v) {
// Do whatever work you want.
int id = v.getid();
// check for id and do you task.
}
Arraylist<Button> btn_group = new Arraylist<Button>;
or
Arraylist<int> btn_id_group = new ArrayList<int>;
for (int i =0; i < 10; i++) {
Button btn = new Button(getApplicationContext());
btn.setId(i);
btn_id_group.add(i) or btn_group.add(btn);
btn.SetOnClickListener(btn_listener);
}
I think it will work for you.
You can use View.setTag(object) and View.getTag() and store in it sqlite id. Something like this:
View.OnClickListener listener = View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(TravelBite.this, (String)v.getTag(), Toast.LENGTH_SHORT).show();
//or some function that do somthing useful
//if( ((String)v.getTag).equals("image1") ){} or anything else
}
And in for loop:
String tagFromSqlite = "image1";
ImageButton ib = new ImageButton(this);
ImageButton.setTag(tagFromSqlite);
ib.setOnClickListener(listener);
final OnClickLisener listener = new OnClickListener() {
public void onClick(View v){
switch((Integer)v.getTag()){
case R.id.zero:
break;
case R.id.one:
break;
case R.id.two:
break;
}
}
}
//when init the Buttom
ImageButton btn = new ImageButton(Context);
btn.setTag(NUMBER);
you'll have to manually assign ID's to keep them separated - I had to do something similar (1000 was the base ID I chose to add upon as well)
Although View v in the listener refers to the button pressed, when you programmatically create buttons the id's are not unique and caused me issues, so that's why I set them specifically
View.OnClickListener btn_listener = View.OnClickListener()
{
#Override
public void onClick(View v)
{
//you can use v.getID() here
}
}
for (int i =0; i < 10; i++)
{
Button btn = new Button(getApplicationContext());
btn.setID( 1000 + i );
btn.SetOnClickListener(btn_listener);
}

Android Dialog return String?

Hi I am popping up dialog to take comments from user. And returning a value according to that. That "rcomment" is a global variable. And it returns null. This is not working. What am I doing wrong ?
public String getDoNotBoardDialog(final int groupposition)
{
final Dialog dia = new Dialog(this);
dia.requestWindowFeature(Window.FEATURE_NO_TITLE);
dia.setContentView(R.layout.donotboard);
final EditText donotedit = (EditText) dia.findViewById(R.id.donotboardcomment);
donotedit.setText("");
Button button1 = (Button) dia.findViewById(R.id.donotboardbutton);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
r = donotedit.getText().toString();
String boardingComment = getString(R.string.donotboard) + " " + r;
PostCommentForAC(groupposition, boardingComment);
Intent intent = new Intent(getBaseContext(), TestExList.class);
intent.putExtra("EmpID", empid);
startActivity(intent);
rcomment = "true";
dia.cancel();
}
});
Button button2 = (Button) dia.findViewById(R.id.boardbutton);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
rcomment = "false";
dia.cancel();
}
});
dia.show();
return rcomment;
}
The getDoNotBoardDialog will initial return rcomment as null. rcomment will only be changed to "true" or "false" when the onClickListeners are fired. They are fired not when getDoNotBoardDialog is run, but after that, whenever the onClickListeners are fired.
Whatever you want to happen when rcomment is changed to "true" or "false" should be placed in the onClick methods. So if you want to check what rcomment is after a user clicks, do it there.
Edit: dont use below code. It will kill your app (ANR).
You would have to wait before you can return something. A quick (but really dirty) solution would be to add some wait/notify mechanism like so: (written blind so might contain some errors).
public String getDoNotBoardDialog(final int groupposition) {
// some Object to wait on
final Object waitOnMe = new Object();
final Dialog dia = new Dialog(this);
dia.requestWindowFeature(Window.FEATURE_NO_TITLE);
dia.setContentView(R.layout.donotboard);
final EditText donotedit = (EditText) dia.findViewById(R.id.donotboardcomment);
donotedit.setText("");
Button button1 = (Button) dia.findViewById(R.id.donotboardbutton);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
r = donotedit.getText().toString();
String boardingComment = getString(R.string.donotboard) + " " + r;
PostCommentForAC(groupposition, boardingComment);
Intent intent = new Intent(getBaseContext(), TestExList.class);
intent.putExtra("EmpID", empid);
startActivity(intent);
rcomment = "true";
dia.cancel();
// stop waiting.
synchronized(waitOnMe) {
waitOnMe.notify();
}
}
});
Button button2 = (Button) dia.findViewById(R.id.boardbutton);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
rcomment = "false";
dia.cancel();
// stop waiting.
synchronized(waitOnMe) {
waitOnMe.notify();
}
}
});
dia.show();
// this wait's until someone calls notify
synchronized (waitOnMe) {
try {
waitOnMe.wait();
} catch (InterruptedException e) {}
}
return rcomment;
}
It's problematic though. You could miss the notify() and thus never stop waiting (e.g. when you close the dialog via the "back" button). A much cleaner and safer solution would be to use some Callback mechanism (you call some method in your program from each onClick) to get a value from the dialog.
In you application
the program flows to
return rcomment;
directly after going to
dia.show();
Remember it does not wait for the button to be clicked before it goes to the return statement!!! It goes to return statement directly after the dialog is shown (before the button is clicked)
Try using this string.equals(data) it should find out if the string is the same. Since rcomment is a string. Also like Soham said it will not update only until it is clicked.
I suggest that in the future you should change to Boolean rcomment. Since it only looks like you are doing either true or false status.

Categories

Resources