Search code examples
javaandroidspinnercurrencymultiplication

Android Studio multiple numbers with spinner selection


So I'm attempting to have a spinner where you select a currency to convert to from GBP, enter a value in GBP and convert to the selected currency from the spinner by pressing a button. The converted value will then appear in the textview below

enter image description here

Here is the following code I have in the Convert activity i'm using, the app is crashing upon trying to switch to this layout from the main menu, however it was working before i tried adding the multiplication code. Thanks in advance.

public class Convert extends AppCompatActivity {


final EditText currency_input = (EditText) findViewById(R.id.editText_currency_input);
final TextView answer = (TextView) findViewById(R.id.textView_convert_to);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.convert);

    Spinner spinner_convert_from = (Spinner) findViewById(R.id.spinner_convert_from);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.currency_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_convert_from.setAdapter(adapter);
}



private void USD() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))*1.2798));
}

private void EUR() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))*1.14502));
}

private void AUD() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))*1.71911));
}

private void CAD() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))*1.7226));
}

private void JPY() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))*142.482));
}

private void CHF() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))* 1.24662));
}

private void CNY() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))* 8.7714));
}

private void KRW() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))*1430.8));
}

private void SEK() {
    answer.setText(String.valueOf(Double.valueOf(String.valueOf(currency_input.getText()))* 11.1187));
}


public class planOnClickListener implements AdapterView.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos,
                               long id) {
        parent.getItemAtPosition(pos);

        if (pos == 0) {
            USD();
        } else if (pos == 1) {
            EUR();
        } else if (pos == 2) {
            AUD();
        } else if (pos == 3) {
            CAD();
        } else if (pos == 4) {
            JPY();
        } else if (pos == 5) {
            CHF();
        } else if (pos == 6) {
            CNY();
        } else if (pos == 7) {
            KRW();
        } else if (pos == 8) {
            SEK();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }
}

}


Solution

  • There are some common errors in your codes. I have updated your code.

    Here is the working code:

    //Convert.java
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.TextView;
    
    public class Convert extends AppCompatActivity {
    
        EditText currency_input ;
        TextView answer;
    
        String input;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.convert);
    
            // Views
            currency_input = (EditText) findViewById(R.id.editText_currency_input);
            answer = (TextView) findViewById(R.id.textView_convert_to);
    
            // Default value
            currency_input.setText("0.0");
    
            Spinner spinner_convert_from = (Spinner) findViewById(R.id.spinner_convert_from);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.currency_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner_convert_from.setAdapter(adapter);
    
            // Add item selected listener
            spinner_convert_from.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View v, int pos,
                                           long id) {
    
                    // Get input text 
                    input = currency_input.getText().toString();
    
                    if (pos == 0) {
                        USD();
                    } else if (pos == 1) {
                        EUR();
                    } else if (pos == 2) {
                        AUD();
                    } else if (pos == 3) {
                        CAD();
                    } else if (pos == 4) {
                        JPY();
                    } else if (pos == 5) {
                        CHF();
                    } else if (pos == 6) {
                        CNY();
                    } else if (pos == 7) {
                        KRW();
                    } else if (pos == 8) {
                        SEK();
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
    
                }
            });
    
        }
    
        private void USD() {
            answer.setText(String.valueOf(Double.valueOf(input)*1.2798));
        }
    
        private void EUR() {
            answer.setText(String.valueOf(Double.valueOf(input)*1.14502));
        }
    
        private void AUD() {
            answer.setText(String.valueOf(Double.valueOf(input)*1.71911));
        }
    
        private void CAD() {
            answer.setText(String.valueOf(Double.valueOf(input)*1.7226));
        }
    
        private void JPY() {
            answer.setText(String.valueOf(Double.valueOf(input)*142.482));
        }
    
        private void CHF() {
            answer.setText(String.valueOf(Double.valueOf(input)* 1.24662));
        }
    
        private void CNY() {
            answer.setText(String.valueOf(Double.valueOf(input)* 8.7714));
        }
    
        private void KRW() {
            answer.setText(String.valueOf(Double.valueOf(input)*1430.8));
        }
    
        private void SEK() {
            answer.setText(String.valueOf(Double.valueOf(input)* 11.1187));
        }
    }
    

    Here is your layout XML:

    // convert.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="16dp"
        tools:context="com.ferdous.stackoverflowanswer.Convert">
    
        <TextView
            android:id="@+id/textView_convert_to"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24dp"/>
    
        <Spinner
            android:id="@+id/spinner_convert_from"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp">
    
        </Spinner>
    
        <EditText
            android:id="@+id/editText_currency_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"/>
    
    </LinearLayout>
    

    OUTPUT:

    enter image description here