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, install 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 in leaving the Bemcoming method in this class or in another class, it goes of your preference (Business rule), if you want the method Welcome() is in another class, you should call it that 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 the "status" of the Checkbox use Sharedpreferences this way:
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());
}
I think that’s not what was asked. He’s talking about Checkboxpreference.
– Pablo Almeida
Boooooa mano, thank you very much. On second thought, instead of calling the screen of preference /xml/prefs.xml, I will create a layout and follow this example. Thank you. Now only lacked to implement Sharedpreference to leave the option always checked or unchecked, rs. You could implement?
– Tech Positivo
I will add to my reply the part of Sharedpreference. @Techpositivo
– Leonardo Rocha
@Leonardorocha Amigo, I don’t know how to thank you. Thank you from my heart. So, before seeing your answer, I had already searched and found a method to save the status of the checkbox. It is saving beauty, but the message of good only appears when is marked or unchecked the box, IE, when I close and open the application again does not appear. Does your code fix this? Specifically I think it’s onResume, right?
– Tech Positivo
@Leonardorocha I did everything as written! Only that gave to realize that the 'onResume' method gives a reset in the marked options.
– Tech Positivo
@Techpositivo you want the "Welcome" message to be shown only at what times?
– Leonardo Rocha
@Leonardorocha Always when the application is open, or onResume() but already get bro. I created several Checkbox’s and put different keys to each of them. Thank you very much, you were super helpful.
– Tech Positivo