[Solved] How to convert a value in a text field (kg to lbs) using a toggle or switch button [closed]


You can’t. You have to implement it yourself by code.

Today I’m in a good mood and I’m bored, so I will write some code for you, even if you made like no effort for it.

In shorts, add the switch and the TextView/EditText in your XML, then implement in your code something like this:

Switch mySwitch;
TextView mTextView;

mSwitch = findViewById(R.id.mSwitch);
mTextView = findViewById(R.id.mTextView);

double kg = 0.0;
double lbs = 0.0;
final double change = 2.2046226218; //this is the change between kg and lbs, 1kg = this amount of lbs

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean convertToKg) {
        double currentValue = Double.parseDouble(mTextView.getText().toString());
        if(convertToKg){
            mTextView.setText(String.valueOf(currentValue / change));
        }else{
            mTextView.setText(String.valueOf(currentValue * change));
        }
    }
});

hope this helps

solved How to convert a value in a text field (kg to lbs) using a toggle or switch button [closed]