package com.example.layouts;

import java.text.NumberFormat;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TemperatureActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.combinedlayout);

        // Make local variables final because they are used within a
        // local inner class
        final Button fahrButton =
                (Button) findViewById(R.id.fahrButton);
        final Button celButton =
                (Button) findViewById(R.id.celsiusButton);
        final EditText temperature =
                (EditText) findViewById(R.id.temperature);
        final TextView result = (TextView) findViewById(R.id.result);

        // Listener class that can convert both F->C and C->F
        class MyButtonListener implements OnClickListener {
            public void onClick(View view) {
                Resources res = getResources();
                // Retrieve the temperature to convert from
                String tempStr = temperature.getText().toString();

                try {
                    // if the button clicked was the F to C button
                    if (((Button) view).getText().equals(
                            fahrButton.getText())) {
                        double f = Double.parseDouble(tempStr);
                        double c = (5.0 / 9.0) * (f - 32);
                        String cStr =
                                NumberFormat.getInstance().format(c);
                        String text =
                                String.format(
                                        res.getString(R.string.f2c_result),
                                        tempStr, cStr);
                        result.setText(text);
                    }
                    // if the button was the C to F button
                    else if (((Button) view).getText().equals(
                            celButton.getText())) {
                        double c = Double.parseDouble(tempStr);
                        double f = (9.0 / 5.0) * c + 32;
                        String fStr =
                                NumberFormat.getInstance().format(f);
                        String text =
                                String.format(
                                        res.getString(R.string.c2f_result),
                                        tempStr, fStr);
                        result.setText(text);
                    }
                }
                catch (NumberFormatException nfe) {
                    result.setText(getString(R.string.error));
                }
            }
        }

        // Register the listener with both buttons
        MyButtonListener listener = new MyButtonListener();
        fahrButton.setOnClickListener(listener);
        celButton.setOnClickListener(listener);
    }
}