Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify theme of a ListFragment to be different from the activity?

Basically I want to set my activity's theme in the manifest as follows:

android:theme="@android:style/Theme.Translucent.NoTitleBar"

And I want my activity to load up a ListFragment with the theme, Theme.Holo.Dialog (defined in my own style), however I can't just call setStyle(....) in my fragment as I could if it were a DialogFragment.

I believe that I should be able to use a ContextThemeWrapper but I'm having trouble understanding the calls I need to make. So far in my onCreateView I have:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    LayoutInflater newInflater = inflater.cloneInContext(new ContextThemeWrapper(getActivity(), R.style.DialogThemeSelector));
    View inflatedView = newInflater.inflate(R.layout.favourites_print, container, false);
    .....

But this is not working. Thanks in advance. Peter.

*Edit * Posting style:

<style name="DialogThemeSelector" parent="@android:style/Theme.Holo.Dialog"/>
like image 878
PJL Avatar asked May 25 '11 08:05

PJL


2 Answers

Try getting a new LayoutInflater instead of converting the provided one, like so:

Context newContext = new ContextThemeWrapper(getActivity(), R.style.DialogThemeSelector);
LayoutInflater newInflater = (LayoutInflater) newContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View inflatedView = newInflater.inflate(R.layout.favourites_print, container, false);

If that doesn't work, I would suspect that there may be something missing in your style.

like image 70
Jason Robinson Avatar answered Oct 11 '22 09:10

Jason Robinson


I just do it the naive way: I define my list item layouts in XML, using the style attribute, and it seems to work. My list items are not so much concerned with whether they are in a fragment or not. I have not tried styling the fragment itself, but since the list items are the only thing in it, styling them seems sufficient.

like image 33
Sparky Avatar answered Oct 11 '22 09:10

Sparky