Going from a PreferenceScreen to a DialogPreference - android

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"/>

Related

Dynamically show Activity as dialog

I have an Activity that I have already implemented sometime ago.
It involves around making a in app purchase, so all the logic is relatively self contained. it doesn't need to care about anything else.
Now, i wish to make that Activity to optionally show up in a dialog in some other activity. Is there a quick way to do that? I still need to keep the old behavior however, where the activity show up as a regular screen.
So is there someway that I could launch the activity with that make it show up as a dialog?
Thanks
You cant show activity as dialog.
Your options are:
1: Open the other activity with some boolean extra like "showDialog", true
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("showDialog", true);
and in the other activity in (for example) onCreate:
Boolean showDialog = getIntent().getExtras().getBoolean("showDialog");
if (showDialog) {
// Code to show dialog
}
2: Create a DialogFragment and show it in your original activity. This custom DialogFragment you can use on both activities
https://guides.codepath.com/android/Using-DialogFragment
Probably your cleanest option depending on how complex your Activity is, is to create a new DialogFragment based on your current activity.
A DialogFragment is basically a Fragment, so has a relatively similar set of lifecycle callbacks to your Activity so it shouldn't be too difficult to re-work as a DialogFragment.
If the in-app purchase framework has specific callback requirements with an Activity then you will need to take that into account.
Another separate option would be to mock the appearance of a Dialog, by creating an Activity that may be transparent around the border of the main content.
Just Inflate the layout one button click on onCreate Method.
WhAT I WILL SUGGEST IS try alert box and in place of normal layout inflate you activity layout .
these might help
The easiest way to do that is to apply a dialog theme to the activity:
<activity android:theme="#style/Theme.AppCompat.Dialog" />
Or in the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog);
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
You can customize parameters of the theme in styles.xml, e.g. dim enabled/disabled, click outside behavior.
The crucial point is to perform setTheme() before super.onCreate(), because Theme is immutable, once set through super.onCreate() it cannot be mutated later.

How to initialize my own Preference layout?

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).

Providing my own list view for a prefrence activity

I have a class which extends from ListView. I added some extra functionality(drag and drop) for this new class. My question is there any way i can use this extended ListView class in a preference activity.
I need to provide drag and drop functionality for a legacy preference activity.
Are you asking if it's possible to open an arbitrary activity from a clicked item in a preference activity? If so, you need to do two things. First, add a PreferenceScreen item to your preferences xml file:
<PreferenceScreen
android:key="CUSTOM_ACTIVITY_KEY"
android:title="Title"
android:summary="Summary" />
Then in your settings activity's onCreate:
Preference pref = getPreferenceScreen().findPreference("CUSTOM_ACTIVITY_KEY");
final Intent intent = new Intent(this, CustomActivity.class);
if (pref != null)
{
pref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(final Preference preference)
{
startActivity(intent);
return false;
}
});
}
If running a custom activity off of a PreferenceScreen item isn't enough, the only other option I could think of is to roll your own Preferences implementation. Someone else should correct me if I'm mistaken, but I think it might be possible to pull the Preferences source and modify accordingly.
I came across another API in
android.preference.PreferenceScreen.bind(ListView listView)
Binds a ListView to the preferences contained in this PreferenceScreen via getRootAdapter().
So after inflating the layout file, we will be able to attach the our custom listview to this the preferenceScreen of the activity.

Starting custom PreferenceActivity INSIDE another PreferenceActivity

Inside my Configuration activity i need to create a preference screen with a fixed View at the top showing a preview of content configured in the page. I don't want to change the main preference screen (i have already a separate activity for that) i want a different layout for a "nested" preferencescreen.
What i've tried is specifying an Intent inside the preference screen however when i click on this options nothing happens and activity goes into timeout... Activity is correctly configured on the manifest (and extends ConfigureActivity as the main one does).
<PreferenceScreen
android:key="inner"
android:title="Title"
android:summary="Summary"
>
<intent
android:action="android.appwidget.action.APPWIDGET_CONFIGURE"
android:targetPackage="my.package.lib"
android:targetClass="my.package.lib.ConfigureClass"
/>
</PreferenceScreen>
Another idea could be creating a custom "preference" that launches another configuration activity, could this work? Would be correct/acceptable to have multiple configuration activities?
The following code on the main ConfigureActivity works however i don't know if its a clean way to do what i want. Could someone confirm?
PreferenceScreen b = (PreferenceScreen) findPreference("my_empty_preference_screen");
b.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(ConfigureActivity.this, ConfigureActivity.class);
intent.setAction("android.appwidget.action.APPWIDGET_CONFIGURE");
ConfigureActivity.this.startActivity(intent);
return false;
}
});

Is it possible to display a DialogPreference without showing a PreferenceScreen?

I'm currently writing a live wallpaper for Android and it has a PreferenceScreen which currently contains only one preference - a DialogPreference to set various properties of animation.
User workflow to configure it currently looks like this:
Settings... => (shows the preferences list with only one title ) Animation speed => MyDialogPreference
What I want is to make the workflow like this:
Settings... => MyDialogPreference
I.e. I'm looking for a way to skip showing that preferences list with only one item and to show that dialog right away.
But it seems that PreferenceActivity requests itself to have PreferenceScreen as a root element of preference hierarchy. So... is it even possible to do what i want? :)
Code references:
Activity code:
public class ForestLakePreferences extends PreferenceActivity
{
protected void onCreate(Bundle savedState)
{
super.onCreate(savedState);
getPreferenceManager().setSharedPreferencesName(
ForestLakeWallpaper.PREFS_NAME);
addPreferencesFromResource(R.xml.preferences);
}
}
Prefs resource:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="lake_preferences">
<DurationEditDialog
android:title="#string/prefs_duration_title"
android:dialogTitle="#string/configure_durations_dlg_title"
android:dialogLayout="#xml/set_durations_layout" />
</PreferenceScreen>
It turned out this can't be done in that way.
But i've found a workaround: i made my activity not a PreferencesActivity, but a custom one and made it look like dialog by placing the following in AndroidManifest.xml
<activity android:theme="#android:style/Theme.Dialog" .... />
A DialogPreference has a showDialog(Bundle state) method, try calling it. I am not sure if you will have to give it anything else like the Preferences or anything.

Categories

Resources