1

I've been developing an Android app that allows users to track budget spending records. Since this is a budget spending tracker, I want users to select their currencies. In this case, I have three currencies: US Dollar, Japanese Yen, and Euro. To enable it, I created three classes: BudgetTrackerSettingsActivity, CurrencySpinnerAdapter, and Currency. The default currency is US Dollar. And I want it to display the selected currency symbols, such as $,¥, and €, which are Vector Assets, at the beggining of EditText.

Since Vector Assets themselves can't be handled by SharedPreferences, I used an if-else statement to branch the execution to display the currency symbols in EditText.

The problem is that it temporarily changes the currency symbol. But when I restart the app, the change goes back to the default settings and the currency is always US Dollar. How can I make it remain in the settings permanently unless the user changes it again?

BudgetTrackerSettingsActivity.java

package com.myproject.offlinebudgettrackerappproject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.myproject.offlinebudgettrackerappproject.adapter.CurrencySpinnerAdapter;

public class BudgetTrackerSettingsActivity extends AppCompatActivity {

    Spinner spinner;
    public static String spinnerText;

    Button currencyBtn;
    String selectedCurrency;
    String finalCurrency;
    private static final String PREF_CURRENCY_VALUE = "currencyValue";

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

        spinner = (Spinner) findViewById(R.id.currency_spinner);
        currencyBtn = (Button) findViewById(R.id.currency_btn);

        CurrencySpinnerAdapter currencySpinnerAdapter = new CurrencySpinnerAdapter(this, R.layout.currency_spinner_adopter,
                Currency.getCurrencyArrayList());
        spinner.setAdapter(currencySpinnerAdapter);

        final SharedPreferences sharedPreferences = getSharedPreferences("CURRENCY_SHARED", 0);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                Currency currency = (Currency) spinner.getSelectedItem();
                spinnerText = currency.getCurrency();
                if (spinnerText == "US Dollars") {
                    Toast.makeText(BudgetTrackerSettingsActivity.this, "US Dollars", Toast.LENGTH_SHORT).show();
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "US_DOLLAR";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                } else if (spinnerText == "Japanese Yen") {
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "JAPANESE_YEN";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                } else {
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "EURO";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.putInt(PREF_CURRENCY_VALUE, 2);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                }
                Log.d("Currency", "onCreate: " + spinnerText);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                spinnerText = null;
            }
        });
    }


}

CurrencySpinnerAdapter.java

package com.myproject.offlinebudgettrackerappproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.myproject.offlinebudgettrackerappproject.Currency;
import com.myproject.offlinebudgettrackerappproject.R;

import java.util.List;

public class CurrencySpinnerAdapter extends ArrayAdapter<Currency> {

    LayoutInflater layoutInflater;

    public CurrencySpinnerAdapter(@NonNull Context context, int resource, @NonNull List<Currency> currencies) {
        super(context, resource, currencies);
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View rowView = layoutInflater.inflate(R.layout.currency_spinner_adopter, null, true);
        Currency currency = getItem(position);
        TextView textView = (TextView) rowView.findViewById(R.id.currency_name_textview);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.currency_symbol);
        textView.setText(currency.getCurrency());
        imageView.setImageResource(currency.getCurrencyImage());
        return rowView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.currency_spinner_adopter, parent, false);
        }

        Currency currency = getItem(position);
        TextView textView = (TextView) convertView.findViewById(R.id.currency_name_textview);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.currency_symbol);
        textView.setText(currency.getCurrency());
        imageView.setImageResource(currency.getCurrencyImage());

        return convertView;
    }
}

Currency.java

package com.myproject.offlinebudgettrackerappproject;

import java.util.ArrayList;

public class Currency {

    private static ArrayList<Currency> currencyArrayList = new ArrayList<>();

    private String id;
    private String currency;

    public Currency(String id, String currency) {
        this.id = id;
        this.currency = currency;
    }

    public Currency() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public static void initCurrencies() {
        Currency usd = new Currency("0", "US Dollars");
        currencyArrayList.add(usd);

        Currency jpy = new Currency("1", "Japanese Yen");
        currencyArrayList.add(jpy);

        Currency euro = new Currency("2", "Euro");
        currencyArrayList.add(euro);


    }

    public int getCurrencyImage() {
        switch (getId()) {
            case "0":
                return R.drawable.ic_money;
            case "1":
                return R.drawable.ic_yen;
            case "2":
                return R.drawable.ic_euro;
        }
        return R.drawable.ic_money;
    }

    public static ArrayList<Currency> getCurrencyArrayList() {
        return currencyArrayList;
    }
}

HomeFragment.java (This is the class that receives the changed settings)

final SharedPreferences sharedPreferences = getActivity().getSharedPreferences("CURRENCY_SHARED", 0);;

        //if-else branch to select currency
        String currentCurrency = sharedPreferences.getString("CURRENCY", "");

        currentCurrencyTv.setText(currentCurrency);
        if (currentCurrency == "US Dollars") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_money, 0, 0, 0);
        } else if (currentCurrency == "Japanese Yen") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_yen, 0, 0, 0);
        } else if (currentCurrency == "Euro") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_euro, 0, 0, 0);
        }

0 Answers0