How to add ripple effect to preferences in android? - android

I am working on adding ripple effect when the preference is touched (selected). I have customized my preference by extending the ListPreference. I have tried to set the ripple effect programmatically by using RippleDrawable but I don't see the animation.
Here is my customized preference class
public class CustomListPreference extends ListPreference {
public CustomListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListPreference(Context context) {
super(context);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
setCustomStyle(view);
}
private void setCustomStyle(View view) {
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setTypeface(InitActivity.TYPEFACE_REGULAR);
TextView summary = (TextView) view.findViewById(android.R.id.summary);
summary.setTypeface(InitActivity.TYPEFACE_REGULAR);
//Setting the drawable here, but it doesn't work.
RippleDrawable drawable = (RippleDrawable) getContext().getResources().getDrawable(R.drawable.my_ripple_background);
view.setBackGround(drawable);
}
}
My preferences layout
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- opens a subscreen of settings -->
<com.abc.app.CustomListPreference
android:defaultValue="1"
android:entries="#array/sampleEntries"
android:entryValues="#array/SampleEntryValues"
android:key="some_preference"
android:title="#string/some_preferences" />
<com.abc.app.CustomCheckboxPreference
android... />
</PreferenceScreen>
My ripple xml
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/light_black_overlay"> <!--#22000000-->
<item>
<shape
android:shape="rectangle">
<solid android:color="#android:color/background_light" />
</shape>
</item>
</ripple>
Am I setting the animation for the correct view? Any ideas are appreciated. Thanks.

This is a minimal complete example for adding a custom ripple effect to a class that extends ListPreference. I just made and tested this with API 21 (5.0).
SettingsActivity (Launch Activity)
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
}
}
pref_general.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:defaultValue="true"
android:key="example_checkbox"
android:summary="a checkbox"
android:title="Checkbox test" />
<!-- replace with com.abc.app.CustomListPreference in your case-->
<com.timcastelijns.rippletest.CustomListPreference
android:defaultValue="1"
android:entries="#array/sampleEntries"
android:entryValues="#array/SampleEntryValues"
android:key="some_preference"
android:title="test" />
</PreferenceScreen>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sampleEntries">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="SampleEntryValues">
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
</resources>
CustomListPreference
public class CustomListPreference extends ListPreference {
private Context ctx;
public CustomListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
}
public CustomListPreference(Context context) {
super(context);
ctx = context;
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
setCustomStyle(view);
}
private void setCustomStyle(View view) {
RippleDrawable drawable = (RippleDrawable) ctx.getDrawable(R.drawable.my_ripple_background);
view.setBackground(drawable);
}
}
my_ripple_background.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/holo_blue_light">
<item android:id="#android:id/mask">
<color android:color="#android:color/white" />
</item>
</ripple>
When pressed, it shows a light blue ripple effect, as specified in the xml:
I built this example based on your code, and code from the example SettingsActivity in the android SDK samples.
Edit:
After some time in chat and trying various things, we have come to a conclusion that the problem is caused by OP's phone (Samsung S5) or it's settings. When OP tried the code in the emulator, it all worked properly.
For reference - this is how it looked in OPs phone:

Related

Using android:drawable in a ColorStateList

When creating a ColorStateList for a share button, I first used android:drawable to specify an item color like so (by accident)
<item android:state_pressed="true" android:drawable="#color/stock_orange"/>
instead of android:color (like "normal")
<item android:state_pressed="true" android:color="#color/stock_orange"/>
Though there wasn't a crash and a color change occurred when pressed, it was the wrong color (magenta instead of the specified orange).
Is there an obvious explanation for this? Can/should drawables be used in a color state list?
Resource/Color/share_btn_color_state_list.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#A0A0A0"/>
<!--QUESTION: works, but color is magenta, not orange -->
<!--item android:state_pressed="true" android:drawable="#color/stock_orange"/-->
<item android:state_pressed="true" android:color="#color/stock_orange"/>
<item android:color="#color/black"/>
</selector>
Resource/Layout/share_view.xml
<ImageView
android:id="#+id/share_btn_iv"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_share_black_24dp"/>
Usage
_shareButton = FindViewById<ImageView>(Resource.Id.share_btn_iv);
_shareButton.Enabled = true;
_shareButton.Drawable.SetTintList(
ColorStateList.CreateFromXml(
Resources,
Resources.GetXml(Resource.Color.share_btn_color_state_list)
)
);
drawable in a ColorStateList
One way is to customize an ImageView and use a combination of a ColorFilter and a ColorStateList that contains your tint color for when the button is pressed.
Extend ImageView and wrap DrawableStateChanged() with code that sets the tint based on the new state :
public class TintableImageView : ImageView
{
private ColorStateList tint;
public TintableImageView(Context context) : base(context)
{
}
public TintableImageView(Context context, IAttributeSet attrs):base(context,attrs)
{
init(context, attrs, 0);
}
public TintableImageView(Context context, IAttributeSet attrs, int defStyle):base(context,attrs,defStyle)
{
init(context, attrs, defStyle);
}
private void init(Context context, IAttributeSet attrs, int defStyle)
{
TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.TintableImageView, defStyle, 0);
tint = a.GetColorStateList(Resource.Styleable.TintableImageView_tint);
a.Recycle();
}
protected override void DrawableStateChanged()
{
base.DrawableStateChanged();
if(tint != null && tint.IsStateful)
{
UpdateTintColor();
}
}
public void SetColorFilter(ColorStateList tint)
{
this.tint = tint;
base.SetColorFilter(new Color(tint.GetColorForState(GetDrawableState(), new Color(0))));
}
private void UpdateTintColor()
{
var color = new Color(tint.GetColorForState(GetDrawableState(), new Color(0)));
SetColorFilter(color);
}
}
Define a custom attribute :
attrs.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="TintableImageView">
<attr name="tint" format="reference|color" />
</declare-styleable>
</resources>
Color selector like this : Resource\Color\color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/colorAccent"/>
<item android:color="#00000000"/>
</selector>
When use this custom ImageView ;
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageDemo.TintableImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/download"
app:tint="#color/color_selector"
android:clickable="true"/>
</LinearLayout>
EDIT :
For example, add a translucent color as the color effect :
Resource\Color\color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#55000000"/>
<item android:color="#00000000"/>
</selector>
Effect :
Usually the color state list are used to change the color of any ui element for different states For example, a Button widget can exist in one of several different states (pressed, focused, or neither) and, using a color state list, you can provide a different color during each state.
And a StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or neither) and, using a state list drawable, you can provide a different background image for each state.
Though what you did worked but it is not a recommended approach

Coordinating drawable state between parent and child

I have a custom view LockButton which is just a FrameLayout that holds two specific child views. It holds an ImageButton as well as a type of custom view ProgressIndicator. This question centers around the behavior of LockButton and ImageButton. In particular I have added a custom state to the LockButton. The ImageButton has a StateListDrawable for it's source and a ColorStateList for its background.
The problem is when I change the state in LockButton, that change is not displayed in the ImageButton
What am I doing wrong?
LockButton Class:
public class LockButton extends FrameLayout {
private static final int[] STATE_LOCKED = {R.attr.state_locked};
private boolean locked = true;
View button;
public LockButton(#NonNull Context context) {
this(context, null);
}
public LockButton(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.lock_button, this);
button = findViewById(R.id.lock_button_actual);
}
#Override
public void setOnClickListener(#Nullable OnClickListener l) {
button.setOnClickListener(l);
}
#Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState;
if(locked){
drawableState = super.onCreateDrawableState(extraSpace + 1);
mergeDrawableStates(drawableState, STATE_LOCKED);
} else {
drawableState = super.onCreateDrawableState(extraSpace);
}
return drawableState;
}
public void lock(boolean locked) {
if(this.locked != locked) {
this.locked = locked;
}
refreshDrawableState();
button.refreshDrawableState();
}
public static class ProgressCircle extends View {...}
}
LockButton Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:state_locked="true"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<view class="com.example.LockButton$ProgressCircle"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_gravity="center"
/>
<ImageButton
android:id="#+id/lock_button_actual"
android:layout_width="#dimen/fab_size"
android:layout_height="#dimen/fab_size"
android:background="#drawable/button_lock_background"
android:src="#drawable/icon_lock_status"
android:tint="#color/white"
android:elevation="6dp"
android:layout_gravity="center"
android:duplicateParentState="true"
/>
</FrameLayout>
Attributes XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="lockState">
<attr name="state_locked" format="boolean" />
</declare-styleable>
</resources>
Drawable icon_lock_status.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item app:state_locked="false" android:drawable="#drawable/icon_lock_open" />
<item app:state_locked="true" android:drawable="#drawable/icon_lock_closed"/>
</selector>
Drawable button_lock_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight"
>
<item>
<shape android:shape="oval">
<solid android:color="#color/button_lock_color" />
</shape>
</item>
</ripple>
Color button_lock_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item app:state_locked="false" android:color="#color/grey_dark" />
<item app:state_locked="true" android:color="#color/vibrant_red" />
</selector>
When I need to change the state, I simply call
lockButton.lock(true); // or false
on the LockButton
I have also tested to see if using default states instead of my custom one would work any better. This did not help. The ImageButton still does not respond to changes in the default states.
Setting a StateListDrawable as a background to the LockButton (FrameLayout) does work. So the state change is working properly on the parent, it is just not reflected in the child.

AnimationDrawable doesn't autostart on Lollipop

I have an Android App where looped AnimationDrawbale where autostarted. With latest Lollipop it doesn't autostart no more.
There is a simple ImageView where the src address the next drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<level-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="#drawable/ic_weather_sun" />
<item android:maxLevel="1" android:drawable="#drawable/ic_weather_cloudsun" />
<item android:maxLevel="2" android:drawable="#drawable/ic_weather_cloud" />
<item android:maxLevel="3" android:drawable="#drawable/ic_weather_rain" />
<item android:maxLevel="4" android:drawable="#drawable/ic_weather_rainstorm" />
</level-list>
each item of the level list is another drawable xml.
Some of them are animation-list (Animationdrawable), others are layer-list with one or more layer composed by an animation-list. Here an example:
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<animation-list android:oneshot="false">
<item android:drawable="#drawable/ic_weather_layer_rain1" android:duration="100" />
<item android:drawable="#drawable/ic_weather_layer_rain2" android:duration="100" />
<item android:drawable="#drawable/ic_weather_layer_rain3" android:duration="100" />
<item android:drawable="#drawable/ic_weather_layer_rain4" android:duration="100" />
</animation-list>
</item>
<item android:drawable="#drawable/ic_weather_layer_cloudcolor1" />
<item android:drawable="#drawable/ic_weather_layer_cloud2" />
<item android:drawable="#drawable/ic_weather_layer_cloud1" />
</layer-list>
The ImageView level is set invisible at activity start and it is simply selected in this way.
final ImageView meteo = (ImageView)findViewById(R.id.image_meteo);
meteo.setVisibility(View.VISIBLE);
meteo.setImageLevel( weatherIdx );
Any idea on th reason? And how I should manage that?
I cannot address all AnimationDrawables, because there are several of them in a not known structure.
Thanks
Here is a class based on AppCompatImageView that will autostart both the source and background AnimationDrawables.
public class AnimationImageView extends AppCompatImageView
{
public AnimationImageView(Context context)
{
super(context);
}
public AnimationImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public AnimationImageView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
#Override
public void setBackgroundResource(#DrawableRes int resId)
{
super.setBackgroundResource(resId);
startAnimation(getBackground());
}
#Override
public void setBackgroundDrawable(Drawable background)
{
super.setBackgroundDrawable(background);
startAnimation(background);
}
#Override
public void setImageResource(#DrawableRes int resId)
{
super.setImageResource(resId);
startAnimation(getDrawable());
}
#Override
public void setImageDrawable(#Nullable Drawable drawable)
{
super.setImageDrawable(drawable);
startAnimation(drawable);
}
protected void startAnimation(Drawable drawable)
{
if (drawable instanceof AnimationDrawable)
{
((AnimationDrawable) drawable).start();
}
}
}
You can use it in your layouts as follows (change com.sample.ui.views to match the package where you will place the class above):
<com.sample.ui.views.AnimationImageView
android:id="#+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_src_animation"
android:background="#drawable/my_bg_animation"/>
should just be able to cast it to an animation drawable and then click start.
((AnimationDrawable) imageview.getDrawable()).start();
not sure why there is a change in behavior on 5.0 but this does not change behavior on lower APIs

Custom SwitchPreference in Android

How to set a custom style or other background selector drawable for the SwitchPreference widget in Android?
(Note: not the regular Switch widget, I mean the standart SwitchPreference widget that used in PreferenceActivity / PreferenceFragment)
You have to create a custom layout for the switch itself and you can apply it dynamically like.
preference.setWidgetLayoutResource(R.layout.custom_switch);
But I'll go into details and show you exactly how to achieve this.
So, you define your preference in an xml file like preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="YOUR_CATEGORY_TITLE" >
<SwitchPreference
android:key="SWITCH"
android:title="YOUR_TITLE_FOR_SWITCH" />
</PreferenceCategory>
</PreferenceScreen>
Then read it in the onCreate() method inside your PreferenceActivty class:
SwitchPreference pref = (SwitchPreference) findPreference(getString(R.string.SWITCH));
//pref.setChecked(true); // You can check it already if needed to true or false or a value you have stored persistently
pref.setWidgetLayoutResource(R.layout.custom_switch); // THIS IS THE KEY OF ALL THIS. HERE YOU SET A CUSTOM LAYOUT FOR THE WIDGET
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Here you can enable/disable whatever you need to
return true;
}
});
The custom_switch layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_switch_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textIsSelectable="false"
android:textSize="18sp"
android:textStyle="bold"
android:track="#drawable/switch_track"
android:thumb="#drawable/switch_thumb"/>
And for the switch you'll have 2 selectors for the track and thumb properties.
The drawables for these selectors can be generated with the Android Holo Color Generator, which was suggested by tasomaniac. In this case, all you have to do, is to copy the content of the generated drawable folders(only for the drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi). But you can create custom views for each state you need.
Here is how these selectors will look like:
switch_track:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/switch_bg_focused" android:state_focused="true"/>
<item android:drawable="#drawable/switch_bg"/>
</selector>
switch_thumb:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/switch_thumb_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/switch_thumb_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/switch_thumb_activated" android:state_checked="true"/>
<item android:drawable="#drawable/switch_thumb"/>
</selector>
And that's pretty much it. This solution helped me out. If I omitted something, please let me know and I'll correct the issues.
You can use the below website to generate style for your Switch.
http://android-holo-colors.com/
And then you can use following libraries to custom implementation of the regular Switch. These libraries also include SwitchPreference alternative.
https://github.com/BoD/android-switch-backport
https://github.com/ankri/SwitchCompatLibrary
One way of doing this is to subclass the SwitchPreference and override the onBindView method. In doing so, you'll want to still call super.onBindView(view) in that method, but then find the Switch in the child views and style it as appropriate:
package com.example;
import android.annotation.SuppressLint;
import android.content.Context;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import com.example.R;
public class CustomSwitchPreference extends SwitchPreference {
#SuppressLint("NewApi")
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
Switch theSwitch = findSwitchInChildviews((ViewGroup) view);
if (theSwitch!=null) {
//do styling here
theSwitch.setThumbResource(R.drawable.new_thumb_resource);
}
}
private Switch findSwitchInChildviews(ViewGroup view) {
for (int i=0;i<view.getChildCount();i++) {
View thisChildview = view.getChildAt(i);
if (thisChildview instanceof Switch) {
return (Switch)thisChildview;
}
else if (thisChildview instanceof ViewGroup) {
Switch theSwitch = findSwitchInChildviews((ViewGroup) thisChildview);
if (theSwitch!=null) return theSwitch;
}
}
return null;
}
}
Create a style in your style.xml file and give it Widget.AppCompat.CompoundButton.Switch parent.
<style name="theme_switch_compat" parent="Widget.AppCompat.CompoundButton.Switch">
<item name="colorAccent">#color/YourColorAccent</item>
</style>
Then you can use the link below to complete your theme
How to change the track color of a SwitchCompat

Change height of Preference items

I have a PreferenceFragment subclass. I want each one of its items (Preferences and SwitchPreferences) to have a height of 120dp. How to do that?
Here is the related code:
public class SettingsFragment extends PreferenceFragment {
public SettingsFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.main);
}
}
and
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference android:key="app_main_switch"
android:title="#string/app_name"
android:defaultValue="true"/>
<Preference android:title="#string/events_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.EventList"/>
</Preference>
<Preference android:title="#string/filters_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.FilterList"/>
</Preference>
<SwitchPreference android:dependency="app_main_switch"
android:key="learn_switch"
android:defaultValue="false"
android:title="#string/learning"/>
</PreferenceScreen>
Here is how it looks like now:
So I want all four items of the list to have a height of 120dp. As you can see I'm not the one creating the ListView, it's created internally. I tried to retrieve it with
findViewById(android.R.id.list)
but iterating over its elements gives Preference objects which do not allow me to set the height.
Just do it this way:
<!-- Application theme -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Min item height -->
<item name="android:listPreferredItemHeight">10dp</item>
</style>
another styling attributes that can be overridden can be found here preference item layout
Try creating a custom Preference class (you may have to do it for every type of Preference you are going to use/want height to be 120DP)
public class SwitchPref extends SwitchPreference {
Context context;
public Pref(Context context) {
super(context);
this.context = context;
}
#Override
protected View onCreateView(ViewGroup parent) {
LinearLayout layout = new LinearLayout(getContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(120));
layout.setLayoutParams(params1);
//if this returns just the linearlayout, you will have to add your own switch
// or reference to a layout.xml resource
return super.onCreateView(layout);
}
public int dpToPx(int dp) {
int valueInDp = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
.getDisplayMetrics());
return valueInDp;
}
}
Then, in your preference.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!--use your custom SwitchPrefence class-->
<my.package.name.SwitchPref android:key="app_main_switch"
android:title="#string/app_name"
android:defaultValue="true"/>
<Preference android:title="#string/events_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.EventList"/>
</Preference>
<Preference android:title="#string/filters_lowercase"
android:dependency="app_main_switch">
<intent android:targetPackage="hu.ppke.itk.marma.android.bead"
android:targetClass="hu.ppke.itk.marma.android.bead.FilterList"/>
</Preference>
<my.package.name.SwitchPref android:dependency="app_main_switch"
android:key="learn_switch"
android:defaultValue="false"
android:title="#string/learning"/>
</PreferenceScreen>
Hope this helps, happy coding!
Here is how I managed to do this:
I did need to subclass Preference but MattMatt's answer didn't work.
public class HigherPreference extends Preference {
public HigherPreference(Context context) {
super(context);
}
public HigherPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected View onCreateView(ViewGroup parent) {
Resources res=getContext().getResources();
View v=super.onCreateView(parent);
v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, res.getDimensionPixelSize(R.dimen.rowheight)));
return v;
}
}
The layout XML goes like what he provided.

Categories

Resources