Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting preference layout and changing the attribute in it
    text
    copied!<p>is it possible to access programmatically a layout which is set to a Preference?</p> <p>Here is what I have, a very simple project - proof of concept</p> <p>The Preference Activity:</p> <pre><code>package com.example; import android.os.Bundle; import android.preference.PreferenceActivity; import android.util.Log; import android.view.View; public class PreferenceExampleActivity extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); ImageView v = (ImageView) findViewById(R.id.iconka); } } </code></pre> <p>The resource XML:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="settings"&gt; &lt;PreferenceCategory android:title="Category Setting Name" android:order="1" android:key="Main"&gt; &lt;Preference android:order="1" android:title="Setting" android:summary="Setting1" android:layout="@layout/profile_preference_row" android:key="profile" /&gt; &lt;/PreferenceCategory&gt; &lt;/PreferenceScreen&gt; </code></pre> <p>The custom layout for the Preference:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/widget_frame" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?android:attr/listPreferredItemHeight" android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize"&gt; &lt;RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dip" android:layout_marginRight="6dip" android:layout_marginTop="6dip" android:layout_marginBottom="6dip" android:layout_weight="1"&gt; &lt;TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge" android:ellipsize="marquee" android:fadingEdge="horizontal" /&gt; &lt;TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="?android:attr/textColorSecondary" android:maxLines="4" /&gt; &lt;/RelativeLayout&gt; &lt;ImageView android:id="@+id/iconka" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>What I want is to be able to access the "iconka" ImageView from the Activity and change the image from there. I am using API 8 (Android 2.2)</p> <p>Currently the "v" is null and I don't have any idea why is that.</p> <p>A hint will be much appreciated!</p> <p><strong>Update - The Solution:</strong> Actually, I needed a custom preference which I can modify for my needs. This one is a practical guide how to create your own Custom preference in your project: <a href="http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html" rel="noreferrer">Android &amp; Amir - Android Preferences </a> See the part when the author creates a custom preference class.</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