How to initialize my own Preference layout? - android

I've created my custom preference layout with imageView.
<Preference
...
android:key="pref_custom"
android:layout="#layout/preference_layout"
>
In my own PreferenceActivity inside onCreate I want to change my CustomPreference ImageView.
Problem is that code below always returns null.
imageViewSmallContact = (ImageView) findViewById(R.id.ImageViewSmallContact);
I can find this ImageView only inside OnPreferenceClickListener.onPreferenceClick method my pref_custom Preference.
In My class ConfigureActivity (extends PreferenceActivity) in onCreate method I have only":
...
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
updatePreferenceView(); // here I update all my preferences `
...
In my PreferenceActivity onCreate method I read my preference
Preference pref= findPreference("pref_phone_");
Than I want to change Image in my custom layout of this one Preference.
imageViewSmallContact = (ImageView) findViewById(R.id.ImageViewSmallContact);
Here imageViewSmallContact returns null.
Then in this preference I register OnPreferenceClickListener
Inside onPreferenceClick findViewById(R.id.ImageViewSmallContact); return correct value.
My question is :
How can I initialize this layout to get imageView from my PreferenceActivity ?

When setting the xml configuration for the PreferenceActivity, the layout for the Preferences isn't immediately inflated. This is only done, once it is needed. You could try to debug into
android.preference.Preference.onBindView/onCreateView(..)
to see, when this is called. I would think it is similar to the inflating of views in a ListView which is only done when they are actually displayed.
What are you trying to do to your ImageView? Maybe you can specify it inside your layout.xml file already?
It might be possible to override Preference and supply a custom implementation of onBindView() that does whatever is necessary to your ImageView.
I can only speculate here, because I ditched the whole PreferenceActivity when I wanted more sophisticated layout, but I would think you could create a subclass of Preference which you then reference in your xml. In that class (e.g. MyImagePreference) you would override the onBindView method to initialize your view as needed (after the super.onBindView() call).

Related

How the view are refereed under MainActivity In android

Here is the XML code for a simple TextView:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:id="#+id/test"/>
Then, in MainActivity, it is used like this:
TextView test = (TextView) findViewById(R.id.test);
test.setText("test");
I want to know how the view is accessed in Main class, which is defined in xml layout.
Can anyone explain how it happens?
how the view accessed in Main class
To be honest, i dont know the process in detail. Let me help you on what i know :
The Activity will search the layout XML in setContentView method.
After the layout has been found, we can use findViewById to link the instance (test - in your case) we created to the layout XML.
If the ID is found, the instance (Java) and XML will be linked.
Of course, you can do something like :
TextView test2 = new TextView(this);
Which means the instance is not must exist in XML.
Sorry English is not my native language.
On your Activity, you have:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//...
}
According to Android documentation, setContentView is responsible for linking your XML layout to your Activity:
Set the activity content to an explicit view. This view is placed
directly into the activity's view hierarchy. It can itself be a
complex view hierarchy.
After the Activity's content view is set, you can use findViewById in order to access the views of the XML linked to your Activity:
Finds a view that was identified by the id attribute from the XML that
was processed in onCreate(Bundle).
Hope it helps you to understand this process! :)
I think this is the method
1.when you create or declare a text view or something like that a reference is generated in your R file(JAVA file).
2.which is what you access through
R.id.test
this is how your linking works

How can I setText() (TextView) when the TextView is in another layout?

My app has 2 layouts (main layout) and (preference (prefs) layout).
When the MainActivity loads, I set setContentView(R.layout.main); - main layout
I need to then set text for a TextView in the preference layout, but it never gets set.
LayoutInflater factory = getLayoutInflater();
View inflate = factory.inflate(R.layout.prefs, null);
TextView eSerial = (TextView) inflate.findViewById(R.id.editTextSerial);
mSerial = "Test";
eSerial.setText(mSerial);
The way I get to the preference page is with a menu and then the page loads up with no change to TextView
I have searched and not found an answer yet.
Please help.
Thank you.
When the menu kicks off your prefs activity, you can populate the view with the values. user1853479 points out one way of doing this, which is to add the values to an intent. Assuming you want to store these prefs for future runs, you can also set any that for that specific run and save them in your local store. Another method is to create a singleton to store your settings, load it when your app starts, modify and save as needed, and access it from any of your activities.
Short answer: You can't do this.
Long answer:
If you are launching the preference page yourself, you must be creating an Intent to do so. Call putExtra() to store your text inside that intent. In your PreferenceActivity, call getIntent().getStringExtra() to get the text, then put it in your TextView.

PreferenceFragment - Difference between getPreferenceManager() and getPreferenceScreen()?

I've implemented my own PreferenceFragment subclass (detailed here), and want to listen for preference changes within it. PreferenceFragment provides you with two ways of doing this:
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
and
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
Which one should be used? What's the difference? I don't really understand the distinction made in the Android docs.
The core difference is in their names, PreferenceManger grants access to different functionalities to the developer for managing SharedPreferences, such as retrieving the map of current preference values or setting user preferences. to their default values. PreferenceScreen handles displaying a screen of user preferences, so that the user can assign values to them. Sometimes this means displaying a list item on a screen with other preferences, that opens another screen with more preferences when clicked, as is the case when PreferenceScreens are nested.
Your question implies that you think there is a difference between what PreferenceManager.getSharedPreferences() and PreferenceScreen.getSharedPreferences() does, but according to the source code, they are identical.
PreferenceScreen:
public SharedPreferences getSharedPreferences() {
if (mPreferenceManager == null) {
return null;
}
return mPreferenceManager.getSharedPreferences();
}
So the PreferenceManger and PreferenceScreen are different entities, but the SharedPreference those method return should be the same object, since PreferenceScreen calls the method from PreferenceManager. I hope that is the answer you've been seeking.
If you have a choice, go with PreferenceManager.getSharedPreferences(), it's more obvious and one fewer method call internally.
Fun fact:
PreferenceFragment:
public PreferenceManager getPreferenceManager() {
return mPreferenceManager;
}
public PreferenceScreen getPreferenceScreen() {
return mPreferenceManager.getPreferenceScreen();
}
The first one gets the shared preferences from the PreferenceManager. The second one, from the PreferenceScreen, that inherits this method from Preference class.
I think this is not a functional difference, because both return probably the same instance of the SharedPreferences objects, but I think it's clearer to use the first one (using PreferenceManager instead of PreferenceScreen).
PreferenceScreen see domentation here
PreferenceScreen class can appear in two places:
When a PreferenceActivity points to this, it is used as the root and
is not shown (only the contained preferences are shown).
When it appears inside another preference hierarchy, it is shown and
serves as the gateway to another screen of preferences (either by
showing another screen of preferences as a Dialog or via a
startActivity(android.content.Intent) from the getIntent()). The
children of this PreferenceScreen are NOT shown in the screen that
this PreferenceScreen is shown in. Instead, a separate screen will be
shown when this preference is clicked.
PreferenceManager see documentation here:
Difference :
getPreferenceManager () returns the current preference manager associated with the fragment.
getPreferenceScreen () returns the root PreferenceScreen i.e. root preference screen used in the fragment from preference xml file(preferences.xml).

Calling an empty function from Android layout file crashes App

I have a checkbox in the Android layout file. When the checkbox is clicked i call an empty function in the activity class. This causes the app to stop working. Why is this?
I am assuming that you are using the "onclick" attribute in your XML.
If you are receiving a MethodNotFound exception, it could be one of two things:
You have a typo in either your Activity's method name or your XML's method name, or...
The visibility of your method in your Activity is not public.
When specifying onclick values in XML, the method should look like this in your activity:
public void myOnClickMethod(View v) { ... }

Going from a PreferenceScreen to a DialogPreference

My application has a setting menu which is actually a PreferenceActivity.
When it's created, if a boolean value is not set I want to go to the DialogPreference which sets that.
I tried doing it with an intent but the application force closed with this error msg:
E/AndroidRuntime( 239):
android.content.ActivityNotFoundException:
Unable to find explicit activity class
{com.xxxx/com.xxxx.xxxxPreference};
have you declared this activity in
your AndroidManifest.xml?
How should I do this? It's ok to add that DialogPreference to the manifest?
A DialogPreference isn't an Activity in its own right. It's just a Preference which displays a Dialog when clicked.
The problem is that there's no obvious way programmatically click a Preference. However, since you're using DialogPreference you've already got you own subclass of it. So we can solve our problem by adding the following method to your subclass of DialogPreference:
//Expose the protected onClick method
void show() {
onClick();
}
Then in the onCreate() of your PreferencesActivity you'll have something like this to load the preferences from your XML file:
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
After that you can put some code like this:
booleanProp = true; //set this to the value of the property you're checking
if (! booleanProp) {
//Find the Preference via its android:key
//MyDialogPreference is your subclasss of DialogPreference
MyDialogPreference dp = (MyDialogPreference)getPreferenceScreen().findPreference("dialog_preference");
dp.show();
}
This is a bit of hack, as exposing protected methods isn't ideal, but it does work.
Another option would be to replace the Dialog with a PrefenceActivity which contained all the options you wish to maintain and then you could launch it via an Intent, but I'm assuming there's a good reason that you want your own custom Dialog with a specific layout. If you do want a second PreferenceActivity you can add it to your preferences XML file as follows:
<PreferenceScreen
android:title="#string/title_of_preference"
android:summary="#string/summary_of_preference">
<intent android:action="your.action.goes.HERE"/>
</PreferenceScreen>
To start an activity with an Intent, the activity must be in the Android manifest. Just add a line like:
<activity android:name=".path.to.MyActivity"/>

Categories

Resources