Reputation: 1248
I'm trying to make a converter which can convert mile to km and vice versa, now the problem is whenever I'm clicking the button nothing is happening. The values just stayed the same whatsoever.
My Code is:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Distance extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.activity_distance, container, false);
Button button = (Button) v.findViewById(R.id.button12);
final EditText editText = (EditText) v.findViewById(R.id.edittext1);
final EditText editText2 = (EditText) v.findViewById(R.id.edittext2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editText.getText() != null && editText2.getText() == null) {
String s = editText.getText().toString();
Double d = Double.parseDouble(s) * 0.621371;
editText2.setText(d.toString());
} else if (editText2.getText() != null && editText.getText() == null) {
String s = editText2.getText().toString();
Double d = Double.parseDouble(s) / 0.621371;
editText.setText(d.toString());
}
}
});
return v;
}
}
Upvotes: 0
Views: 118
Reputation: 46
Button button = (Button) findViewById(R.id.button12);
final EditText editText = (EditText) findViewById(R.id.edittext1);
final EditText editText2 = (EditText) findViewById(R.id.edittext2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editText.getText().toString().equals("") && editText2.getText().toString().equals("")) {
String s = editText.getText().toString();
Double d = Double.parseDouble(s) * 0.621371;
editText2.setText(d.toString());
} else if (!editText2.getText().toString().equals("") && editText.getText().toString().equals("")) {
String s = editText2.getText().toString();
Double d = Double.parseDouble(s) / 0.621371;
editText.setText(d.toString());
}
}
});
This code is working successful.
Upvotes: 0
Reputation: 621
if (!editText.getText().toString().isEmpty() && editText2.getText() .toString().isEmpty() ) {
String s = editText.getText().toString();
Double d = Double.parseDouble(s) * 0.621371;
editText2.setText(d.toString());
} else if (!editText2.getText().toString().isEmpty() && editText.getText().toString().isEmpty()) {
String s = editText2.getText().toString();
Double d = Double.parseDouble(s) / 0.621371;
editText.setText(d.toString());
}
instead of using string.equals("") it is wise to use string.isEmpty()
Upvotes: 1
Reputation: 39
Change your if-else conditions to this.
if (!TextUtills.isEmpty(editText.getText().toString()) && !TextUtils.isEmpty(editText2.getText().toString)) {
}
Upvotes: 1
Reputation: 560
Rather than checking null, please check with the edittext values. Your code would be like this
if ((!TextUtils.isEmpty(editText.getText().toString()) && (TextUtils.isEmpty(editText2.getText().toString()))
Simillaraly modify another condition.
Upvotes: 1
Reputation: 3053
editText2.getText() == null and editText.getText() == null are always false because getText() can not be null here.
Upvotes: 1
Reputation: 5711
You have already initialized editText
and editText2
that's why your both if and else if condition not satisfied and your code doesn't respond .
final EditText editText = (EditText) v.findViewById(R.id.edittext1);
final EditText editText2 = (EditText) v.findViewById(R.id.edittext2);
And your conditions not matched in below code:
if (editText.getText() != null && editText2.getText() == null) {
String s = editText.getText().toString();
Double d = Double.parseDouble(s) * 0.621371;
editText2.setText(d.toString());
} else if (editText2.getText() != null && editText.getText() == null) {
String s = editText2.getText().toString();
Double d = Double.parseDouble(s) / 0.621371;
editText.setText(d.toString());
}
Upvotes: 0
Reputation: 66
Your button's callback is correctly called. You should check your conditions.
Upvotes: 1
Reputation: 6405
First declare globally:
EditText editText ;
EditText editText2;
Then in onCreateView:
editText = (EditText) v.findViewById(R.id.edittext1);
editText2 = (EditText) v.findViewById(R.id.edittext2);
Finally, Use this code:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editText.getText().toString().equals("") && editText.getText().toString().equals("")) {
String s = editText.getText().toString();
Double d = Double.parseDouble(s) * 0.621371;
editText2.setText(d.toString());
} else if (editText2.getText().toString().equals("") && editText.getText().toString().equals("")) {
String s = editText2.getText().toString();
Double d = Double.parseDouble(s) / 0.621371;
editText.setText(d.toString());
}
}
});
Hope this helps.
Upvotes: 1
Reputation: 16577
Change your if-else conditions to this.
if (!editText.getText().toString().equals("") && editText2.getText().toString.equals("")) {
And similarly the other.
Upvotes: 2