How to use Percentage for android layout?

UPDATE 2018: PercentRelativeLayout and PercentFrameLayout are deprecated. Consider using ConstraintLayout Old Answer: So far Android Support library has limited on where to use this: with RelativeLayout android.support.percent.PercentRelativeLayout with FrameLayout android.support.percent.PercentFrameLayout How to use it? Well, first make sure to include the dependency at build.grade(Module: app) in your android app. dependencies { compile ‘com.android.support:percent:23.3.0’ } Then … Read more

Dynamic ListPreference in android

Every XML element in Android can be created programmatically as the element name is also a Java class. Hence you can create a ListPreference in code: CharSequence[] entries = { “One”, “Two”, “Three” }; CharSequence[] entryValues = { “1”, “2”, “3” }; ListPreference lp = new ListPreference(this); lp.setEntries(entries); lp.setEntryValues(entryValues); You could alternatively create it in … Read more

How do you create Preference Activity and Preference Fragment on Android?

I found this post (What to use instead of “addPreferencesFromResource” in a PreferenceActivity?) that help me understand that you have to go through a PreferenceFragment in order to do it. In the following explanation I use your.package. just to show that you have to put the package name. Everybody has its own package so please … Read more

Android – How Do I Set A Preference In Code

I assume by preferences you are referring to your application’s preferences and not Android phone settings. To store preferences between runs of you application you need to do the following Create a SharedPreferences object SharedPreferences settings = getSharedPreferences(String n, MODE_PRIVATE); String n identifies your preferences and the second argument is the mode they’ll be accessed … Read more

How to change text color of preference category in Android?

use this customize PreferenceCategory class : public class MyPreferenceCategory extends PreferenceCategory { public MyPreferenceCategory(Context context) { super(context); } public MyPreferenceCategory(Context context, AttributeSet attrs) { super(context, attrs); } public MyPreferenceCategory(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onBindView(View view) { super.onBindView(view); TextView titleView = (TextView) view.findViewById(android.R.id.title); titleView.setTextColor(Color.RED); } } and … Read more

When to use CheckBox and when Switch

I actually find this to be quite an interesting question, particularly considering that one can easily emulate the functionality of a switch using a checkbox in Android. According to the Android developers guide, a checkbox is simply a type of switch. Check the quotes below or read the full description here. Checkboxes: Checkboxes allow the … Read more

How to open or simulate a click on an android Preference, created with XML, programmatically?

See the new accepted answer for a much cleaner approach! This was working, but not really the clean way of doing it. Damn it, it got me several hours, but it finally works. The solution is the undocumented call public void onItemClick (…). It takes several arguments, and as pointed out by this question it … Read more

Non Deprecated findPreference() Method? – Android

Is there a non deprecated equivalent? If you are using PreferenceFragment on API Level 11+ devices, you would call findPreference() on it. Otherwise, call findPreference() on your PreferenceActivity, as you have no choice. If not, what if I use it anyway? It will work. How can Fragments help me do my task in a better … Read more