Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Snackbar in preference fragement

I want to add a basic snackbar to my application but I have received an error that I couldn't figure out why. I think the problem is getView(), may be that I must use getListView() but I am in a static context so how?

Here is my Java code:

public class SettingsFragment extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction().replace(android.R.id.content,
                new PrefsFragment()).commit();
        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4CAF50")));

    }


    public static class PrefsFragment extends PreferenceFragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.settings);

            Preference save_database = findPreference("savedatabase");
            save_database.setOnPreferenceClickListener( new Preference.OnPreferenceClickListener()
            {
                public boolean onPreferenceClick( Preference pref )
                {
                    Data.bdd.Save_database(getActivity(),getView());
                    return true;
                }
            } );



            final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkbox_always_on");
            checkboxPref.
                    setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                        public boolean onPreferenceChange(Preference preference, Object newValue) {
                            if (newValue.toString().equals("true")) {

                                Screen.screen_on(getActivity());
                                Snackbar.make(getListView(),"Auto-extinction de l'écran désactivé",Snackbar.LENGTH_SHORT);

                            } else {
                                Screen.screen_off(getActivity());
                                Snackbar.make(getView(),"Auto-extinction de l'écran activé",Snackbar.LENGTH_SHORT);
                            }
                            return true;
                        }
                    });
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = super.onCreateView(inflater, container, savedInstanceState);
            view.setBackgroundColor(getResources().getColor(R.color.green_800));

            return view;
        }
    }

Graddle dependencies :

    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support:support-v13:23.0.0'
    compile 'com.android.support:support-v4:23.0.0'
    compile 'com.android.support:design:23.0.0'
    compile 'com.android.support:recyclerview-v7:23.0.0'

I have this error that I do not understand

android.view.InflateException: Binary XML file line #18: Error inflating class android.support.design.widget.Snackbar$SnackbarLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183)
at android.support.design.widget.Snackbar.make(Snackbar.java:206)
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87)
at android.preference.Preference.callChangeListener(Preference.java:1076)
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64)
at android.preference.Preference.performClick(Preference.java:1131)
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225)
at android.widget.AdapterView.performItemClick(AdapterView.java:334)
at android.widget.AbsListView.performItemClick(AbsListView.java:1531)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667)
at android.widget.AbsListView$3.run(AbsListView.java:5590)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
at android.support.design.widget.Snackbar.make(Snackbar.java:206) 
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87) 
at android.preference.Preference.callChangeListener(Preference.java:1076) 
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64) 
at android.preference.Preference.performClick(Preference.java:1131) 
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225) 
at android.widget.AdapterView.performItemClick(AdapterView.java:334) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1531) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667) 
at android.widget.AbsListView$3.run(AbsListView.java:5590) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5944) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:480)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:629)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
at android.support.design.widget.Snackbar.make(Snackbar.java:206) 
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87) 
at android.preference.Preference.callChangeListener(Preference.java:1076) 
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64) 
at android.preference.Preference.performClick(Preference.java:1131) 
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225) 
at android.widget.AdapterView.performItemClick(AdapterView.java:334) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1531) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667) 
at android.widget.AbsListView$3.run(AbsListView.java:5590) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5944) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:480) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:629) 
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
at android.support.design.widget.Snackbar.make(Snackbar.java:206) 
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87) 
at android.preference.Preference.callChangeListener(Preference.java:1076) 
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64) 
at android.preference.Preference.performClick(Preference.java:1131) 
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225) 
at android.widget.AdapterView.performItemClick(AdapterView.java:334) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1531) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667) 
at android.widget.AbsListView$3.run(AbsListView.java:5590) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5944) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 5
at android.content.res.TypedArray.twGetColorStateList(TypedArray.java:438)
at android.widget.TextView.<init>(TextView.java:1388)
at android.widget.Button.<init>(Button.java:115)
at android.widget.Button.<init>(Button.java:108)
at android.widget.Button.<init>(Button.java:104)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:480) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:629) 
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
at android.support.design.widget.Snackbar.make(Snackbar.java:206) 
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87) 
at android.preference.Preference.callChangeListener(Preference.java:1076) 
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64) 
at android.preference.Preference.performClick(Preference.java:1131) 
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225) 
at android.widget.AdapterView.performItemClick(AdapterView.java:334) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1531) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667) 
at android.widget.AbsListView$3.run(AbsListView.java:5590) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5944) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 

EDIT : my style

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    android:pre


    <PreferenceCategory
        android:title="General">

        <CheckBoxPreference
            android:key="checkbox_always_on"
            android:title="Garder l'écran allumé"
            android:defaultValue="false"/>


    </PreferenceCategory>

    <PreferenceCategory
        android:title="Base de donnée">

        <!--<ListPreference-->
            <!--android:key="list_preference"-->
            <!--android:title="General"-->
            <!--android:dialogTitle="General"-->
            <!--android:entries="@array/entries_list_preference"-->
            <!--android:entryValues="@array/entryvalues_list_preference" />-->

        <!--<Preference-->
            <!--android:title="General">-->
            <!--<intent android:action="android.intent.action.VIEW"-->
                <!--android:data="http://codepath.com/" />-->
        <!--</Preference>-->

        <Preference
            android:title="Sauvegarder la base de donnée sur la carte SD"
            android:key="savedatabase">

        </Preference>


    </PreferenceCategory>

</PreferenceScreen>
like image 612
filol Avatar asked Jun 21 '16 12:06

filol


2 Answers

So with the help of @YasinKaçmaz I found the answer to my problem. Here is my code

public class SettingsFragment extends AppCompatActivity  {

static CoordinatorLayout coordinatorLayout;
 Activity thisActivity=this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SnackbarShow.showSnackbar(thisActivity);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction().replace(android.R.id.content,
                new PrefsFragment()).commit();
        ActionBar bar = getSupportActionBar();
       bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4CAF50")));


    }

    public static class PrefsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.settings);


            Preference save_database = findPreference("savedatabase");
            save_database.setOnPreferenceClickListener( new Preference.OnPreferenceClickListener()
            {
                public boolean onPreferenceClick( Preference pref )
                {
                    Data.bdd.Save_database(getActivity(),getView());
                    return true;
                }
            } );


            final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkbox_always_on");
            checkboxPref.
                    setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                        public boolean onPreferenceChange(Preference preference, Object newValue) {
                            if (newValue.toString().equals("true")) {
                                Screen.screen_on(getActivity());
                                showSnackbar(getView());
                            } else {
                                Screen.screen_off(getActivity());
                                showSnackbar(getView());
                            }
                            return true;
                        }
                    });
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = super.onCreateView(inflater, container, savedInstanceState);

            view.setBackgroundColor(getResources().getColor(R.color.green_800));

            return view;
        }


        public static void showSnackbar(View view) {
            Snackbar snackbar = Snackbar
                    .make(view, "There is one text", Snackbar.LENGTH_LONG)
                    .setAction("Action Text", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Log.v("snackbar","yoo clicked action");
                        }
                    });

            // Changing message text color
            snackbar.setActionTextColor(Color.RED);

            // Changing action button text color
            View sbView = snackbar.getView();
            TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.YELLOW);

            snackbar.show();
        }
    }
like image 85
filol Avatar answered Sep 21 '22 05:09

filol


As per the documentation - https://developer.android.com/reference/android/support/design/widget/Snackbar.html#make(android.view.View,%20java.lang.CharSequence,%20int)

You will need parent view which will be pass to make() method as a first param, So in your Fragment's layout file, find out the parent view, i.e. RelativeLayout or LinearLayout. Get the XML reference of the same using view.findViewById() method. And pass the object of the parent view as first parameter. Hope it will help!

like image 30
Onkar Nene Avatar answered Sep 24 '22 05:09

Onkar Nene