Alpit Anand
Alpit Anand

Reputation: 1248

Button is not doing anything

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

Answers (9)

Dinesh Shingadiya
Dinesh Shingadiya

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

Digvijay Singh
Digvijay Singh

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

bayoudh Mohamed
bayoudh Mohamed

Reputation: 39

Change your if-else conditions to this.

if (!TextUtills.isEmpty(editText.getText().toString()) && !TextUtils.isEmpty(editText2.getText().toString)) {

}

Upvotes: 1

Sandeep Kharat
Sandeep Kharat

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

Imene Noomene
Imene Noomene

Reputation: 3053

editText2.getText() == null and editText.getText() == null are always false because getText() can not be null here.

Upvotes: 1

Chetan Joshi
Chetan Joshi

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

HkBrc
HkBrc

Reputation: 66

Your button's callback is correctly called. You should check your conditions.

Upvotes: 1

tahsinRupam
tahsinRupam

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

mehulmpt
mehulmpt

Reputation: 16577

Change your if-else conditions to this.

if (!editText.getText().toString().equals("") && editText2.getText().toString.equals("")) {

And similarly the other.

Upvotes: 2

Related Questions