Example for interacting res / xml / prefs.xml with an activity.java

1

As much as I've searched, I've never found an exact example where a preference file interacts with a Java activity, for example:

private void Bemvindo(){
Toast.makeText(this, "Seja bem vindo", Toast.LENGHT_SHORT).show();
}

And in the preference file located in the folder res / xml will have this checkbox:

<CheckBoxPreference
                android:defaultValue="true"
                android:key="enable_boasvindas"
                android:title="Mostrar mensagem de boas vindas" />

How do you make the checkbox become a principal? If it has checked, it calls the Welcome method and Toast appears, otherwise it leaves the null method. Another thing; the Welcome method must be written to the main activity ( MainActivity.java ) or to the Settings.java preference activity)? If it's in the main activity, how do you call it from the Settings activity?

Thank you very much, thank you in advance! hugs.

    
asked by anonymous 20.07.2016 / 23:19

1 answer

0

To work with CheckBox, in your layout create a CheckBox:

<CheckBox
  android:id="@+id/checkBox"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Seu CheckBox"/>

In your MainActivity, instantiate the object:

public class MainActivity extends AppCompatActivity {
    private CheckBox checkBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.SEU_LAYOUT_XML);

         checkBox = (CheckBox) findViewById(R.id.checkBox);
         checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             // Se estiver selecionado então mostre a mensagem de boas vindas
             if (checkBox.isChecked()) 
                 benVindo();
           }
        });
    }
    private void bemVindo() {
         Toast.makeText(this, "Seja bem vindo", Toast.LENGHT_SHORT).show();
    }
}

As for your doubt about leaving the method coming on in this class or another class, go to your preference (Business rule), if you want the method strong> is in another class, you should call it this way:

 public class Configuracoes {
      public static void bemVindo(Context context) {
           Toast.makeText(context, "Seja bem vindo", Toast.LENGHT_SHORT).show();
      } 
 }

Now in your MainActivity:

public class MainActivity extends AppCompatActivity {
private CheckBox checkBox;

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.SEU_LAYOUT_XML);

     checkBox = (CheckBox) findViewById(R.id.checkBox);
     checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         // Se estiver selecionado então mostre a mensagem de boas vindas
         if (checkBox.isChecked())
             Configuracoes.bemVindo(MainActivity.this);
       }
    });
}

}

To save CheckBox's status, use SharedPreferences like this:

private void salvar(final boolean isChecked) {
     SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.commit();
}

private boolean carregar() { 
   SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
   return sharedPreferences.getBoolean("check", false);
}

// Você pode associar esse método ao click do botão salvar por exemplo, ou caso não queira
private void salvarStatusCheckBox() {
    save(checkBox.isChecked());
}

// No onResume você carrega o sharedPreferences
@Override
public void onResume() {
    super.onResume();
    checkBox.setChecked(load());
}
    
21.07.2016 / 19:10