How to set a Content Description on an Android Preference view
Asked Answered
S

1

7

Is there a way to gain add a Content Description to the View representing a Preference in a PreferenceFragment or PreferenceActivity?

Basically I need a way to obtain a handle on the View representing the preference. This would allow me to setContentDescription(...) on the view.

It is surprising that this is not directly exposed as an xml attribute in preference xml files, since this is an accessibility feature it seems like it should be everywhere a UI element can be proxied / generated.

Sordid answered 9/10, 2014 at 0:1 Comment(3)
Are you trying to override the speech generated from the title and summary, or do you need to specify the content description for some other reason?Esquire
@Esquire I need to set the content description for automated UI testing :-\Sordid
Any success with this or workaround for automated UI testing?Vapor
R
0

Getting the View from the Preference is the obvious way, since setContentDescription() can be called on any View. Now, how to get the View is not very well documented but it is fairly simple. In your Preference Fragment class, do the following:

public class SettingsFragment extends PreferenceFragment {
    
    ...
    private Preference toGetViewFrom;
    private ViewGroup parent;
    private View obtainedView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ...
        parent = container;
        obtainedView = toGetViewFrom.getView(null, parent);
        //Now do whatever you want with your view. In your case,
        obtainedView.setContentDescription("Content Description");
    }
Richia answered 1/5, 2021 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.