-1

Solved with https://stackoverflow.com/posts/26564401/revisions

I've an issue with my preferences screen. If anyone have an idea of the problem. I've test many things but I don't really understand layout with preference fragment...

Activity_settings.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="?attr/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    />

Preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout="@layout/activity_settings">
    <EditTextPreference android:title="Your Name"
        android:key="username"
        android:summary="Please provide your username"></EditTextPreference>
    <CheckBoxPreference android:title="Application Updates"
        android:defaultValue="false"
        android:summary="This option if selected will allow the application to check for latest versions."
        android:key="applicationUpdates" />
    <ListPreference     android:title="Download Details"
        android:summary="Select the kind of data that you would like to download"
        android:key="downloadType"
        android:defaultValue="1"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues" />
    <PreferenceScreen
        android:title="Test de merde"
        android:summary="Lolilol de merde">

        <intent android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />

    </PreferenceScreen>
    <PreferenceScreen
        android:title="Bonjour"
        android:summary="Hello"
        android:key="user">
    </PreferenceScreen>
</PreferenceScreen>

MyPreferencesActivity.java

package activity;

import android.app.FragmentManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.pierre.tan.R;

/**
 * Created by dev on 19/04/15.
 */
public class MyPreferencesActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
            Toolbar bar;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
                bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.activity_settings, root, false);
                root.addView(bar, 0); // insert at top
            } else {
                ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
                ListView content = (ListView) root.getChildAt(0);

                root.removeAllViews();

                bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.activity_settings, root, false);


                int height;
                TypedValue tv = new TypedValue();
                if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
                    height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
                }else{
                    height = bar.getHeight();
                }

                content.setPadding(0, height, 0, 0);

                root.addView(content);
                root.addView(bar);
            }

            bar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }






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

                addPreferencesFromResource(R.xml.preferences);

                Preference userButton = (Preference) findPreference("user");
                userButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                    @Override
                    public boolean onPreferenceClick(Preference arg0) {
                        Intent intent = new Intent(getActivity(), ChangelogActivity.class);
                        startActivity(intent);
                        return true;
                    }
                });
            }
        }

    }

And a screenshot to show my problem:

enter image description here

Thank you for all :)

Community
  • 1
  • 1
  • What is your problem? Please clarify what do you want to ask. Currently your question is vague. We cant help you untill we don't understand what do you want to do actually. – BBdev Apr 22 '15 at 05:21
  • link to the actual [answer](https://stackoverflow.com/questions/26564400/creating-a-preference-screen-with-support-v21-toolbar/26564401#26564401) and not the revision page – lasec0203 Nov 22 '21 at 06:44

1 Answers1

0

Use setSupportActionBar(bar) rather than root.addView(bar).

natario
  • 24,954
  • 17
  • 88
  • 158
  • But i can't have extends ActionBarActivity, no ? – francois53 Apr 19 '15 at 11:09
  • Yes, switch PreferenceActivity to ActionBarActivity. You are using a PreferenceFragment so you already benefit from the "preference" stuff, no need for a PreferenceActivity. – natario Apr 19 '15 at 11:18
  • "java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewParent android.view.View.getParent()' on a null object reference" – francois53 Apr 19 '15 at 11:28
  • public class MyPreferencesActivity extends ActionBarActivity { private Toolbar mToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } – francois53 Apr 19 '15 at 11:35
  • You need to alter your layout accordingly, you can't use `android.R.id.content` anymore. Namely, you should have a FrameLayout below the Toolbar view, and inflate the fragment inside that frame. That's common practice. – natario Apr 19 '15 at 11:52