0

I would like the background colour to change to red on the StartStop CardView. It is currently green. How do I do this?

Unfortunately StartStopCard.setCardBackgroundColor(Color.parseColor("#b70505")); will not work.

I have attached my whole code below

public class LandingPage extends AppCompatActivity implements View.OnClickListener {

    private CardView appsCard, parentalControlsCard, customSettingsCard, activateCard, StartStopCard;
    private TextView lockStatus, processStatus;
    private AlarmManager alarmManager;
    private ArrayList<RuleSet> ruleSets = null;
    private boolean mStarted = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);
        lockStatus = (TextView) findViewById(R.id.onOff);
        processStatus = (TextView) findViewById(R.id.processStartStop);


        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        final Intent intent = new Intent(LandingPage.this, LockOptionReceiver.class);
        SharedPreferences setting = getSharedPreferences("PREFS", 0);
        String switcher = setting.getString("lockStatus", "");
        setStatus(switcher);
        String switcher2 = setting.getString("processStatus" , "");
        setProcess(switcher2);


        try {
            ruleSets = RuleSetList.retrieveRuleSet(this);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        //Defining Cards on Landing Page
        appsCard = (CardView) findViewById(R.id.apps_card);
        parentalControlsCard = (CardView) findViewById(R.id.parentalControls_id);
        customSettingsCard = (CardView) findViewById(R.id.customSettings);
        activateCard = (CardView) findViewById(R.id.activate_id);
        StartStopCard = (CardView) findViewById(R.id.StartStopCard);


        //Add OnClick Listeners
        appsCard.setOnClickListener(this);
        parentalControlsCard.setOnClickListener(this);
        customSettingsCard.setOnClickListener(this);
        activateCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ruleSets.isEmpty()) {
                    Toast.makeText(LandingPage.this, "You did not create a custom setting.", Toast.LENGTH_SHORT).show();
                } else {
                    PendingIntent pending_start;
                    PendingIntent pending_stop;
                    Intent startIntent = new Intent(LandingPage.this, LockOptionReceiver.class);
                    Intent stopIntent = new Intent(LandingPage.this, LockOptionReceiver.class);
                    Calendar startTime = new GregorianCalendar();
                    Calendar endTime = new GregorianCalendar();
                    String startString = ruleSets.get(0).getStartTime();
                    String endString = ruleSets.get(0).getEndTime();

                    String[] startArr = startString.split(":");
                    String[] endArr = endString.split(":");

                    startTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startArr[0]));
                    startTime.set(Calendar.MINUTE, Integer.parseInt(startArr[1]));

                    endTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endArr[0]));
                    endTime.set(Calendar.MINUTE, Integer.parseInt(endArr[1]));

                    pending_start = PendingIntent.getBroadcast(LandingPage.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    pending_stop = PendingIntent.getBroadcast(LandingPage.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                    startIntent.putExtra("status", "start");
                    stopIntent.putExtra("status", "stop");

                    Toast.makeText(LandingPage.this, "Your ruleset will start at " + startString, Toast.LENGTH_SHORT).show();

                    setStatus("Lock Active");
                    setProcess("Stop");

                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, startTime.getTimeInMillis(), pending_start);
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, startTime.getTimeInMillis(), pending_stop);
                }
            }
        });


        final CardView StartStopCard = (CardView) findViewById(R.id.StartStopCard);
        StartStopCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mStarted) {
                    mStarted=false;
                    SharedPreferences setting = getSharedPreferences("PREFS", 0);
                    SharedPreferences.Editor editor = setting.edit();
                    editor.remove("switcher");
                    editor.remove("switcher2");
                    editor.remove("lockStatus");
                    editor.remove("processStatus");
                    editor.commit();
                    editor.putString("switcher", "true");
                    editor.putString("switcher2", "true");
                    editor.putString("lockStatus", "Lock Active");
                    editor.putString("processStatus", "Start");
                    editor.apply();
                    intent.putExtra("status", "start");
                    sendBroadcast(intent);
                    intent.putExtra("processStatus", "start");
                    sendBroadcast(intent);
                    String status = setting.getString("lockStatus", "");
                    setStatus(status);
                    String processStatus = setting.getString("processStatus" , "");
                    setProcess(processStatus);

                }
                else {

                    mStarted= true;
                    SharedPreferences setting = getSharedPreferences("PREFS", 0);
                    SharedPreferences.Editor editor = setting.edit();
                    editor.remove("switcher");
                    editor.remove("switcher2");
                    editor.remove("lockStatus");
                    editor.remove("processStatus");
                    editor.commit();
                    editor.putString("switcher", "false");
                    editor.putString("switcher2", "false");
                    editor.putString("lockStatus", "Lock Deactivated");
                    editor.putString("processStatus", "Stop");
                    editor.apply();
                    intent.putExtra("status", "stop");
                    sendBroadcast(intent);
                    intent.putExtra("processStatus", "start");
                    sendBroadcast(intent);
                    String status = setting.getString("lockStatus", "");
                    setStatus(status);
                    String processStatus = setting.getString("processStatus" , "");
                    setProcess(processStatus);
                    StartStopCard.setCardBackgroundColor(Color.parseColor("#b70505"));

                }
            }

        });

    }






    @Override
    public void onClick(View v) {

    Intent i;

    switch (v.getId()) {
        case R.id.apps_card: i = new Intent(this,AppList.class);startActivity(i);break;
        case R.id.parentalControls_id:i = new Intent(this,ParentalWelcomeActivity.class);startActivity(i);break;
        case R.id.customSettings:i = new Intent(this,ViewRuleSets.class);startActivity(i);break;
        case R.id.activate_id:i = new Intent(this, RuleSet.class);startActivity(i);break;
        case R.id.StartStopCard:?????
        default:break;

    }
}

Any help at all would be hugely appreciated thank you very much

user12345
  • 55
  • 7

2 Answers2

0

Create your startStopCard view or any other view you want to change it's color for example (a layout background) and then instantiate it in your onCreate, then in your onClick switch case you can set its color by: startStopCard.setCardBackgroundColor(getResources().getColor(R.color.red));

M.FD
  • 83
  • 1
  • 7
  • Thanks, but how do I set the switch case? Something like this? case R.id.StartStopCard = startStopCard.setCardBackgroundColor(getResources().getColor(R.color.samsMoving)); – user12345 Mar 08 '18 at 15:03
  • This is how your case should be : case R.id.StartStopCard: startStopCard.setCardBackgroundColor(getResources().getColor(R.color.samsMoving)); break; – M.FD Mar 08 '18 at 15:07
  • It is still not working unfortunately will I post whole code? – user12345 Mar 08 '18 at 15:12
  • which view do you want to change its background is it the cardview's background or the activity's background ? – M.FD Mar 08 '18 at 15:16
  • it is the cardviews bg – user12345 Mar 08 '18 at 15:22
  • Does the click action of the other cardviews working fine ? and check if you setClicklistener to startStopCard ? something like this startStopCard.setOnClickListener(this); – M.FD Mar 08 '18 at 15:28
  • I have set an OnClick listener for the StartStopCard in a different area. Should I change the background color under that part? – user12345 Mar 08 '18 at 22:09
  • What do you mean by different area? you can just set the onclicklistener after calling the findviewbyid of StartStopCard. – M.FD Mar 09 '18 at 09:26
0

If R.id.StartStopCard id represent a cardView than do as -

@Override
    public void onClick(View v) {

    Intent i;

    switch (v.getId()) {
        case R.id.apps_card: i = new Intent(this,AppList.class);startActivity(i);break;
        case R.id.parentalControls_id:i = new Intent(this,ParentalWelcomeActivity.class);startActivity(i);break;
        case R.id.customSettings:i = new Intent(this,ViewRuleSets.class);startActivity(i);break;
        case R.id.activate_id:i = new Intent(this, RuleSet.class);startActivity(i);break;
        case R.id.StartStopCard: ((CardView)v).setCardBackgroundColor((getResources().getColor(R.color.red)));
        default:break;

    }
}
Sanjay Kumar
  • 1,135
  • 14
  • 27