2

Please click to see the image http://i1094.photobucket.com/albums/i455/tdounnyy/device.png

At the bottom of this PreferenceActivity, the "Select ringtone" has an icon/button on the right side.

And "Vibrate" does not.

This Vibrate Preference is one custome Preference extends Preference.

I want to put the same Icon in the Vibrate row. One way is to extends ListPreference or RingtonePreference to cheat OS.

I don't like this, wondering if there is a better solution.

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Layout used by PreferenceScreen. This is inflated inside 
        android.R.layout.preference. -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="4dip"
    android:layout_gravity="center_vertical"
    android:background="@drawable/btn_circle"
    android:src="@drawable/ic_btn_round_more" />

I added prefix "android" as:

android:background="@android:drawable/btn_circle"
android:src="@android:drawable/ic_btn_round_more"

Now, Eclipse says: Error: Resource is not public. I googled but no luck. Any thought how to break through?

Gary Chen
  • 248
  • 2
  • 14
tdounnyy
  • 23
  • 1
  • 5

2 Answers2

7

When you create your preference xml file, you can specify a custom layout for a preference, and you can put an icon in it. The easiest way to do that is to look in the Android SDK folder: platforms/data//res/layout/ (I believe this it) contains the actual layout files used for the different preference types: you can probably copy one of them from there and use that as the basis of your own custom preference.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Thx, this is really one elegant solution. But then, I`m facing another problem, see above. Reply appreciates ~~ – tdounnyy May 05 '11 at 12:18
  • you don't have to reference the android resource (which is not public), but you have to literally copy the resource file and add it to your project drawables set. – Marcovena May 05 '11 at 12:43
  • @Marcovena is right: you have to copy it to your own res/layout folder to use it, and then its just a regular application resource, not an android one anymore. – Femi May 05 '11 at 13:58
  • I`d like to stay the same style with the whole system. While user changes theme, my app will just look like the others. – tdounnyy May 09 '11 at 08:22
  • Its a good point: I think if your style references the Android theme colors it will adapt to changes in system theme. Never tried it, though, so I honestly can't confirm one way or another how it works. – Femi May 09 '11 at 14:04
0

inside your preference.xml

android:widgetLayout="@layout/layout_contain_imageview"

or in .java

 setWidgetLayoutResource(R.layout.layout_contain_imageview);

example for layout_contain_imageview.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ivIcon"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:src="@drawable/round_app_settings_alt_white_36"
    app:tint="@color/icon_tint"/>

when you want to change icon at runtime

@Override
    public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        ImageView ivIcon = (ImageView) holder.findViewById(R.id.ivIcon);
        ivIcon.setImageResource(R.drawable.round_get_app_white_36);
    }
rattisuk
  • 407
  • 5
  • 7