Example to interact res/xml/prefs.xml with a.java activity.

Asked

Viewed 72 times

1

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

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

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

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

How do you make for the checkbox be in charge? If he has scored, call the method Welcome and appears the Toast, otherwise leaves the method void. Another thing; the method Welcome must be written in the main activity (Mainactivity.java) or in the activity of preference (Java settings.)? If it is in the main activity, how to call it from the activity Settings?

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

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, 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.

  • 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?

  • 1

    I will add to my reply the part of Sharedpreference. @Techpositivo

  • @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?

  • @Leonardorocha I did everything as written! Only that gave to realize that the 'onResume' method gives a reset in the marked options.

  • @Techpositivo you want the "Welcome" message to be shown only at what times?

  • @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.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.