dimanche 17 septembre 2017

Calculate currency exchange using checkboxes

I've just started learning Java in Android Studio and now I'm doing a simple currency exchange app. It has checkboxes indicating different countries with different currency values. Now I have a problem saying it crashed when i run the emulator. (It crashes only when I implemented the calculation inside case US). It works fine displaying only the strings though.

Java file:

public class MainActivity extends AppCompatActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_result;
EditText mEdit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final_result = (TextView)findViewById(R.id.result_text);
    final_result.setEnabled(false);
}

public void selectCountry (View view)
{
    boolean checked = ((CheckBox) view).isChecked();

    switch (view.getId())
    {
        case R.id.US:
            if(checked) {
                mEdit = (EditText)findViewById(R.id.edit_myr);
                Double output = Double.valueOf(mEdit.getText().toString());
                double s;
                s=output*4.25;
                String output2=String.valueOf(s);
                selection.add(output2);
            }
            else
            {
                selection.remove("United States");
            }
            break;
        case R.id.AU:
            if(checked) {
                selection.add("Australia");
            }
            else
            {
                selection.remove("Australia");
            }
            break;
        case R.id.UK:
            if(checked) {
                selection.add("United Kingdom");
            }
            else
            {
                selection.remove("United Kingdom");
            }
            break;
        case R.id.JAP:
            if(checked) {
                selection.add("Japan");
            }
            else
            {
                selection.remove("Japan");
            }
            break;
        case R.id.CH:
            if(checked) {
                selection.add("China");
            }
            else
            {
                selection.remove("China");
            }
            break;
        case R.id.SG:
            if(checked) {
                selection.add("Singapore");
            }
            else
            {
                selection.remove("Singapore");
            }
            break;
        case R.id.IN:
            if(checked) {
                selection.add("India");
            }
            else
            {
                selection.remove("India");
            }
            break;
        case R.id.TH:
            if(checked) {
                selection.add("Thailand");
            }
            else
            {
                selection.remove("Thailand");
            }
            break;
    }
}
public void finalSelection (View view) {
    String final_selection = "";

    for(String Selections : selection)
    {
        final_selection = final_selection + Selections + "\n";
    }
    final_result.setText(final_selection);
    final_result.setEnabled(true);
}

XML:

    <EditText
    android:id="@+id/edit_myr"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/myr"
    android:layout_marginRight="40dp"
    android:backgroundTint="@android:color/holo_red_light"/>
</RelativeLayout>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/third"
    android:textSize="20sp"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="20dp"
    android:textColor="#000000"/>
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Convert"
    android:layout_gravity="center_horizontal"
    android:onClick="finalSelection"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/result_text"
    android:layout_gravity="center_horizontal"/>

my US checkbox in XML is:

        <CheckBox
        android:id="@+id/US"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:onClick="selectCountry"


        android:drawableLeft="@drawable/united_states"/>




Aucun commentaire:

Enregistrer un commentaire