Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have figured out a way to retrieve the default color used by the Android device your application is running in. It is a bit tricky and requieres that you retrieve the color being shown from another Preference Summary View of your activity and store it in runtime.</p> <p>Then you can use the same color code in other Views of other preferences, assuring that you will allways get the same color code Android assigned to the standard preferences. Here is how I did it:</p> <p>My preferences activity has a normal CheckBoxPreference that I use to activate or deactivate a service. I have extended CheckBoxPreference as follows, so my extension retrieves in rutime the default color Android finally gave to the summary of that CheckBoxPreference:</p> <pre><code>public class MyCheckBoxPreference extends android.preference.CheckBoxPreference { private static int sSummaryColor = Color.WHITE; private static boolean sInitialized = false; public MyCheckBoxPreference(Context context) { super(context); } public MyCheckBoxPreference(Context context, AttributeSet attrs) { super(context, attrs); } public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onBindView(View view) { super.onBindView(view); if (!sInitialized) { sSummaryColor = getSummaryColor(view); sInitialized = true; } } private int getSummaryColor(View view) { int color = Color.WHITE; // Gets the color android gave to the summary by default TextView summaryView = (TextView) view.findViewById(android.R.id.summary); if (summaryView != null) { ColorStateList list = summaryView.getTextColors(); if (list != null) { color = list.getDefaultColor(); } } return color; } public static int getSummaryColor() { return sSummaryColor; } } </code></pre> <p>In my preferences.xml I instantiate that preference as MyCheckBoxPreference instead of just CheckBoxPreference:</p> <pre><code>&lt;org.yourpackage.MyCheckBoxPreference android:title="@string/preference_title_activate" android:defaultValue="false" android:summary="@string/preference_summary_activate_off" android:summaryOff="@string/preference_summary_activate_off" android:key="preference_activate"&gt; &lt;/org.yourpackage.MyCheckBoxPreference&gt; </code></pre> <p>The MyCheckBoxPreference has to be instantiated once before retrieving the summary color with MyCheckBoxPreference.getSummaryColor(). </p> <p>Now you can set the color of other customized preferences from onBindView(View):</p> <pre><code>public class MyCustmizedPreference extends Preference { public MyCustmizedPreference (Context context) { super(context); setLayoutResource(R.layout.my_customized_preference); } @Override public void onBindView(View view) { super.onBindView(view); TextView summaryView = (TextView) view.findViewById(android.R.id.summary); if (summaryView != null) { summaryView.setTextColor(MyCheckBoxPreference.getSummaryColor()); } } } </code></pre> <p>It actually works under Samsung Galaxy S. I have also tested that it doesn't break anything under the emulator.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload