Android, show dialog when ListPreference item is clicked - java

Basically I have a ListPreference to allow a user to change the X position of some text on my Live Wallpaper.
It contains 4 entries: top, middle, bottom and manually input X. The first 3 options are no problem, I simply get the SharedPreferences in my WallpaperService class and check if they are top, middle or bottom and change the position corresponding to their choice.
However, the last option is proving more difficult, what I want to do is have an EditText alert box popup when the user clicks the "Manually input X" ListPreference item so they can enter a value for X. I just cant figure out how to make the alert popup from clicking that specific List element.

You probably want to create a custom ListPreference. Basically you want to extend from ListPreference (see original here), and provide a custom protected void onPrepareDialogBuilder(Builder builder), in which you provide the additional "custom" list item and the onclick to handle the selection of the "custom" entry.
Note that I keep saying "custom" because it would be a best practice to make this class as reusable as possible.

Override onPreferenceTreeClick() in your PreferenceActivity and compare the preference it gives to the one you want to do something for.

Related

How to keep a users selection even after they close the app? Android

So at the moment the user will select a colour, shape and time. When the app first starts up there is a default for each. What I want to happen is to keep the users selection for all 3 of these even after they close the app while it runs in the background.
If you need me to post anything to help ask away. Thanks.
After writing out what the problem was, it's not that they're not being saved it's the fact that the selections ARE being saved but not being displayed properly. I.E by a border or the dropdown menu results back to it's default value instead of the users selection.
So the values are being saved(my bad) but the app overwrites these saves and doesn't use the border or beings the dropdown menu back to it's default value.
circularImageView = (CircularImageView)findViewById(R.id.activity_main_silver_color_button);
circularImageView.setBorderColor(getResources().getColor(R.color.unselected_border));
circularImageView = (CircularImageView)findViewById(v.getId());
circularImageView.setBorderColor(getResources().getColor(R.color.selected_border));
These work independent of each other but when the border is selected, if a another color was selected then it becomes an unselected border. But what happens if "selected_border" state is never saved.
public static void saveLockScreenDuration(Context context, int duration){
getStoredPreferences(context).edit().putInt(SELECTED_DURATION, duration).apply();
public static int getLockScreenDuration(Context context){
return getStoredPreferences(context)
.getInt(SELECTED_DURATION, DEFAULT_DURATION);
}
This is where all the prefereces are stored. DEFAULT_DURATION and SELECTED_DURATION are in a class called constants.
Upon selection you can store selected values in Shared Preferences. And upon launching it again you can check if those values are null or not. And display accordingly. Read how to use Shared Preferences here.
if you talking about for activity restore data.
then see here.
https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras
Correct me if i am wrong. I think you can use PreferenceManager.getDefaultSharedPreferences(Context context)
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
which is shared across all your Activity and Service classes. If you set an accessibility service to get the event when app is in background and used defaultsharedprefrences to store i think it will work for your case.

Android - how to turn numbers in a TextView into buttons

My utility app requires the user to select a meeting from the calendar, then it removes the non-numerical text, and parses what remains using some logic, with the final aim of identifying two numbers only.
Whenever the app fails to remain with 2 numbers only, I want to open a new activity in which the user will "tell" the app which 2 numbers are the relevant ones.
So I want to display the meeting title, location, and description in 3 TextViews but all numbers should be clickable with a special onClick action that will trigger some methods that handle the identified two numbers...
My question is about how to turn the numbers in the textViews to be clickable and have the special onClick action.
I don't think I can use Linkify as I cannot put an onClickListener on the links.
My only idea now is to turn the numbers into clickable buttons and add the relevant listener to the buttons.
1) Any suggestion on how to do this conversion other than simple step by step parsing?
2) Any better idea than using buttons?
Thanks a million
call setOnClickListener(this) on the TextView and supply a onClick method in your code

Highlight and disable Listview items

I have a little Problem with my ListView.
I fill my ListView with an ArrayList<String> per ArrayAdapter<String> with more than 100 items.
Before my dialog will be show i want to highlight and disable some items. I have found the solution with ListView.post(new Runnable() {...});
I highlight my items with lv.getChildAt(2).setBackgroundColor(Color.BLUE);
and disbable an item with lv.getChildAt(3).setEnabled(false);
I do both also in an OnItemClickListener().
Now my problem:
if in the ListView are 11 items visible at runtime, the the highligt- and disable-pattern will repeat every 11 items.
i.e. if i highlight just the 3. item also the 14., 25. ... item will get a blue background.
And if i disable the 4. item also the 15. and 26. and so on is disabled.
If i scroll fast to buttom and back to top other items are highlighted and disabled.
Another problem is, that i can only access the first (11) visible items in the post-runnable. If i try to highlight the 20. item the app will crash with a NullPointerException.
What can i do to prevent the "item-recycling" and to get full access to all items before the Dialog is shown?
I'm not sure i understand what you want to achieve but here are some suggestions for you.
1) Always Recycle, you should never avoid recicling since you maybe run into another problem, you will run out of memory.
2) In your model implement the checkeable interface, so the model should know if an item is selected or not, Not the view only
3) When iterating each element make use of a ViewHolder and then check the model to see if the elment being inflaed it selected or not and use the desired color
Please have a look at this example link, it describes the use of CAB (Contextual Action Bar) but it uses the things i'm mentioning.
I hope it helps you.

View's contentDescription does not work correctly with TextToSpeech

I work with TextToSpeech and adding "android:contentDescription="#string/custom_button" for ImageButton in xml, reads the value from "#string/custom_button" and adds another "button" in speech.
EG:
"#string/custom_button" = "Custom Button" mapped for mentiond ImageButton, is read as "Custom Button button". There is no text in this button. Id is much different.
How to get rid ot this last "button". It refers to all views read by TextToSpeech.
From my work/research with accessibility, Android automatically adds "button' to the end of talk back for buttons. I could not find any documentation breaking now accessibility to the coding level, but from what i gather they probably add this in the case the developer forgets to indicate in the content description that the the user has clicked a button. There is nothing you can do to remove android's addition of the word button. What you can do is remove it from you custom description so that is only says "Custom". Hope this helps.

PreferenceActivity, how to make a Preference item that will navigate back through menus?

My overriding question is this: In Android PreferenceActivity, how can I write an OnClickListener which will have the same functionality as pressing the Android back button as I navigate through PreferenceScreen defined menus? That is to say, I would like users of my App to explicity see a menu choice "Back" which will bring them to the previous menu, or bring them out of the menu activity to their previous activity if they are at the root of this particular PreferenceActivity session.
The android developer documents tell us
Note that this XML resource contains a preference screen holding another fragment, the Prefs1FragmentInner implemented here. This allows the user to traverse down a hierarchy of preferences; pressing back will pop each fragment off the stack to return to the previous preferences.
And they are correct about that. I navigate happily through my menus by clicking on PreferenceScreen items to get to that screen, and using the Android back button to go Back up a level. But I'm not sure a casual user really understands the "Back" button, I know I didn't until I read about it in Developer docs. SO I would would like them to have an explicit Preference defined menu choice whos OnClickListener duplicates the function of the Android back button.
So I tried to put in a Preference in my menu that would go back. Having determined that a not Overriden onBackPressed in a my subclass of PreferenceActivity just referred back to Activity.onBackPressed() which merely calls finish(), I tried this OnClickListener:
private OnPreferenceClickListener clickFinishesSuccessfully = new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
finish();
return true;
}
};
As it turns out, this did NOT do the same thing as pressing the back button! Pressing this button always took me out of the PreferenceActivity entirely, back to the Activity from which I had called my PreferenceActivity. Specifically, it did NOT navigate back through my menus no matter how deep I was when I clicked it.
I am guessing here: When I have gotten to a submenu by clicking an onscreen preference which is really a PreferenceScreen, I am no longer in my own PreferenceActivity. I must be in some other Activity?
So my functional question: what can I put in my OnClickListener of my "Back" Preference to get the same function as the Android Back button navigating through my menus?
I think the casual user should know about the back button. The button is used everywhere so it might be a problem getting used in the first day but after that it's natural. Without being used to the "back" button I can hardly imagine doing the everyday tasks.
The preference you want to add just duplicates functionality and doesn't provide a consistent way with the rest of the system. If Google was considering back being an uncommon thing for casual users would have added that option in phone's Settings which is also a PreferenceActivity.

Categories