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");
}
Related
If the user clicks the "go" button, the application should check if the EditText's value is "Settings" or not? How can I do this?
Something like this:
Button buttn1;
EditText Text1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttn1 = (Button)findViewById(R.id.Button111);
Text1 = (EditText)findViewById(R.id.Text111);
buttn1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if (Text1 == "Settings") {
//CODE
}else {
//CODE
}
}
});
}
You can get the content of an EditText field like this: Text1.getText().toString() and you can check String equality using .equals().
So combined, this would be Text1.getText().toString().equals("Settings")
Try This:
if (Text1.getText().toString().equals("Settings")) {
//CODE
}else {
//CODE
}
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();
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)) {
I'm making a simple app which has two buttons and one edittext. When the buttons are clicked the edittext wil display values of my database. I tried this code but it did not work
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == bnt1) {
SharedPreferences sharename = PreferenceManager.getDefaultSharedPreferences(this);
String na = sharename.getString("thename", "null");
edit.setText(na);
} else if (v == bnt2) {
SharedPreferences sharedescribed = PreferenceManager.getDefaultSharedPreferences(this);
String dess = sharedescribed.getString("thedescribed", "null");
edit.setText(dess);
}
}
Please show me the problems.
It looks like you probably don't have a Button attached to your onClick() method which would result in a null pointer exception when you try to reference v inside the method. There are different ways of implementing this (programmatically or through xml). With what you show (not overriding the onClick() method) it looks like you would want to use the xml way which means you need to declare the function for your Buttons
<Button
android:id="#+id/btn1"
android:onClick="onClick"
then do the same for btn2. Then v will reference whichever Button was clicked. You can also do this programmatically but setting an onClickListener
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
// some code
}
});
But then you have to do the same thing for btn2 so if you want to reuse the same function for both Buttons then the first method would be preferred.
Here is a link to a previous answer of mine convering the same thing
Use the below code:
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharename = PreferenceManager.getDefaultSharedPreferences(this);
String na = sharename.getString("thename", "null");
edit.setText(na);
}
}
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedescribed = PreferenceManager.getDefaultSharedPreferences(this);
String dess = sharedescribed.getString("thedescribed", "null");
edit.setText(dess);
}
}
Try this code :
SharedPreferences sharename = this.getSharedPreferences("sharename", MODE_WORLD_READABLE);
String prefName = sharename.getString("thename", 0);
edit.setText(prefName);
I guess the Exception is NullPointerException. Please check your share preference not null before call getString() method.
I have two onclick method in android project
clr=(Button)findViewById(R.id.Button01);
clr.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tv1.setText("CLR");
et1.setText("");
refrigerant = "";
pres = "";
temperature = "";
superheat_oda = 0;
sub_cool = 0;
}
});
And i have onther onClick method in which i have to call that method directly
prs=(Button)findViewById(R.id.Button02);
prs.setOnClickListener(new OnClickListener() {
----- I have to call that method---
}
});
Is there Any Solution for this?
You want to call the first onClick from the second? Just extract the contents of your first onClick in a separate method and call that method from each onClick.
Edit: As per st0le's comment, you can do what you want by calling clr.performClick(). (Didn't know that.) Still, extracting it into a separate method seems cleaner.
you can do something like this in the XML file
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="some_function" />
and put this function in the Java file
public void some_function(View view) {
// stuff...
}
and put the some_function in both "onClick"s
You should turn to use the simplest way that I always do as below:
#Override
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
//DO something
break;
case R.id.button2:
//DO something
break;
}
}
};
I would recommend to use the same OnClickListener for both buttons if both buttons really have to do the same thing:
OnClickListener l=new OnClickListener() {
public void onClick(View v) {
tv1.setText("CLR");
et1.setText("");
refrigerant = "";
pres = "";
temperature = "";
superheat_oda = 0;
sub_cool = 0;
}
};
clr=(Button)findViewById(R.id.Button01);
clr.setOnClickListener(l);
prs=(Button)findViewById(R.id.Button02);
prs.setOnClickListener(l);
or if its not exactly the same you could access the listener method by l.onClick(null); manually..