2

I am currently using a CardView as a Start Button in an activity. However once I have clicked the CardView I need the bg color to change along with the text and function to my STOP activity.

  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);
        StartStopCard = (CardView) findViewById(R.id.StartStopCard);

        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);
        StartStopCard.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);

                }
            }

        });

    }



    @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;
            default:
                break;

        }
    }



    private void setStatus(String s) {
        lockStatus.setText(s);
    }

    private void setProcess(String s) { processStatus.setText(s);}


    public void ruleSet(View view) {
        Intent intent = new Intent(this, ViewRuleSets.class);
        startActivity(intent);
    }


}

I am currently using a CardView as a Start Button in an activity. However once I have clicked the CardView I need the bg color to change along with the text and function to my STOP activity.

user12345
  • 55
  • 7

1 Answers1

0

Set a class member variable called mStarted, for example, and set it initially to `false'.

private boolean mStarted = false;

Now in your onClick() listener, flip the value of this variable then check check it to determine a course of action.

CardView startStopCard = (CardView) findViewById(R.id.StartStopCard);
startStopCard .setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mStarted = !mStarted;
        if (mStarted) {
            // do start stuff 
        } else {
            // do stop stuff
        }
    }
}
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • Do i place the stop CardView anywhere? – user12345 Mar 05 '18 at 16:55
  • @user12345 Don't you want just one `CardView`? The code will work with a single `CardView` and you don't need separate start/stop views. Comment back if I misunderstood. – Cheticamp Mar 05 '18 at 16:57
  • sorry yes I do want just one cardview. However, this function is inside an onCreate method, so will I need to override it with public void onClick? – user12345 Mar 05 '18 at 16:59
  • Thank you very much. Do you know how I change the colour and text for stop here? Plz – user12345 Mar 05 '18 at 17:10
  • @user12345 Depends on what is in you `CardView`. Are you changing the background color, some text or something else. Post your XML or the code that creates the `CardView`. That will help. – Cheticamp Mar 05 '18 at 17:13
  • Posted the XML. I am looking to change bg color to red and text to STOP . Thanks – user12345 Mar 05 '18 at 17:18
  • @user12345 To change the text view text, do a `findViewById()` then you can use [`setText()`](https://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)). To change the card view's bg color use [`setBackgroundColor()`](https://developer.android.com/reference/android/view/View.html#setBackgroundColor(int)). – Cheticamp Mar 05 '18 at 17:54
  • @user12345 Glad you figured it out. – Cheticamp Mar 05 '18 at 18:37
  • Hi @cheticamp sorry but i have other question pls. I cannot change the color or text. However I wonder should it be changed in onClick statement? I have posted the code above sorry for inconvenience – user12345 Mar 08 '18 at 14:59
  • @user12345 Yes, the `onClick()` method is where your should make the changes if they are in response to a click. – Cheticamp Mar 08 '18 at 17:28
  • I have posted all of my code have you any idea? I figured out how to make the text in the cardview change. Just not the function or bg color I feel i have tried everything – user12345 Mar 08 '18 at 18:56