jeudi 9 août 2018

How to use checkbox to determine show or skip onboarding screen?

I've got a issue that I don't have a clue how to save checkbox state and use it in sharedpreferences to display or skip onboarding (walkthrough) screen to my application. I have made to make display onboarding screen only at first launch of the app, but that's not what I'm looking for. I need to use checkbox to allow user choose if they want to display it again at launch or not.

Here's my onboarding screen layout and class.

<CheckBox
    android:id="@+id/radiobtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/prevBtn"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="40dp"
    android:buttonTint="@color/white"
    android:checked="false"
    android:text="do not show again"
    android:textColor="@color/white"
    android:textSize="12sp" />

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    final SharedPreferences.Editor editor = preferences.edit();
    if(preferences.contains("checked") && preferences.getBoolean("checked", false) == true){
        radioButton.setChecked(true);
    }else{
        radioButton.setChecked(false);
    }

    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(radioButton.isChecked()){
                editor.putBoolean("checked", true);

            }
        }

I know It might have no sense cus I'm trying on my own. I don;t see any example to practice.

Here's my sharedpreferences to skip onboarding screen at second launch. This code belongs to MainActivity which is displayed after onboarding activity.

Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);

    if (isFirstRun) {
        //show sign up activity
        startActivity(new Intent(MainActivity.this, Preshow.class));
        finish();
    }


    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
            .putBoolean("isFirstRun", false).commit();

I'll be appreciated if you write me how to make onboarding screen to not show again if user checks checkbox which is placed in onboarding screen activity.

Onboarding screen




Aucun commentaire:

Enregistrer un commentaire