I'm trying to have a view show the color of a setting option with the view set as a widget on the setting button, I can make the view change color after the preference has been created but not during.
//Part of the setting class
the error im getting is "Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference"
If i stick the "View colorView = getView().findViewById(R.id.color_view);" inside a click listerner for example it works, what am i doing wrong?
public static class ThemeFragment extends PreferenceFragmentCompat {
Context context;
private int defaultColor; // Assuming this is a class-level variable
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.theme_prefernces, rootKey);
//the id of the view
View colorView = getView().findViewById(R.id.color_view);
Preference backGroundColorPreference = findPreference("background_color_preference");
Preference buttonColorPreference = findPreference("button_color_preference");
Preference letterColorPreference = findPreference("letter_color_preference");
// the method that changes the color of the view
SharedPreferences btColorPreference = PreferenceManager.getDefaultSharedPreferences(requireContext());
int btColor = btColorPreference.getInt("button_color_preference", 50);
colorView.setBackgroundColor(btColor);
if (backGroundColorPreference != null){
backGroundColorPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
// Handle preference click here
showColorPickerDialog("background_color_preference");
return true; // Return true if the click was handled
}
});
}
if (buttonColorPreference != null) {
buttonColorPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
// Handle preference click here
showColorPickerDialog("button_color_preference");
return true; // Return true if the click was handled
}
});
}
if (letterColorPreference != null) {
letterColorPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
// Handle preference click here
showColorPickerDialog("letter_color_preference");
return true; // Return true if the click was handled
}
});
}
}
private void showColorPickerDialog(String pref) {
AmbilWarnaDialog ambilWarnaDialog = new AmbilWarnaDialog(requireContext(), defaultColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
// Handle cancellation
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
// Update the variable with the selected color
defaultColor = color;
// Save the selected color to SharedPreferences
saveColorToSettings(pref, color);
}
});
ambilWarnaDialog.show();
}
private void saveColorToSettings(String pref, int color) {
// Save the selected color to SharedPreferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(requireContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(pref, color); // Use the correct key
editor.apply();
}
}
public static class FontFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.font_preferences, rootKey);
}
}
public static class ServerFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.server_preferences, rootKey);
}
}
public static class DataBaseFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.database_preferences, rootKey);
}
}
public void toolbar(){
setSupportActionBar(toolbar);
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
}
//the xml of the theme_preference
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:title="Different Color Selector">
<Preference
android:key="background_color_preference"
android:title=" Main Page Background Color"
android:summary="Change the background color"/>
<Preference
android:key="button_color_preference"
android:title="Main Page Button Color"
android:summary="Change the color of buttons"
android:widgetLayout="@layout/button_color_layout"/>
<Preference
android:key="letter_color_preference"
android:title="Main Page Writing Color"
android:summary="Change the color of the writing"/>
</PreferenceCategory>
</PreferenceScreen>
//the button_layout_preference
<!-- res/layout/button_color_layout.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Add other UI elements if needed -->
<!-- Color view -->
<View
android:id="@+id/color_view"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@color/colorButton"/>
</LinearLayout>