Reputation: 465
I'm trying to use snackbar with custom xml in my preferences activity. And i'm doing something wrong, because i get a nullpointer exception. I think it has something to do with the view, or maybe it`s because i don't use coordinator layout. Here is my Java code:
@Override
public boolean onPreferenceChange(Preference preference, final Object newValue) {
String key = preference.getKey();
if (key.equals("distance")) {
preference.setSummary((String) newValue);
Snackbar snackbar = Snackbar
.make(findViewById(R.id.snackbarPosition), "You choose " + newValue, Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
if (newValue.equals("km")){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyPrefs.this);
SharedPreferences.Editor editor = sp.edit();
editor.putString("distance", "miles");
editor.commit();
}else if (newValue.equals("miles")){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyPrefs.this);
SharedPreferences.Editor editor = sp.edit();
editor.putString("distance", "km");
editor.commit();
}
}
});
snackbar.show();
Snackbar xml:
<?xml version="1.0" encoding="utf-8"?>
<View
android:id="@+id/sb__divider"
android:layout_width="match_parent"
android:layout_height="3dp" />
<LinearLayout
android:id="@+id/sb__inner"
android:background="@color/transparent_black_percent_70"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/sb__text"
style="@style/Snackbar.Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="You choose blah blah"
android:ellipsize="end"
android:focusable="true" />
<Button
android:id="@+id/sb__action"
style="@style/Snackbar.Text.Action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="undo"
android:gravity="right"
android:focusable="true" />
</LinearLayout>
Preferences activity:
<?xml version="1.0" encoding="utf-8"?>
<ListPreference
android:dialogTitle="Select a distance system"
android:entries="@array/distanceEntries"
android:entryValues="@array/distanceValues"
android:key="distance"
android:title="Distance" />
Upvotes: 0
Views: 1487
Reputation: 8794
Gigi Sprintzin's link provides the answer you need to show a Snack Bar in the preferences activity. I've adapted the linked code to your exact problem:
@Override
public boolean onPreferenceChange(Preference preference, final Object newValue) {
View root = SettingsPreferenceScreen.this.getListView();
String key = preference.getKey();
if (key.equals("distance")) {
preference.setSummary((String) newValue);
Snackbar snackbar = Snackbar
.make(root, "You choose " + newValue, Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
if (newValue.equals("km")){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyPrefs.this);
SharedPreferences.Editor editor = sp.edit();
editor.putString("distance", "miles");
editor.commit();
}else if (newValue.equals("miles")){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyPrefs.this);
SharedPreferences.Editor editor = sp.edit();
editor.putString("distance", "km");
editor.commit();
}
}
});
snackbar.show();
}
}
Of course, you must change View root = SettingsPreferenceScreen.this.getListView();
to use the name of your preferences class.
Upvotes: -1
Reputation: 465
To get the parent layout in Preferences Activity one should use getListView(). From here: https://stackoverflow.com/a/8432096/5342189
Upvotes: 5
Reputation: 3873
Try This:
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Had a snack at Snackbar", Snackbar.LENGTH_LONG)
.setAction("Undo", mOnClickListener);
snackbar.setActionTextColor(Color.WHITE);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.DKGRAY);
TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
Upvotes: 0